/** * Returns the signature to display for found problems * * @param member the member to get the signature from * @param qualified if the returned signature should be type-qualified or not * @return * @throws CoreException */ private String getDisplay(IApiMember member, boolean qualified) throws CoreException { String typeName = qualified ? getTypeName(member) : getSimpleTypeName(member); if (typeName.indexOf('$') != -1) { typeName = typeName.replace('$', '.'); } switch (member.getType()) { case IApiElement.FIELD: { StringBuffer buffer = new StringBuffer(); IApiField field = (IApiField) member; buffer.append(typeName).append('.').append(field.getName()); return String.valueOf(buffer); } case IApiElement.METHOD: { // reference in a method declaration IApiMethod method = (IApiMethod) member; if (qualified) { return Signatures.getMethodSignature(method); } return Signatures.getQualifiedMethodSignature(method); } default: break; } return typeName; }
/* * (non-Javadoc) * @see org.eclipse.pde.api.tools.internal.builder.AbstractProblemDetector# * getElementType * (org.eclipse.pde.api.tools.internal.provisional.builder.IReference) */ @Override protected int getElementType(IReference reference) { IApiMember member = reference.getMember(); switch (member.getType()) { case IApiElement.TYPE: return IElementDescriptor.TYPE; case IApiElement.METHOD: return IElementDescriptor.METHOD; case IApiElement.FIELD: return IElementDescriptor.FIELD; default: return 0; } }
/** * Returns whether all enclosing types of the given member are visible. * * @param member member * @return whether all enclosing types of the given member are visible * @throws CoreException */ protected boolean isEnclosingTypeVisible(IApiMember member) throws CoreException { IApiType type = null; if (member.getType() == IApiElement.TYPE) { type = (IApiType) member; } else { type = member.getEnclosingType(); } while (type != null) { if (((Flags.AccPublic | Flags.AccProtected) & type.getModifiers()) == 0) { // the type is private or default protection, do not retain the reference return false; } type = type.getEnclosingType(); } return true; }
/** * Returns the fully qualified type name associated with the given member. * * @param member * @return fully qualified type name */ protected String getTypeName(IApiMember member) throws CoreException { switch (member.getType()) { case IApiElement.TYPE: { IApiType type = (IApiType) member; if (type.isAnonymous()) { return getTypeName(member.getEnclosingType()); } else if (type.isLocal()) { return getTypeName(member.getEnclosingType()); } return member.getName(); } default: { return getTypeName(member.getEnclosingType()); } } }
/** * Returns whether the referenced type name matches a non-API package. * * @param reference * @return whether the referenced type name matches a non-API package */ protected boolean isNonAPIReference(IReference reference) { String packageName = Signatures.getPackageName(reference.getReferencedTypeName()); if (fNonApiPackageNames.contains(packageName)) { return true; } // could be a reference to a package visible type IApiMember member = reference.getMember(); IApiType type = null; if (member.getType() == IApiElement.TYPE) { type = (IApiType) member; } else { type = (IApiType) member.getAncestor(IApiElement.TYPE); } String origin = Signatures.getPackageName(type.getName()); if (packageName.equals(origin)) { return true; // possible package visible reference } return false; }
/** * Returns the unqualified type name associated with the given member. * * @param member * @return unqualified type name */ protected String getSimpleTypeName(IApiMember member) throws CoreException { switch (member.getType()) { case IApiElement.TYPE: { IApiType type = (IApiType) member; if (type.isAnonymous()) { return getSimpleTypeName(type.getEnclosingType()); } else if (type.isLocal()) { String name = getSimpleTypeName(member.getEnclosingType()); int idx = name.indexOf('$'); if (idx > -1) { return name.substring(0, idx); } return name; } return Signatures.getTypeName(Signatures.getTypeSignature(type)); } default: return getSimpleTypeName(member.getEnclosingType()); } }
/* * (non-Javadoc) * @see org.eclipse.pde.api.tools.internal.builder.AbstractProblemDetector# * getSourceRange(org.eclipse.jdt.core.IType, * org.eclipse.jface.text.IDocument, * org.eclipse.pde.api.tools.internal.provisional.builder.IReference) */ @Override protected Position getSourceRange(IType type, IDocument document, IReference reference) throws CoreException, BadLocationException { switch (reference.getReferenceType()) { case IReference.T_TYPE_REFERENCE: { int linenumber = reference.getLineNumber(); if (linenumber > 0) { // line number starts at 0 for the linenumber--; } if (linenumber > 0) { int offset = document.getLineOffset(linenumber); String line = document.get(offset, document.getLineLength(linenumber)); String qname = reference.getReferencedTypeName().replace('$', '.'); int first = line.indexOf(qname); if (first < 0) { qname = Signatures.getSimpleTypeName(reference.getReferencedTypeName()); qname = qname.replace('$', '.'); first = line.indexOf(qname); } Position pos = null; if (first > -1) { pos = new Position(offset + first, qname.length()); } else { // optimistically select the whole line since we can't // find the correct variable name and we can't just // select // the first occurrence pos = new Position(offset, line.length()); } return pos; } else { IApiMember apiMember = reference.getMember(); switch (apiMember.getType()) { case IApiElement.FIELD: { IApiField field = (IApiField) reference.getMember(); return getSourceRangeForField(type, reference, field); } case IApiElement.METHOD: { // reference in a method declaration IApiMethod method = (IApiMethod) reference.getMember(); return getSourceRangeForMethod(type, reference, method); } default: break; } // reference in a type declaration ISourceRange range = type.getNameRange(); Position pos = null; if (range != null) { pos = new Position(range.getOffset(), range.getLength()); } if (pos == null) { return defaultSourcePosition(type, reference); } return pos; } } case IReference.T_FIELD_REFERENCE: { int linenumber = reference.getLineNumber(); if (linenumber > 0) { return getFieldNameRange( reference.getReferencedTypeName(), reference.getReferencedMemberName(), document, reference); } // reference in a field declaration IApiField field = (IApiField) reference.getMember(); return getSourceRangeForField(type, reference, field); } case IReference.T_METHOD_REFERENCE: { if (reference.getLineNumber() >= 0) { String referenceMemberName = reference.getReferencedMemberName(); String methodName = null; boolean isConstructor = Util.isConstructor(referenceMemberName); if (isConstructor) { methodName = Signatures.getSimpleTypeName(reference.getReferencedTypeName().replace('$', '.')); } else { methodName = referenceMemberName; } Position pos = getMethodNameRange(isConstructor, methodName, document, reference); if (pos == null) { return defaultSourcePosition(type, reference); } return pos; } // reference in a method declaration IApiMethod method = (IApiMethod) reference.getMember(); return getSourceRangeForMethod(type, reference, method); } default: return null; } }