public static IMethod[] removeGeneratedMethods(IMethod[] methods) throws Exception { List<IMethod> result = new ArrayList<IMethod>(); for (IMethod m : methods) { if (m.getNameRange().getLength() > 0 && !m.getNameRange().equals(m.getSourceRange())) result.add(m); } return result.size() == methods.length ? methods : result.toArray(new IMethod[result.size()]); }
public static org.eclipse.jdt.core.dom.AbstractTypeDeclaration findTypeDeclaration(IType searchType, List<?> nodes) { for (Object object : nodes) { if (object instanceof org.eclipse.jdt.core.dom.AbstractTypeDeclaration) { org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = (org.eclipse.jdt.core.dom.AbstractTypeDeclaration) object; if (typeDeclaration.getName().toString().equals(searchType.getElementName())) return typeDeclaration; } } return null; }
/** * Creates a JavaCodeFormatter using the default formatter options and * optionally applying user provided options on top. * * @param overrideOptions user provided options to apply on top of defaults */ public JavaCodeFormatter(final Map<String, Object> overrideOptions) { Map formatterOptions = new HashMap<>(DEFAULT_FORMATTER_OPTIONS); if (overrideOptions != null) { formatterOptions.putAll(overrideOptions); } this.codeFormatter = ToolFactory.createCodeFormatter(formatterOptions, ToolFactory.M_FORMAT_EXISTING); }
public static org.eclipse.jdt.core.dom.MethodDeclaration getRealMethodDeclarationNode(org.eclipse.jdt.core.IMethod sourceMethod, org.eclipse.jdt.core.dom.CompilationUnit cuUnit) throws JavaModelException { MethodDeclaration methodDeclarationNode = ASTNodeSearchUtil.getMethodDeclarationNode(sourceMethod, cuUnit); if (isGenerated(methodDeclarationNode)) { IType declaringType = sourceMethod.getDeclaringType(); Stack<IType> typeStack = new Stack<IType>(); while (declaringType != null) { typeStack.push(declaringType); declaringType = declaringType.getDeclaringType(); org.eclipse.jdt.core.dom.AbstractTypeDeclaration typeDeclaration = findTypeDeclaration(rootType, cuUnit.types()); while (!typeStack.isEmpty() && typeDeclaration != null) { typeDeclaration = findTypeDeclaration(typeStack.pop(), typeDeclaration.bodyDeclarations()); String methodName = sourceMethod.getElementName(); for (Object declaration : typeDeclaration.bodyDeclarations()) { if (declaration instanceof org.eclipse.jdt.core.dom.MethodDeclaration) { org.eclipse.jdt.core.dom.MethodDeclaration methodDeclaration = (org.eclipse.jdt.core.dom.MethodDeclaration) declaration; if (methodDeclaration.getName().toString().equals(methodName)) { return methodDeclaration;
@Override protected IResource getModifiedResource() { IJavaElement elem= JavaCore.create(fHandle); if (elem != null) { return elem.getResource(); } return null; }
@Override public String getSimpleName() { IType declaringType = this.method.getDeclaringType(); return declaringType.getElementName() + "#" + this.method.getElementName(); }
@Override public IProject[] getProjects() { return new IProject[] {fUnit.getJavaProject().getProject() }; } @Override
/** * Returns the element at the given position in the given type */ protected static IJavaElement getElementAt(IType type, int pos) throws JavaModelException { if (type.isBinary()) { return type.getClassFile().getElementAt(pos); } return type.getCompilationUnit().getElementAt(pos); }
@Override public IProject[] getProjects() { return new IProject[] { fClassFile.getJavaProject().getProject() }; } @Override
private boolean accept(int offset, IMember member) throws JavaModelException { ISourceRange nameRange = member.getNameRange(); if (nameRange == null) { return false; } int srcOffset = nameRange.getOffset(); return srcOffset > offset; }
private static boolean isNestedInterfaceOrAnnotation(IMember member) throws JavaModelException{ return member.getElementType() == IJavaElement.TYPE && member.getDeclaringType() != null && ((IType) member).isInterface(); }
private boolean isDeprecated(IJavaElement element) throws JavaModelException { if (element instanceof ITypeRoot) { return Flags.isDeprecated(((ITypeRoot) element).findPrimaryType().getFlags()); } return false; }
CompilationUnitReorgChange(ICompilationUnit cu, IPackageFragment dest, INewNameQuery newNameQuery) { fCuHandle= cu.getHandleIdentifier(); fNewPackageHandle= dest.getHandleIdentifier(); fNewNameQuery= newNameQuery; fOldPackageHandle= cu.getParent().getHandleIdentifier(); }
public IJavaProject getProject() { if (fSelectionCompilationUnit != null) return fSelectionCompilationUnit.getJavaProject(); if (fSelectionClassFile != null) return fSelectionClassFile.getJavaProject(); if (fTargetMethod != null) return fTargetMethod.getJavaProject(); return null; }
in = annotatable.getAnnotations(); } catch (Exception e) { return out; boolean found = false; for (IAnnotation i : in) { String name = i.getElementName(); int li = name.lastIndexOf('.'); if (li > -1) name = name.substring(li + 1);
public static SearchMatch[] removeGenerated(SearchMatch[] returnValue) { List<SearchMatch> result = new ArrayList<SearchMatch>(); for (int j = 0; j < returnValue.length; j++) { SearchMatch searchResult = returnValue[j]; if (searchResult.getElement() instanceof IField) { IField field = (IField) searchResult.getElement(); // can not check for value=lombok because annotation is // not fully resolved IAnnotation annotation = field.getAnnotation("Generated"); if (annotation != null) { // Method generated at field location, skip continue; } } result.add(searchResult); } return result.toArray(new SearchMatch[result.size()]); }
ICompilationUnit cu = ... // an ICompilationUnit to rename RefactoringContribution contribution = RefactoringCore.getRefactoringContribution(IJavaRefactorings .RENAME_COMPILATION_UNIT); RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) contribution.createDescriptor(); descriptor.setProject(cu.getResource().getProject().getName( )); descriptor.setNewName("NewClass"); // new name for a Class descriptor.setJavaElement(cu); RefactoringStatus status = new RefactoringStatus(); try { Refactoring refactoring = descriptor.createRefactoring(status); IProgressMonitor monitor = new NullProgressMonitor(); refactoring.checkInitialConditions(monitor); refactoring.checkFinalConditions(monitor); Change change = refactoring.createChange(monitor); change.perform(monitor); } catch (CoreException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }
public static boolean isSourceAvailable(ISourceReference sourceReference) { try { return SourceRange.isAvailable(sourceReference.getSourceRange()); } catch (JavaModelException e) { return false; } } }
public static SearchResultGroup[] createFakeSearchResult(SearchResultGroup[] returnValue, Object/* * org.eclipse.jdt.internal.corext.refactoring.rename. * RenameFieldProcessor */ processor) throws Exception { if (returnValue == null || returnValue.length == 0) { // if no matches were found, check if Data annotation is present on the class Field declaredField = processor.getClass().getDeclaredField("fField"); if (declaredField != null) { declaredField.setAccessible(true); SourceField fField = (SourceField) declaredField.get(processor); IAnnotation dataAnnotation = fField.getDeclaringType().getAnnotation("Data"); if (dataAnnotation != null) { // add fake item, to make refactoring checks pass return new SearchResultGroup[] {new SearchResultGroup(null, new SearchMatch[1])}; } } } return returnValue; }
@Override public int compare(IMember o1, IMember o2){ try{ return o1.getNameRange().getOffset() - o2.getNameRange().getOffset(); } catch (JavaModelException e){ return 0; } } };