List of usage examples for org.eclipse.jdt.core.dom ITypeBinding getDeclaredMethods
public IMethodBinding[] getDeclaredMethods();
From source file:astview.Binding.java
License:Open Source License
@Override public Object[] getChildren() { try {/*from ww w.j a v a 2s. co m*/ if (fBinding != null) { fBinding.getKey(); } } catch (RuntimeException e) { return new Object[] { new Error(this, "BrokenBinding: " + fBinding, null) }; } if (fBinding != null) { ArrayList<ASTAttribute> res = new ArrayList<>(); res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS RECOVERED", fBinding.isRecovered(), true)); //$NON-NLS-1$ switch (fBinding.getKind()) { case IBinding.VARIABLE: IVariableBinding variableBinding = (IVariableBinding) fBinding; res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), false), //$NON-NLS-1$ true)); res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$ res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "CONSTANT VALUE", variableBinding.getConstantValue(), true)); //$NON-NLS-1$ //$NON-NLS-2$ res.add(new BindingProperty(this, "IS EFFECTIVELY FINAL", variableBinding.isEffectivelyFinal(), //$NON-NLS-1$ true)); break; case IBinding.PACKAGE: IPackageBinding packageBinding = (IPackageBinding) fBinding; res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ break; case IBinding.TYPE: ITypeBinding typeBinding = (ITypeBinding) fBinding; res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$ int typeKind = getTypeKind(typeBinding); boolean isRefType = isType(typeKind, REF_TYPE); final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE); StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$ if (typeBinding.isArray()) kinds.append(" isArray"); //$NON-NLS-1$ if (typeBinding.isCapture()) kinds.append(" isCapture"); //$NON-NLS-1$ if (typeBinding.isNullType()) kinds.append(" isNullType"); //$NON-NLS-1$ if (typeBinding.isPrimitive()) kinds.append(" isPrimitive"); //$NON-NLS-1$ if (typeBinding.isTypeVariable()) kinds.append(" isTypeVariable"); //$NON-NLS-1$ if (typeBinding.isWildcardType()) kinds.append(" isWildcardType"); //$NON-NLS-1$ // ref types if (typeBinding.isAnnotation()) kinds.append(" isAnnotation"); //$NON-NLS-1$ if (typeBinding.isClass()) kinds.append(" isClass"); //$NON-NLS-1$ if (typeBinding.isInterface()) kinds.append(" isInterface"); //$NON-NLS-1$ if (typeBinding.isEnum()) kinds.append(" isEnum"); //$NON-NLS-1$ res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$ StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (typeBinding.isRawType()) generics.append(" isRawType"); //$NON-NLS-1$ if (typeBinding.isGenericType()) generics.append(" isGenericType"); //$NON-NLS-1$ if (typeBinding.isParameterizedType()) generics.append(" isParameterizedType"); //$NON-NLS-1$ if (!isType(typeKind, GENERIC | PARAMETRIZED)) { generics.append(" (non-generic, non-parameterized)"); } res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); final String createArrayTypeLabel = "CREATE ARRAY TYPE (+1)"; try { ITypeBinding arrayType = typeBinding.createArrayType(1); res.add(new Binding(this, createArrayTypeLabel, arrayType, true)); } catch (RuntimeException e) { String msg = e.getClass().getName() + ": " + e.getLocalizedMessage(); boolean isRelevant = !typeBinding.getName().equals(PrimitiveType.VOID.toString()) && !typeBinding.isRecovered(); if (isRelevant) { res.add(new Error(this, createArrayTypeLabel + ": " + msg, e)); } else { res.add(new BindingProperty(this, createArrayTypeLabel, msg, false)); } } StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$ if (typeBinding.isTopLevel()) origin.append(" isTopLevel"); //$NON-NLS-1$ if (typeBinding.isNested()) origin.append(" isNested"); //$NON-NLS-1$ if (typeBinding.isLocal()) origin.append(" isLocal"); //$NON-NLS-1$ if (typeBinding.isMember()) origin.append(" isMember"); //$NON-NLS-1$ if (typeBinding.isAnonymous()) origin.append(" isAnonymous"); //$NON-NLS-1$ res.add(new BindingProperty(this, origin, isRefType)); res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); // res.add(new Binding(this, "DECLARING MEMBER", typeBinding.getDeclaringMember(), typeBinding.isLocal())); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), false), //$NON-NLS-1$ isRefType)); res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$ String isTypeDeclaration = typeBinding == typeBinding.getTypeDeclaration() ? " ( == this)" : " ( != this)"; res.add(new Binding(this, "TYPE DECLARATION" + isTypeDeclaration, typeBinding.getTypeDeclaration(), //$NON-NLS-1$ true)); String isErasure = typeBinding == typeBinding.getErasure() ? " ( == this)" : " ( != this)"; res.add(new Binding(this, "ERASURE" + isErasure, typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$ isType(typeKind, GENERIC))); res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$ isType(typeKind, PARAMETRIZED))); res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$ isType(typeKind, VARIABLE_TYPE | WILDCARD_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$ isType(typeKind, WILDCARD_TYPE))); res.add(new Binding(this, "GENERIC TYPE OF WILDCARD TYPE", //$NON-NLS-1$ typeBinding.getGenericTypeOfWildcardType(), isType(typeKind, WILDCARD_TYPE))); res.add(new BindingProperty(this, "RANK", typeBinding.getRank(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$ res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$ res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "FUNCTIONAL INTERFACE METHOD", typeBinding.getFunctionalInterfaceMethod(), //$NON-NLS-1$ typeBinding.isInterface())); res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE ANNOTATIONS", typeBinding.getTypeAnnotations(), true)); //$NON-NLS-1$ break; case IBinding.METHOD: IMethodBinding methodBinding = (IMethodBinding) fBinding; res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$ true)); res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$ // res.add(new Binding(this, "DECLARING MEMBER", methodBinding.getDeclaringMember(), methodBinding.getDeclaringMember() != null)); //$NON-NLS-1$ res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", getModifiersString(fBinding.getModifiers(), true), //$NON-NLS-1$ true)); res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$ StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (methodBinding.isRawMethod()) genericsM.append(" isRawMethod"); //$NON-NLS-1$ if (methodBinding.isGenericMethod()) genericsM.append(" isGenericMethod"); //$NON-NLS-1$ if (methodBinding.isParameterizedMethod()) genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$ res.add(new BindingProperty(this, genericsM, true)); String isMethodDeclaration = methodBinding == methodBinding.getMethodDeclaration() ? " ( == this)" : " ( != this)"; res.add(new Binding(this, "METHOD DECLARATION" + isMethodDeclaration, //$NON-NLS-1$ methodBinding.getMethodDeclaration(), true)); res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$ true)); res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue())); int parameterCount = methodBinding.getParameterTypes().length; BindingProperty[] parametersAnnotations = new BindingProperty[parameterCount]; for (int i = 0; i < parameterCount; i++) { parametersAnnotations[i] = new BindingProperty(this, "Parameter " + String.valueOf(i), methodBinding.getParameterAnnotations(i), true); } res.add(new BindingProperty(this, "PARAMETER ANNOTATIONS", parametersAnnotations, true)); break; case IBinding.ANNOTATION: IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding; res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true)); res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS", annotationBinding.getDeclaredMemberValuePairs(), true)); res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS", annotationBinding.getAllMemberValuePairs(), true)); break; case IBinding.MEMBER_VALUE_PAIR: IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding; res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true)); res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true)); res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue())); break; } try { IAnnotationBinding[] annotations = fBinding.getAnnotations(); res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$ } catch (RuntimeException e) { String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\""; res.add(new Error(this, label, e)); } try { IJavaElement javaElement = fBinding.getJavaElement(); } catch (RuntimeException e) { String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ res.add(new Error(this, label, e)); } return res.toArray(); } return EMPTY; }
From source file:ca.ecliptical.pde.internal.ds.AnnotationProcessor.java
License:Open Source License
private IMethodBinding findReferenceMethod(ITypeBinding componentClass, ITypeBinding serviceType, String name) { ITypeBinding testedClass = componentClass; IMethodBinding candidate = null;//from ww w . j a va 2s .co m int priority = 0; // priority: // 0: <assignment-compatible-type>, Map // 1: <exact-type>, Map // 2: <assignment-compatible-type> // 3: <exact-type> do { for (IMethodBinding declaredMethod : testedClass.getDeclaredMethods()) { if (name.equals(declaredMethod.getName()) && Void.TYPE.getName().equals(declaredMethod.getReturnType().getName()) && (testedClass.isEqualTo(componentClass) || Modifier.isPublic(declaredMethod.getModifiers()) || Modifier.isProtected(declaredMethod.getModifiers()) || (!Modifier.isPrivate(declaredMethod.getModifiers()) && testedClass.getPackage().isEqualTo(componentClass.getPackage())))) { ITypeBinding[] paramTypes = declaredMethod.getParameterTypes(); if (paramTypes.length == 1) { if (ServiceReference.class.getName().equals(paramTypes[0].getErasure().getQualifiedName())) // we have the winner return declaredMethod; if (priority < 3 && serviceType.isEqualTo(paramTypes[0])) priority = 3; else if (priority < 2 && serviceType.isAssignmentCompatible(paramTypes[0])) priority = 2; else continue; // we have a (better) candidate candidate = declaredMethod; } else if (paramTypes.length == 2) { if (priority < 1 && serviceType.isEqualTo(paramTypes[0]) && Map.class.getName().equals(paramTypes[1].getErasure().getQualifiedName())) priority = 1; else if (candidate != null || !serviceType.isAssignmentCompatible(paramTypes[0]) || !Map.class.getName().equals(paramTypes[1].getErasure().getQualifiedName())) continue; // we have a candidate candidate = declaredMethod; } } } } while ((testedClass = testedClass.getSuperclass()) != null); return candidate; }
From source file:cideplus.ui.astview.Binding.java
License:Open Source License
public Object[] getChildren() { if (fBinding != null) { ArrayList res = new ArrayList(); res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS RECOVERED", fBinding.isRecovered(), true)); //$NON-NLS-1$ switch (fBinding.getKind()) { case IBinding.VARIABLE: IVariableBinding variableBinding = (IVariableBinding) fBinding; res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$ res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$ res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "CONSTANT VALUE", variableBinding.getConstantValue(), true)); //$NON-NLS-1$ //$NON-NLS-2$ break; case IBinding.PACKAGE: IPackageBinding packageBinding = (IPackageBinding) fBinding; res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ break; case IBinding.TYPE: if (1 == 1) { break;//no iremos mostrar todas essas informaes da classe }// w w w . ja v a 2 s.c o m ITypeBinding typeBinding = (ITypeBinding) fBinding; res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$ int typeKind = getTypeKind(typeBinding); boolean isRefType = isType(typeKind, REF_TYPE); final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE); StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$ if (typeBinding.isArray()) kinds.append(" isArray"); //$NON-NLS-1$ if (typeBinding.isCapture()) kinds.append(" isCapture"); //$NON-NLS-1$ if (typeBinding.isNullType()) kinds.append(" isNullType"); //$NON-NLS-1$ if (typeBinding.isPrimitive()) kinds.append(" isPrimitive"); //$NON-NLS-1$ if (typeBinding.isTypeVariable()) kinds.append(" isTypeVariable"); //$NON-NLS-1$ if (typeBinding.isWildcardType()) kinds.append(" isWildcardType"); //$NON-NLS-1$ // ref types if (typeBinding.isAnnotation()) kinds.append(" isAnnotation"); //$NON-NLS-1$ if (typeBinding.isClass()) kinds.append(" isClass"); //$NON-NLS-1$ if (typeBinding.isInterface()) kinds.append(" isInterface"); //$NON-NLS-1$ if (typeBinding.isEnum()) kinds.append(" isEnum"); //$NON-NLS-1$ res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$ StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (typeBinding.isRawType()) generics.append(" isRawType"); //$NON-NLS-1$ if (typeBinding.isGenericType()) generics.append(" isGenericType"); //$NON-NLS-1$ if (typeBinding.isParameterizedType()) generics.append(" isParameterizedType"); //$NON-NLS-1$ if (!isType(typeKind, GENERIC | PARAMETRIZED)) { generics.append(" (non-generic, non-parameterized)"); } res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); final String createArrayTypeLabel = "CREATE ARRAY TYPE (+1)"; try { ITypeBinding arrayType = typeBinding.createArrayType(1); res.add(new Binding(this, createArrayTypeLabel, arrayType, true)); } catch (RuntimeException e) { String msg = e.getClass().getName() + ": " + e.getLocalizedMessage(); boolean isRelevant = !typeBinding.getName().equals(PrimitiveType.VOID.toString()) && !typeBinding.isRecovered(); if (isRelevant) { res.add(new Error(this, createArrayTypeLabel + ": " + msg, e)); } else { res.add(new BindingProperty(this, createArrayTypeLabel, msg, false)); } } res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$ isType(typeKind, VARIABLE_TYPE | CAPTURE_TYPE))); StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$ if (typeBinding.isTopLevel()) origin.append(" isTopLevel"); //$NON-NLS-1$ if (typeBinding.isNested()) origin.append(" isNested"); //$NON-NLS-1$ if (typeBinding.isLocal()) origin.append(" isLocal"); //$NON-NLS-1$ if (typeBinding.isMember()) origin.append(" isMember"); //$NON-NLS-1$ if (typeBinding.isAnonymous()) origin.append(" isAnonymous"); //$NON-NLS-1$ res.add(new BindingProperty(this, origin, isRefType)); res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$ res.add(new Binding(this, "TYPE DECLARATION", typeBinding.getTypeDeclaration(), isNonPrimitive)); //$NON-NLS-1$ res.add(new Binding(this, "ERASURE", typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$ isType(typeKind, GENERIC))); res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$ isType(typeKind, PARAMETRIZED))); res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$ isType(typeKind, WILDCARD_TYPE))); res.add(new Binding(this, "GENERIC TYPE OF WILDCARD TYPE", //$NON-NLS-1$ typeBinding.getGenericTypeOfWildcardType(), isType(typeKind, WILDCARD_TYPE))); res.add(new BindingProperty(this, "RANK", typeBinding.getRank(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$ res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$ res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$ break; case IBinding.METHOD: IMethodBinding methodBinding = (IMethodBinding) fBinding; res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$ true)); res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$ res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$ StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (methodBinding.isRawMethod()) genericsM.append(" isRawMethod"); //$NON-NLS-1$ if (methodBinding.isGenericMethod()) genericsM.append(" isGenericMethod"); //$NON-NLS-1$ if (methodBinding.isParameterizedMethod()) genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$ res.add(new BindingProperty(this, genericsM, true)); res.add(new Binding(this, "METHOD DECLARATION", methodBinding.getMethodDeclaration(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$ true)); res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue())); int parameterCount = methodBinding.getParameterTypes().length; GeneralAttribute[] parametersAnnotations = new GeneralAttribute[parameterCount]; for (int i = 0; i < parameterCount; i++) { parametersAnnotations[i] = new GeneralAttribute(this, "Parameter " + String.valueOf(i), methodBinding.getParameterAnnotations(i)); } res.add(new GeneralAttribute(this, "PARAMETER ANNOTATIONS", parametersAnnotations)); break; case IBinding.ANNOTATION: IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding; res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true)); res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS", annotationBinding.getDeclaredMemberValuePairs(), true)); res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS", annotationBinding.getAllMemberValuePairs(), true)); break; case IBinding.MEMBER_VALUE_PAIR: IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding; res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true)); res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true)); res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue())); break; } try { IAnnotationBinding[] annotations = fBinding.getAnnotations(); res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$ } catch (RuntimeException e) { String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\""; res.add(new Error(this, label, e)); ASTViewPlugin.log("Exception thrown in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"", e); } try { IJavaElement javaElement = fBinding.getJavaElement(); res.add(new JavaElement(this, javaElement)); } catch (RuntimeException e) { String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ res.add(new Error(this, label, e)); ASTViewPlugin.log("Exception thrown in IBinding#getJavaElement() for \"" + fBinding.getKey() + "\"", e); } return res.toArray(); } return EMPTY; }
From source file:coloredide.astview.internal.Binding.java
License:Open Source License
public Object[] getChildren() { if (fBinding != null) { ArrayList res = new ArrayList(); res.add(new BindingProperty(this, "NAME", fBinding.getName(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "KEY", fBinding.getKey(), true)); //$NON-NLS-1$ switch (fBinding.getKind()) { case IBinding.VARIABLE: IVariableBinding variableBinding = (IVariableBinding) fBinding; res.add(new BindingProperty(this, "IS FIELD", variableBinding.isField(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ENUM CONSTANT", variableBinding.isEnumConstant(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS PARAMETER", variableBinding.isParameter(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "VARIABLE ID", variableBinding.getVariableId(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$ res.add(new Binding(this, "TYPE", variableBinding.getType(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", variableBinding.getDeclaringClass(), true)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING METHOD", variableBinding.getDeclaringMethod(), true)); //$NON-NLS-1$ res.add(new Binding(this, "VARIABLE DECLARATION", variableBinding.getVariableDeclaration(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ Object constVal = variableBinding.getConstantValue(); res.add(new BindingProperty(this, "CONSTANT VALUE", constVal == null ? "null" : constVal.toString(), //$NON-NLS-1$//$NON-NLS-2$ true));//from w w w . ja v a 2s. c om break; case IBinding.PACKAGE: IPackageBinding packageBinding = (IPackageBinding) fBinding; res.add(new BindingProperty(this, "IS UNNAMED", packageBinding.isUnnamed(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ break; case IBinding.TYPE: ITypeBinding typeBinding = (ITypeBinding) fBinding; res.add(new BindingProperty(this, "QUALIFIED NAME", typeBinding.getQualifiedName(), true)); //$NON-NLS-1$ int typeKind = getTypeKind(typeBinding); boolean isRefType = isType(typeKind, REF_TYPE); final boolean isNonPrimitive = !isType(typeKind, PRIMITIVE_TYPE); StringBuffer kinds = new StringBuffer("KIND:"); //$NON-NLS-1$ if (typeBinding.isArray()) kinds.append(" isArray"); //$NON-NLS-1$ if (typeBinding.isCapture()) kinds.append(" isCapture"); //$NON-NLS-1$ if (typeBinding.isNullType()) kinds.append(" isNullType"); //$NON-NLS-1$ if (typeBinding.isPrimitive()) kinds.append(" isPrimitive"); //$NON-NLS-1$ if (typeBinding.isTypeVariable()) kinds.append(" isTypeVariable"); //$NON-NLS-1$ if (typeBinding.isWildcardType()) kinds.append(" isWildcardType"); //$NON-NLS-1$ // ref types if (typeBinding.isAnnotation()) kinds.append(" isAnnotation"); //$NON-NLS-1$ if (typeBinding.isClass()) kinds.append(" isClass"); //$NON-NLS-1$ if (typeBinding.isInterface()) kinds.append(" isInterface"); //$NON-NLS-1$ if (typeBinding.isEnum()) kinds.append(" isEnum"); //$NON-NLS-1$ res.add(new BindingProperty(this, kinds, true)); //$NON-NLS-1$ StringBuffer generics = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (typeBinding.isRawType()) generics.append(" isRawType"); //$NON-NLS-1$ if (typeBinding.isGenericType()) generics.append(" isGenericType"); //$NON-NLS-1$ if (typeBinding.isParameterizedType()) generics.append(" isParameterizedType"); //$NON-NLS-1$ if (!isType(typeKind, GENERIC | PARAMETRIZED)) { generics.append(" (non-generic, non-parameterized)"); } res.add(new BindingProperty(this, generics, isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "ELEMENT TYPE", typeBinding.getElementType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new Binding(this, "COMPONENT TYPE", typeBinding.getComponentType(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new BindingProperty(this, "DIMENSIONS", typeBinding.getDimensions(), //$NON-NLS-1$ isType(typeKind, ARRAY_TYPE))); res.add(new BindingProperty(this, "TYPE BOUNDS", typeBinding.getTypeBounds(), //$NON-NLS-1$ isType(typeKind, VARIABLE_TYPE | CAPTURE_TYPE))); StringBuffer origin = new StringBuffer("ORIGIN:"); //$NON-NLS-1$ if (typeBinding.isTopLevel()) origin.append(" isTopLevel"); //$NON-NLS-1$ if (typeBinding.isNested()) origin.append(" isNested"); //$NON-NLS-1$ if (typeBinding.isLocal()) origin.append(" isLocal"); //$NON-NLS-1$ if (typeBinding.isMember()) origin.append(" isMember"); //$NON-NLS-1$ if (typeBinding.isAnonymous()) origin.append(" isAnonymous"); //$NON-NLS-1$ res.add(new BindingProperty(this, origin, isRefType)); res.add(new BindingProperty(this, "IS FROM SOURCE", typeBinding.isFromSource(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "PACKAGE", typeBinding.getPackage(), isRefType)); //$NON-NLS-1$ res.add(new Binding(this, "DECLARING CLASS", typeBinding.getDeclaringClass(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new Binding(this, "DECLARING METHOD", typeBinding.getDeclaringMethod(), //$NON-NLS-1$ isType(typeKind, REF_TYPE | VARIABLE_TYPE | CAPTURE_TYPE))); res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "BINARY NAME", typeBinding.getBinaryName(), true)); //$NON-NLS-1$ res.add(new Binding(this, "TYPE DECLARATION", typeBinding.getTypeDeclaration(), isNonPrimitive)); //$NON-NLS-1$ res.add(new Binding(this, "ERASURE", typeBinding.getErasure(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE PARAMETERS", typeBinding.getTypeParameters(), //$NON-NLS-1$ isType(typeKind, GENERIC))); res.add(new BindingProperty(this, "TYPE ARGUMENTS", typeBinding.getTypeArguments(), //$NON-NLS-1$ isType(typeKind, PARAMETRIZED))); res.add(new Binding(this, "BOUND", typeBinding.getBound(), isType(typeKind, WILDCARD_TYPE))); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS UPPERBOUND", typeBinding.isUpperbound(), //$NON-NLS-1$ isType(typeKind, WILDCARD_TYPE))); res.add(new Binding(this, "WILDCARD", typeBinding.getWildcard(), isType(typeKind, CAPTURE_TYPE))); //$NON-NLS-1$ res.add(new Binding(this, "SUPERCLASS", typeBinding.getSuperclass(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "INTERFACES", typeBinding.getInterfaces(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED TYPES", typeBinding.getDeclaredTypes(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED FIELDS", typeBinding.getDeclaredFields(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "DECLARED METHODS", typeBinding.getDeclaredMethods(), isRefType)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), isNonPrimitive)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), isRefType)); //$NON-NLS-1$ break; case IBinding.METHOD: IMethodBinding methodBinding = (IMethodBinding) fBinding; res.add(new BindingProperty(this, "IS CONSTRUCTOR", methodBinding.isConstructor(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEFAULT CONSTRUCTOR", methodBinding.isDefaultConstructor(), //$NON-NLS-1$ true)); res.add(new Binding(this, "DECLARING CLASS", methodBinding.getDeclaringClass(), true)); //$NON-NLS-1$ res.add(new Binding(this, "RETURN TYPE", methodBinding.getReturnType(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "MODIFIERS", Flags.toString(fBinding.getModifiers()), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "PARAMETER TYPES", methodBinding.getParameterTypes(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS VARARGS", methodBinding.isVarargs(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "EXCEPTION TYPES", methodBinding.getExceptionTypes(), true)); //$NON-NLS-1$ StringBuffer genericsM = new StringBuffer("GENERICS:"); //$NON-NLS-1$ if (methodBinding.isRawMethod()) genericsM.append(" isRawMethod"); //$NON-NLS-1$ if (methodBinding.isGenericMethod()) genericsM.append(" isGenericMethod"); //$NON-NLS-1$ if (methodBinding.isParameterizedMethod()) genericsM.append(" isParameterizedMethod"); //$NON-NLS-1$ res.add(new BindingProperty(this, genericsM, true)); res.add(new Binding(this, "METHOD DECLARATION", methodBinding.getMethodDeclaration(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE PARAMETERS", methodBinding.getTypeParameters(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "TYPE ARGUMENTS", methodBinding.getTypeArguments(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS SYNTHETIC", fBinding.isSynthetic(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS DEPRECATED", fBinding.isDeprecated(), true)); //$NON-NLS-1$ res.add(new BindingProperty(this, "IS ANNOTATION MEMBER", methodBinding.isAnnotationMember(), //$NON-NLS-1$ true)); res.add(Binding.createValueAttribute(this, "DEFAULT VALUE", methodBinding.getDefaultValue())); int parameterCount = methodBinding.getParameterTypes().length; GeneralAttribute[] parametersAnnotations = new GeneralAttribute[parameterCount]; for (int i = 0; i < parameterCount; i++) { parametersAnnotations[i] = new GeneralAttribute(this, "Parameter " + String.valueOf(i), methodBinding.getParameterAnnotations(i)); } res.add(new GeneralAttribute(this, "PARAMETER ANNOTATIONS", parametersAnnotations)); break; case IBinding.ANNOTATION: IAnnotationBinding annotationBinding = (IAnnotationBinding) fBinding; res.add(new Binding(this, "ANNOTATION TYPE", annotationBinding.getAnnotationType(), true)); res.add(new BindingProperty(this, "DECLARED MEMBER VALUE PAIRS", annotationBinding.getDeclaredMemberValuePairs(), true)); res.add(new BindingProperty(this, "ALL MEMBER VALUE PAIRS", annotationBinding.getAllMemberValuePairs(), true)); break; case IBinding.MEMBER_VALUE_PAIR: IMemberValuePairBinding memberValuePairBinding = (IMemberValuePairBinding) fBinding; res.add(new Binding(this, "METHOD BINDING", memberValuePairBinding.getMethodBinding(), true)); res.add(new BindingProperty(this, "IS DEFAULT", memberValuePairBinding.isDefault(), true)); res.add(Binding.createValueAttribute(this, "VALUE", memberValuePairBinding.getValue())); break; } try { IAnnotationBinding[] annotations = fBinding.getAnnotations(); res.add(new BindingProperty(this, "ANNOTATIONS", annotations, true)); //$NON-NLS-1$ } catch (RuntimeException e) { String label = "Error in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\""; res.add(new Error(this, label, e)); ColoredIDEPlugin .log("Exception thrown in IBinding#getAnnotations() for \"" + fBinding.getKey() + "\"", e); } try { IJavaElement javaElement = fBinding.getJavaElement(); res.add(new JavaElement(this, javaElement)); } catch (RuntimeException e) { String label = ">java element: " + e.getClass().getName() + " for \"" + fBinding.getKey() + "\""; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ res.add(new Error(this, label, e)); ColoredIDEPlugin .log("Exception thrown in IBinding#getJavaElement() for \"" + fBinding.getKey() + "\"", e); } return res.toArray(); } return EMPTY; }
From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java
License:Open Source License
/** * If the expression is part of a method invocation (aka a function call) or a * class instance creation (aka a "new SomeClass" constructor call), we try to * find the type of the argument being used. If it is a String (most likely), we * want to return true (to generate a getString() call). However if there might * be a similar method that takes an int, in which case we don't want to do that. * * This covers the case of Activity.setTitle(int resId) vs setTitle(String str). *///from ww w . ja v a 2 s . c om @SuppressWarnings("rawtypes") private boolean examineMethodInvocation(StringLiteral node) { ASTNode parent = null; List arguments = null; IMethodBinding methodBinding = null; MethodInvocation invoke = findParentClass(node, MethodInvocation.class); if (invoke != null) { parent = invoke; arguments = invoke.arguments(); methodBinding = invoke.resolveMethodBinding(); } else { ClassInstanceCreation newclass = findParentClass(node, ClassInstanceCreation.class); if (newclass != null) { parent = newclass; arguments = newclass.arguments(); methodBinding = newclass.resolveConstructorBinding(); } } if (parent != null && arguments != null && methodBinding != null) { // We want to know which argument this is. // Walk up the hierarchy again to find the immediate child of the parent, // which should turn out to be one of the invocation arguments. ASTNode child = null; for (ASTNode n = node; n != parent;) { ASTNode p = n.getParent(); if (p == parent) { child = n; break; } n = p; } if (child == null) { // This can't happen: a parent of 'node' must be the child of 'parent'. return false; } // Find the index int index = 0; for (Object arg : arguments) { if (arg == child) { break; } index++; } if (index == arguments.size()) { // This can't happen: one of the arguments of 'invoke' must be 'child'. return false; } // Eventually we want to determine if the parameter is a string type, // in which case a Context.getString() call must be generated. boolean useStringType = false; // Find the type of that argument ITypeBinding[] types = methodBinding.getParameterTypes(); if (index < types.length) { ITypeBinding type = types[index]; useStringType = isJavaString(type); } // Now that we know that this method takes a String parameter, can we find // a variant that would accept an int for the same parameter position? if (useStringType) { String name = methodBinding.getName(); ITypeBinding clazz = methodBinding.getDeclaringClass(); nextMethod: for (IMethodBinding mb2 : clazz.getDeclaredMethods()) { if (methodBinding == mb2 || !mb2.getName().equals(name)) { continue; } // We found a method with the same name. We want the same parameters // except that the one at 'index' must be an int type. ITypeBinding[] types2 = mb2.getParameterTypes(); int len2 = types2.length; if (types.length == len2) { for (int i = 0; i < len2; i++) { if (i == index) { ITypeBinding type2 = types2[i]; if (!("int".equals(type2.getQualifiedName()))) { //$NON-NLS-1$ // The argument at 'index' is not an int. continue nextMethod; } } else if (!types[i].equals(types2[i])) { // One of the other arguments do not match our original method continue nextMethod; } } // If we got here, we found a perfect match: a method with the same // arguments except the one at 'index' is an int. In this case we // don't need to convert our R.id into a string. useStringType = false; break; } } } return useStringType; } return false; }
From source file:com.android.ide.eclipse.adt.internal.refactorings.extractstring.ReplaceStringsVisitor.java
License:Open Source License
/** * Find all method or fields that are candidates for providing a Context. * There can be various choices amongst this class or its super classes. * Sort them by rating in the results map. * * The best ever choice is to find a method with no argument that returns a Context. * The second suitable choice is to find a Context field. * The least desirable choice is to find a method with arguments. It's not really * desirable since we can't generate these arguments automatically. * * Methods and fields from supertypes are ignored if they are private. * * The rating is reversed: the lowest rating integer is used for the best candidate. * Because the superType argument is actually a recursion index, this makes the most * immediate classes more desirable.//www . j a v a2s.com * * @param results The map that accumulates the rating=>expression results. The lower * rating number is the best candidate. * @param clazzType The class examined. * @param superType The recursion index. * 0 for the immediate class, 1 for its super class, etc. */ private void findContextCandidates(TreeMap<Integer, Expression> results, ITypeBinding clazzType, int superType) { for (IMethodBinding mb : clazzType.getDeclaredMethods()) { // If we're looking at supertypes, we can't use private methods. if (superType != 0 && Modifier.isPrivate(mb.getModifiers())) { continue; } if (isAndroidContext(mb.getReturnType())) { // We found a method that returns something derived from Context. int argsLen = mb.getParameterTypes().length; if (argsLen == 0) { // We'll favor any method that takes no argument, // That would be the best candidate ever, so we can stop here. MethodInvocation mi = mAst.newMethodInvocation(); mi.setName(mAst.newSimpleName(mb.getName())); results.put(Integer.MIN_VALUE, mi); return; } else { // A method with arguments isn't as interesting since we wouldn't // know how to populate such arguments. We'll use it if there are // no other alternatives. We'll favor the one with the less arguments. Integer rating = Integer.valueOf(10000 + 1000 * superType + argsLen); if (!results.containsKey(rating)) { MethodInvocation mi = mAst.newMethodInvocation(); mi.setName(mAst.newSimpleName(mb.getName())); results.put(rating, mi); } } } } // A direct Context field would be more interesting than a method with // arguments. Try to find one. for (IVariableBinding var : clazzType.getDeclaredFields()) { // If we're looking at supertypes, we can't use private field. if (superType != 0 && Modifier.isPrivate(var.getModifiers())) { continue; } if (isAndroidContext(var.getType())) { // We found such a field. Let's use it. Integer rating = Integer.valueOf(superType); results.put(rating, mAst.newSimpleName(var.getName())); break; } } // Examine the super class to see if we can locate a better match clazzType = clazzType.getSuperclass(); if (clazzType != null) { findContextCandidates(results, clazzType, superType + 1); } }
From source file:com.android.ide.eclipse.auidt.internal.refactorings.extractstring.ReplaceStringsVisitor.java
License:Open Source License
/** * If the expression is part of a method invocation (aka a function call) or a * class instance creation (aka a "new SomeClass" constructor call), we try to * find the type of the argument being used. If it is a String (most likely), we * want to return true (to generate a getString() call). However if there might * be a similar method that takes an int, in which case we don't want to do that. * * This covers the case of Activity.setTitle(int resId) vs setTitle(String str). *//* w ww . ja va 2 s . co m*/ @SuppressWarnings("unchecked") private boolean examineMethodInvocation(StringLiteral node) { ASTNode parent = null; List arguments = null; IMethodBinding methodBinding = null; MethodInvocation invoke = findParentClass(node, MethodInvocation.class); if (invoke != null) { parent = invoke; arguments = invoke.arguments(); methodBinding = invoke.resolveMethodBinding(); } else { ClassInstanceCreation newclass = findParentClass(node, ClassInstanceCreation.class); if (newclass != null) { parent = newclass; arguments = newclass.arguments(); methodBinding = newclass.resolveConstructorBinding(); } } if (parent != null && arguments != null && methodBinding != null) { // We want to know which argument this is. // Walk up the hierarchy again to find the immediate child of the parent, // which should turn out to be one of the invocation arguments. ASTNode child = null; for (ASTNode n = node; n != parent;) { ASTNode p = n.getParent(); if (p == parent) { child = n; break; } n = p; } if (child == null) { // This can't happen: a parent of 'node' must be the child of 'parent'. return false; } // Find the index int index = 0; for (Object arg : arguments) { if (arg == child) { break; } index++; } if (index == arguments.size()) { // This can't happen: one of the arguments of 'invoke' must be 'child'. return false; } // Eventually we want to determine if the parameter is a string type, // in which case a Context.getString() call must be generated. boolean useStringType = false; // Find the type of that argument ITypeBinding[] types = methodBinding.getParameterTypes(); if (index < types.length) { ITypeBinding type = types[index]; useStringType = isJavaString(type); } // Now that we know that this method takes a String parameter, can we find // a variant that would accept an int for the same parameter position? if (useStringType) { String name = methodBinding.getName(); ITypeBinding clazz = methodBinding.getDeclaringClass(); nextMethod: for (IMethodBinding mb2 : clazz.getDeclaredMethods()) { if (methodBinding == mb2 || !mb2.getName().equals(name)) { continue; } // We found a method with the same name. We want the same parameters // except that the one at 'index' must be an int type. ITypeBinding[] types2 = mb2.getParameterTypes(); int len2 = types2.length; if (types.length == len2) { for (int i = 0; i < len2; i++) { if (i == index) { ITypeBinding type2 = types2[i]; if (!("int".equals(type2.getQualifiedName()))) { //$NON-NLS-1$ // The argument at 'index' is not an int. continue nextMethod; } } else if (!types[i].equals(types2[i])) { // One of the other arguments do not match our original method continue nextMethod; } } // If we got here, we found a perfect match: a method with the same // arguments except the one at 'index' is an int. In this case we // don't need to convert our R.id into a string. useStringType = false; break; } } } return useStringType; } return false; }
From source file:com.codenvy.ide.ext.java.server.JavadocFinder.java
License:Open Source License
private static IBinding resolveSuperclassConstructor(ITypeBinding superClassDeclaration, IMethodBinding constructor) {/*from w w w . ja v a 2s. c om*/ IMethodBinding[] methods = superClassDeclaration.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { IMethodBinding method = methods[i]; if (method.isConstructor() && constructor.isSubsignature(method)) return method; } return null; }
From source file:com.dnw.depmap.neo.NeoDao.java
License:Open Source License
/** * Creates a method with its override hierarchy. * //from w ww .ja v a 2 s . co m * @author manbaum * @since Oct 10, 2014 * @param method * @return */ public boolean createMethod(IMethodBinding method) { if (method == null) return false; if (isBlocked(method)) return false; ITypeBinding type = method.getDeclaringClass(); if (!createType(type)) return false; IMethodBinding declaration = method.getMethodDeclaration(); if (isCached(declaration)) return true; BindingCache.put(declaration, AstUtil.nameOf(declaration)); w.createMethod(declaration); w.createDeclare(declaration); if (type.isInterface()) { for (ITypeBinding t : type.getInterfaces()) { for (IMethodBinding m : t.getDeclaredMethods()) { if (method.overrides(m)) { IMethodBinding d = m.getMethodDeclaration(); if (createMethod(m)) w.createOverride(declaration, d); } } } } else { for (ITypeBinding t : type.getInterfaces()) { for (IMethodBinding m : t.getDeclaredMethods()) { if (method.overrides(m)) { IMethodBinding d = m.getMethodDeclaration(); if (createMethod(m)) w.createOverride(declaration, d); } } } ITypeBinding t = type.getSuperclass(); if (t != null) for (IMethodBinding m : t.getDeclaredMethods()) { if (method.overrides(m)) { IMethodBinding d = m.getMethodDeclaration(); if (createMethod(m)) w.createOverride(declaration, d); } } } return true; }
From source file:com.google.dart.java2dart.Context.java
License:Open Source License
public void ensureUniqueClassMemberNames(CompilationUnit unit) { unit.accept(new RecursiveASTVisitor<Void>() { private final Set<ClassMember> untouchableMethods = Sets.newHashSet(); private final Map<String, ClassMember> usedClassMembers = Maps.newHashMap(); private final Set<String> superNames = Sets.newHashSet(); private final Map<String, List<IMethodBinding>> superMembers = Maps.newHashMap(); @Override/*from w ww. j a va 2s . co m*/ public Void visitClassDeclaration(ClassDeclaration node) { untouchableMethods.clear(); usedClassMembers.clear(); superNames.clear(); superMembers.clear(); // fill "static" methods from super classes { org.eclipse.jdt.core.dom.ITypeBinding binding = getNodeTypeBinding(node); if (binding != null) { binding = binding.getSuperclass(); while (binding != null) { for (org.eclipse.jdt.core.dom.IMethodBinding method : binding.getDeclaredMethods()) { if (org.eclipse.jdt.core.dom.Modifier.isStatic(method.getModifiers())) { usedClassMembers.put(method.getName(), null); } else { addSuperMember(method); } } binding = binding.getSuperclass(); } } } // fill "untouchable" methods for (ClassMember member : node.getMembers()) { if (member instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) member; Object binding = nodeToBinding.get(member); if (JavaUtils.isMethodDeclaredInClass(binding, "java.lang.Object")) { untouchableMethods.add(member); usedClassMembers.put(methodDeclaration.getName().getName(), null); } } } // ensure unique method names (and prefer to keep method name over field name) for (ClassMember member : node.getMembers()) { if (member instanceof MethodDeclaration) { MethodDeclaration method = (MethodDeclaration) member; // untouchable if (untouchableMethods.contains(method)) { continue; } // getter/setter can share name { ClassMember otherMember = usedClassMembers.get(method.getName().getName()); if (otherMember instanceof MethodDeclaration) { MethodDeclaration otherMethod = (MethodDeclaration) otherMember; if (method.isGetter() && otherMethod.isSetter() || method.isSetter() && otherMethod.isGetter()) { continue; } } } // may be overloaded method { Object binding = nodeToBinding.get(method); if (binding instanceof IMethodBinding) { IMethodBinding methodBinding = (IMethodBinding) binding; String name = methodBinding.getName(); if (superNames.contains(name)) { IMethodBinding over = Bindings.findOverriddenMethod(methodBinding, false); if (over == null) { usedClassMembers.put(name, null); } } } } // ensure unique name ensureUniqueName(method.getName(), method); } } // ensure unique field names (if name is already used be method) for (ClassMember member : node.getMembers()) { if (member instanceof FieldDeclaration) { FieldDeclaration fieldDeclaration = (FieldDeclaration) member; for (VariableDeclaration field : fieldDeclaration.getFields().getVariables()) { ensureUniqueName(field.getName(), fieldDeclaration); } } } // no recursion return null; } private void addSuperMember(IMethodBinding binding) { String name = binding.getName(); superNames.add(name); List<IMethodBinding> members = superMembers.get(name); if (members == null) { members = Lists.newArrayList(); superMembers.put(name, members); } members.add(binding); } private void ensureUniqueName(Identifier declarationName, ClassMember member) { if (declarationName instanceof SimpleIdentifier) { SimpleIdentifier declarationIdentifier = (SimpleIdentifier) declarationName; String name = declarationIdentifier.getName(); if (!isUniqueClassMemberName(name)) { String newName = generateUniqueName(name); // rename binding if (!newName.equals(name)) { renameIdentifier(declarationIdentifier, newName); name = newName; } } // remember that name is used usedClassMembers.put(name, member); } } private String generateUniqueName(String name) { if (!isGloballyUniqueClassMemberName(name)) { int index = 2; while (true) { String newName = name + index; if (isGloballyUniqueClassMemberName(newName)) { usedNames.add(newName); return newName; } index++; } } return name; } private boolean isGloballyUniqueClassMemberName(String name) { return isUniqueClassMemberName(name) && !usedNames.contains(name); } private boolean isUniqueClassMemberName(String name) { return !forbiddenNames.contains(name) && !usedClassMembers.containsKey(name); } }); }