/** * Constructs a compilation unit relative name for the supplied type. * * <p>If the type is imported, in java.lang, or in the same package as the source file, then the * type name returned is unqualified, otherwise the name returned is the fully qualified type * name. * * @param src The compilation unit. * @param type The type. * @return The relative type name. */ public static String getCompilationUnitRelativeTypeName(ICompilationUnit src, IType type) throws Exception { String typeName = type.getFullyQualifiedName().replace('$', '.'); if (JavaUtils.containsImport(src, type)) { typeName = type.getElementName(); int parentType = type.getParent().getElementType(); if (parentType == IJavaElement.TYPE) { typeName = type.getParent().getElementName() + '.' + typeName; } else if (parentType == IJavaElement.CLASS_FILE) { String parentName = type.getParent().getElementName(); int index = parentName.indexOf('$'); if (index != -1) { parentName = parentName.substring(0, index); typeName = parentName + '.' + typeName; } } } else { typeName = type.getFullyQualifiedName().replace('$', '.'); } return typeName; }
private boolean isInnerType(IJavaElement element) { if (element != null && element.getElementType() == IJavaElement.TYPE) { IType type = (IType) element; try { return type.isMember(); } catch (JavaModelException e) { IJavaElement parent = type.getParent(); if (parent != null) { int parentElementType = parent.getElementType(); return (parentElementType != IJavaElement.COMPILATION_UNIT && parentElementType != IJavaElement.CLASS_FILE); } } } return false; }
@Override public char[] findSource(IType type, IBinaryType info) { IClassFile classFile = (IClassFile) type.getParent(); addDecompiled(this, classFile); return super.findSource(type, info); }
private JavaElement getUnresolvedJavaElement() { IType declaringType = (IType) getDeclaringClass().getJavaElement(); if (declaringType == null) return null; if (!(this.resolver instanceof DefaultBindingResolver)) return null; ASTNode node = (ASTNode) ((DefaultBindingResolver) this.resolver).bindingsToAstNodes.get(this); if (node != null && declaringType.getParent().getElementType() != IJavaElement.CLASS_FILE) { if (node instanceof MethodDeclaration) { MethodDeclaration methodDeclaration = (MethodDeclaration) node; ArrayList parameterSignatures = new ArrayList(); Iterator iterator = methodDeclaration.parameters().iterator(); while (iterator.hasNext()) { SingleVariableDeclaration parameter = (SingleVariableDeclaration) iterator.next(); Type type = parameter.getType(); String typeSig = Util.getSignature(type); int arrayDim = parameter.getExtraDimensions(); if (parameter.getAST().apiLevel() >= AST.JLS3 && parameter.isVarargs()) { arrayDim++; } if (arrayDim > 0) { typeSig = Signature.createArraySignature(typeSig, arrayDim); } parameterSignatures.add(typeSig); } int parameterCount = parameterSignatures.size(); String[] parameters = new String[parameterCount]; parameterSignatures.toArray(parameters); return (JavaElement) declaringType.getMethod(getName(), parameters); } else { // annotation type member declaration AnnotationTypeMemberDeclaration typeMemberDeclaration = (AnnotationTypeMemberDeclaration) node; return (JavaElement) declaringType.getMethod( typeMemberDeclaration.getName().getIdentifier(), new String[0]); // annotation type members don't have parameters } } else { // case of method not in the created AST, or a binary method org.eclipse.jdt.internal.compiler.lookup.MethodBinding original = this.binding.original(); String selector = original.isConstructor() ? declaringType.getElementName() : new String(original.selector); TypeBinding[] parameters = original.parameters; int length = parameters == null ? 0 : parameters.length; String[] parameterSignatures = new String[length]; for (int i = 0; i < length; i++) { parameterSignatures[i] = new String(parameters[i].genericTypeSignature()).replace('/', '.'); } IMethod result = declaringType.getMethod(selector, parameterSignatures); if (declaringType.isBinary()) return (JavaElement) result; IMethod[] methods = null; try { methods = declaringType.getMethods(); } catch (JavaModelException e) { // declaring type doesn't exist return null; } IMethod[] candidates = Member.findMethods(result, methods); if (candidates == null || candidates.length == 0) return null; return (JavaElement) candidates[0]; } }