private static void checkHierarchyOfEnclosedTypes( IType destinationType, RefactoringStatus result, IType type) throws JavaModelException { IType[] enclosedTypes = getAllEnclosedTypes(type); for (int i = 0; i < enclosedTypes.length; i++) { IType enclosedType = enclosedTypes[i]; if (destinationType.getElementName().equals(enclosedType.getElementName())) { String message = Messages.format( RefactoringCoreMessages.MemberCheckUtil_type_name_conflict3, new String[] {getQualifiedLabel(enclosedType), getQualifiedLabel(type)}); RefactoringStatusContext context = JavaStatusContext.create( destinationType.getCompilationUnit(), destinationType.getNameRange()); result.addError(message, context); } if (typeNameExistsInEnclosingTypeChain(destinationType, enclosedType.getElementName())) { String message = Messages.format( RefactoringCoreMessages.MemberCheckUtil_type_name_conflict4, new String[] {getQualifiedLabel(enclosedType), getQualifiedLabel(type)}); RefactoringStatusContext context = JavaStatusContext.create( destinationType.getCompilationUnit(), destinationType.getNameRange()); result.addError(message, context); } } }
/** * Determines is the java element contains a type with a specific annotation. * * <p>The syntax for the property tester is of the form: qualified or unqualified annotation name * <li>qualified or unqualified annotation name, required. For example, <code>org.junit.JUnit * </code>. * </ol> * * @param element the element to check for the method * @param annotationName the qualified or unqualified name of the annotation to look for * @return true if the type is found in the element, false otherwise */ private boolean hasTypeWithAnnotation(IJavaElement element, String annotationType) { try { IType type = getType(element); if (type == null || !type.exists()) { return false; } IBuffer buffer = null; IOpenable openable = type.getOpenable(); if (openable instanceof ICompilationUnit) { buffer = ((ICompilationUnit) openable).getBuffer(); } else if (openable instanceof IClassFile) { buffer = ((IClassFile) openable).getBuffer(); } if (buffer == null) { return false; } ISourceRange sourceRange = type.getSourceRange(); ISourceRange nameRange = type.getNameRange(); if (sourceRange != null && nameRange != null) { IScanner scanner = ToolFactory.createScanner(false, false, true, false); scanner.setSource(buffer.getCharacters()); scanner.resetTo(sourceRange.getOffset(), nameRange.getOffset()); if (findAnnotation(scanner, annotationType)) { return true; } } } catch (JavaModelException e) { } catch (InvalidInputException e) { } return false; }
/** * Default strategy for when no source position can be computed: creates a {@link Position} for * the name of the given {@link IType}. Returns <code>null</code> in the event the given {@link * IType} is <code>null</code> or the name range cannot be computed for the type. * * @param type the type * @param reference the reference * @throws CoreException * @return returns a default {@link Position} for the name range of the given {@link IType} */ protected Position defaultSourcePosition(IType type, IReference reference) throws CoreException { if (type != null) { ISourceRange range = type.getNameRange(); if (range != null) { return new Position(range.getOffset(), range.getLength()); } } return null; }
private static void checkTypeInType(IType destinationType, RefactoringStatus result, IType type) throws JavaModelException { String typeName = type.getElementName(); IType destinationTypeType = destinationType.getType(typeName); if (destinationTypeType.exists()) { String message = Messages.format( RefactoringCoreMessages.MemberCheckUtil_type_name_conflict0, new String[] { BasicElementLabels.getJavaElementName(typeName), getQualifiedLabel(destinationType) }); RefactoringStatusContext context = JavaStatusContext.create( destinationType.getCompilationUnit(), destinationTypeType.getNameRange()); result.addError(message, context); } else { // need to check the hierarchy of enclosing and enclosed types if (destinationType.getElementName().equals(typeName)) { String message = Messages.format( RefactoringCoreMessages.MemberCheckUtil_type_name_conflict1, getQualifiedLabel(type)); RefactoringStatusContext context = JavaStatusContext.create( destinationType.getCompilationUnit(), destinationType.getNameRange()); result.addError(message, context); } if (typeNameExistsInEnclosingTypeChain(destinationType, typeName)) { String message = Messages.format( RefactoringCoreMessages.MemberCheckUtil_type_name_conflict2, getQualifiedLabel(type)); RefactoringStatusContext context = JavaStatusContext.create( destinationType.getCompilationUnit(), destinationType.getNameRange()); result.addError(message, context); } checkHierarchyOfEnclosedTypes(destinationType, result, type); } }
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException { RefactoringStatus result = new RefactoringStatus(); result.merge(Checks.checkAvailability(targetClass)); if (result.hasFatalError()) return result; fRoot = new RefactoringASTParser(AST.JLS3).parse(targetClass.getCompilationUnit(), true, pm); ISourceRange sourceRange = targetClass.getNameRange(); ASTNode node = NodeFinder.perform(fRoot, sourceRange.getOffset(), sourceRange.getLength()); if (node == null) { return mappingErrorFound(result, node); } else { targetClassDeclaration = ASTNodeSearchUtil.getTypeDeclarationNode(targetClass, fRoot); } fRewriter = ASTRewrite.create(fRoot.getAST()); return result; }
/* * (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; } }
public static int getLineNumber(IJavaElement element) { if (element != null && element instanceof IMethod) { try { IMethod method = (IMethod) element; int lines = 0; if (method.getDeclaringType() != null && method.getDeclaringType().getCompilationUnit() != null) { String targetsource = method.getDeclaringType().getCompilationUnit().getSource(); if (targetsource != null) { String sourceuptomethod = targetsource.substring(0, method.getNameRange().getOffset()); char[] chars = new char[sourceuptomethod.length()]; sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0); for (char element0 : chars) { if (element0 == '\n') { lines++; } } return new Integer(lines + 1); } } } catch (JavaModelException e) { } } else if (element != null && element instanceof IType && ((IType) element).getCompilationUnit() != null) { try { IType type = (IType) element; int lines = 0; String targetsource = type.getCompilationUnit().getSource(); if (targetsource != null) { String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset()); char[] chars = new char[sourceuptomethod.length()]; sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0); for (char element0 : chars) { if (element0 == '\n') { lines++; } } return new Integer(lines + 1); } } catch (JavaModelException e) { } } else if (element != null && element instanceof IField) { try { IField type = (IField) element; int lines = 0; ICompilationUnit cu = type.getCompilationUnit(); if (cu != null) { String targetsource = cu.getSource(); if (targetsource != null) { String sourceuptomethod = targetsource.substring(0, type.getNameRange().getOffset()); char[] chars = new char[sourceuptomethod.length()]; sourceuptomethod.getChars(0, sourceuptomethod.length(), chars, 0); for (char element0 : chars) { if (element0 == '\n') { lines++; } } return new Integer(lines + 1); } } } catch (JavaModelException e) { } } return new Integer(-1); }