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 java.util.Objects; 21 import org.eclipse.jdt.core.dom.MethodDeclaration; 22 import org.eclipse.jdt.core.dom.SingleVariableDeclaration; 23 24 import java.util.ArrayList; 25 import java.util.List; 26 27 /** 28 * Matches the parameters associated with a method. 29 */ 30 public final class ParameterMatcher { 31 32 private final List<String> parameterTypes; 33 ParameterMatcher(List<String> parameterTypes)34 public ParameterMatcher(List<String> parameterTypes) { 35 this.parameterTypes = parameterTypes; 36 } 37 matches(MethodDeclaration methodDeclaration)38 public boolean matches(MethodDeclaration methodDeclaration) { 39 List<String> actualParameterTypes = getParameterTypeNames(methodDeclaration); 40 if (actualParameterTypes.size() != parameterTypes.size()) { 41 return false; 42 } 43 for (int i = 0; i < parameterTypes.size(); i++) { 44 String actualTypeName = actualParameterTypes.get(i); 45 String expectedTypeName = parameterTypes.get(i); 46 if (!actualTypeName.equals(expectedTypeName)) { 47 return false; 48 } 49 } 50 return true; 51 } 52 53 /** 54 * Returns the (simple) type names of the parameters for the supplied {@code methodDeclaration}. 55 */ getParameterTypeNames(MethodDeclaration methodDeclaration)56 public static List<String> getParameterTypeNames(MethodDeclaration methodDeclaration) { 57 List<SingleVariableDeclaration> parameters = 58 (List<SingleVariableDeclaration>) methodDeclaration.parameters(); 59 List<String> toReturn = new ArrayList<>(parameters.size()); 60 for (SingleVariableDeclaration singleVariableDeclaration : parameters) { 61 String actualTypeName = singleVariableDeclaration.getType().toString(); 62 if (singleVariableDeclaration.isVarargs()) { 63 // toString() does the right thing in most cases but doesn't do varargs properly. 64 actualTypeName += "..."; 65 } 66 toReturn.add(actualTypeName); 67 } 68 return toReturn; 69 } 70 71 /** 72 * Returns the parameter types of this matcher in a comma separated list. 73 */ toStringForm()74 public String toStringForm() { 75 return Joiner.on(",").join(parameterTypes); 76 } 77 78 @Override equals(Object o)79 public boolean equals(Object o) { 80 if (this == o) { 81 return true; 82 } 83 if (!(o instanceof ParameterMatcher)) { 84 return false; 85 } 86 ParameterMatcher that = (ParameterMatcher) o; 87 return Objects.equals(parameterTypes, that.parameterTypes); 88 } 89 90 @Override hashCode()91 public int hashCode() { 92 return Objects.hash(parameterTypes); 93 } 94 95 @Override toString()96 public String toString() { 97 return "ParameterMatcher{" + 98 "parameterTypes=" + parameterTypes + 99 '}'; 100 } 101 } 102