• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.google.currysrc.api.process.ast;
17 
18 import com.google.common.base.Joiner;
19 
20 import org.eclipse.jdt.core.dom.MethodDeclaration;
21 import org.eclipse.jdt.core.dom.SingleVariableDeclaration;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * Matches the parameters associated with a method.
28  */
29 public final class ParameterMatcher {
30 
31   private final List<String> parameterTypes;
32 
ParameterMatcher(List<String> parameterTypes)33   public ParameterMatcher(List<String> parameterTypes) {
34     this.parameterTypes = parameterTypes;
35   }
36 
matches(MethodDeclaration methodDeclaration)37   public boolean matches(MethodDeclaration methodDeclaration) {
38     List<String> actualParameterTypes = getParameterTypeNames(methodDeclaration);
39     if (actualParameterTypes.size() != parameterTypes.size()) {
40       return false;
41     }
42     for (int i = 0; i < parameterTypes.size(); i++) {
43       String actualTypeName = actualParameterTypes.get(i);
44       String expectedTypeName = parameterTypes.get(i);
45       if (!actualTypeName.equals(expectedTypeName)) {
46         return false;
47       }
48     }
49     return true;
50   }
51 
52   /**
53    * Returns the (simple) type names of the parameters for the supplied {@code methodDeclaration}.
54    */
getParameterTypeNames(MethodDeclaration methodDeclaration)55   public static List<String> getParameterTypeNames(MethodDeclaration methodDeclaration) {
56     List<SingleVariableDeclaration> parameters =
57         (List<SingleVariableDeclaration>) methodDeclaration.parameters();
58     List<String> toReturn = new ArrayList<>(parameters.size());
59     for (SingleVariableDeclaration singleVariableDeclaration : parameters) {
60       // toString() does the right thing in all cases.
61       String actualTypeName = singleVariableDeclaration.getType().toString();
62       toReturn.add(actualTypeName);
63     }
64     return toReturn;
65   }
66 
67   /**
68    * Returns the parameter types of this matcher in a comma separated list.
69    */
toStringForm()70   public String toStringForm() {
71     return Joiner.on(",").join(parameterTypes);
72   }
73 
74   @Override
toString()75   public String toString() {
76     return "ParameterMatcher{" +
77         "parameterTypes=" + parameterTypes +
78         '}';
79   }
80 }
81