/** * Copyright 2011-2015 John Ericksen * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.androidtransfuse.adapter; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * @author John Ericksen */ public class MethodSignature { private final String signature; public MethodSignature(ASTMethod method) { this.signature = makeSignature(method); } public MethodSignature(String methodName, List<ASTType> paramTypes){ this.signature = makeSignature(methodName, paramTypes); } public MethodSignature(String methodName, ASTType... paramTypes){ this(methodName, Arrays.asList(paramTypes)); } /** * Makes a descriptor for a given method. * * @param method * @return descriptor */ private String makeSignature(ASTMethod method) { List<ASTType> paramTypes = new ArrayList<ASTType>(); for (ASTParameter parameter : method.getParameters()) { paramTypes.add(parameter.getASTType()); } return makeSignature(method.getName(), paramTypes); } private String makeSignature(String methodName, List<ASTType> params){ return methodName + ':' + makeSignature(params); } /** * Makes a descriptor for a given method. * * @param paramTypes parameter types. * @return method descriptor */ private String makeSignature(List<ASTType> paramTypes) { StringBuilder builder = new StringBuilder(); builder.append('('); for (ASTType paramType : paramTypes) { makeTypeDescriptor(builder, paramType); } builder.append(')'); return builder.toString(); } private void makeTypeDescriptor(StringBuilder builder, ASTType type) { if (type instanceof ASTArrayType) { builder.append('['); } makeTypeDesc(builder, type); } private void makeTypeDesc(StringBuilder builder, ASTType type) { if (type instanceof ASTPrimitiveType) { builder.append(type.getName()); } else { builder.append('L').append(type.getName().replace('.', '/')).append(';'); } } public String toString(){ return signature; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof MethodSignature)) { return false; } MethodSignature that = (MethodSignature) o; return new EqualsBuilder().append(signature, that.signature).isEquals(); } @Override public int hashCode() { return new HashCodeBuilder().append(signature).hashCode(); } }