List of usage examples for org.eclipse.jdt.core.dom MethodDeclaration delete
public final void delete()
From source file:ch.acanda.eclipse.pmd.java.resolution.unnecessary.UselessOverridingMethodQuickFix.java
License:Open Source License
/** * Removes the method./*from w w w . j a va 2 s. c om*/ */ @Override protected boolean apply(final MethodDeclaration node) { node.delete(); return true; }
From source file:net.sf.eclipsecs.ui.quickfixes.misc.UncommentedMainQuickfix.java
License:Open Source License
/** * {@inheritDoc}//from w w w .j a v a2s .c om */ protected ASTVisitor handleGetCorrectingASTVisitor(final IRegion lineInfo, final int markerStartOffset) { return new ASTVisitor() { public boolean visit(MethodDeclaration node) { // recalculate start position because optional javadoc is mixed // into the original start position int pos = node.getStartPosition() + (node.getJavadoc() != null ? node.getJavadoc().getLength() + JAVADOC_COMMENT_LENGTH : 0); if (containsPosition(lineInfo, pos) && node.getName().getFullyQualifiedName().equals("main")) //$NON-NLS-1$ { node.delete(); } return true; } }; }
From source file:org.mybatis.generator.eclipse.core.merge.ExistingJavaFileVisitor.java
License:Apache License
/** * Find the generated methods and delete them *//* w w w . j a v a 2 s. co m*/ @Override public boolean visit(MethodDeclaration node) { if (isGenerated(node)) { List<Annotation> annotations = retrieveAnnotations(node); if (!annotations.isEmpty()) { String methodSignature = EclipseDomUtils.getMethodSignature(node); methodAnnotations.put(methodSignature, annotations); } node.delete(); } return false; }
From source file:org.onehippo.cms7.essentials.dashboard.services.ContentBeansService.java
License:Apache License
private void updateImageMethod(final Path path, final String oldReturnType, final String newReturnType, final String importStatement) { final CompilationUnit deleteUnit = JavaSourceUtils.getCompilationUnit(path); final ExistingMethodsVisitor methodCollection = JavaSourceUtils.getMethodCollection(path); final List<EssentialsGeneratedMethod> generatedMethods = methodCollection.getGeneratedMethods(); final Map<String, EssentialsGeneratedMethod> deletedMethods = new HashMap<>(); deleteUnit.accept(new ASTVisitor() { @Override//from ww w . j av a2 s . com public boolean visit(MethodDeclaration node) { final String methodName = node.getName().getFullyQualifiedName(); final Type type = node.getReturnType2(); if (type.isSimpleType()) { final SimpleType simpleType = (SimpleType) type; final String returnTypeName = simpleType.getName().getFullyQualifiedName(); final EssentialsGeneratedMethod method = JavaSourceUtils.extractMethod(methodName, generatedMethods); if (method == null) { return super.visit(node); } if (returnTypeName.equals(oldReturnType)) { node.delete(); deletedMethods.put(method.getMethodName(), method); return super.visit(node); } } else if (JavaSourceUtils.getParameterizedType(type) != null) { final String returnTypeName = JavaSourceUtils.getParameterizedType(type); final EssentialsGeneratedMethod method = JavaSourceUtils.extractMethod(methodName, generatedMethods); if (method == null) { return super.visit(node); } if (returnTypeName.equals(oldReturnType)) { node.delete(); deletedMethods.put(method.getMethodName(), method); return super.visit(node); } log.info("oldReturnType {}", oldReturnType); } return super.visit(node); } }); if (deletedMethods.size() > 0) { final AST deleteAst = deleteUnit.getAST(); final String deletedSource = JavaSourceUtils.rewrite(deleteUnit, deleteAst); GlobalUtils.writeToFile(deletedSource, path); for (Map.Entry<String, EssentialsGeneratedMethod> entry : deletedMethods.entrySet()) { final EssentialsGeneratedMethod oldMethod = entry.getValue(); // Add replacement methods: if (newReturnType.equals(HIPPO_GALLERY_IMAGE_SET_CLASS) || newReturnType.equals(HIPPO_GALLERY_IMAGE_SET_BEAN)) { JavaSourceUtils.addBeanMethodHippoImageSet(path, oldMethod.getMethodName(), oldMethod.getInternalName(), oldMethod.isMultiType()); } else { JavaSourceUtils.addBeanMethodInternalImageSet(path, newReturnType, importStatement, oldMethod.getMethodName(), oldMethod.getInternalName(), oldMethod.isMultiType()); } log.debug("Replaced old method: {} with new return type: {}", oldMethod.getMethodName(), newReturnType); context.addPluginContextData(CONTEXT_BEAN_DATA, new BeanWriterLogEntry(path.toString(), oldMethod.getMethodName(), ActionType.MODIFIED_METHOD)); } } }
From source file:org.onehippo.cms7.essentials.dashboard.utils.JavaSourceUtils.java
License:Apache License
public static boolean deleteMethod(final EssentialsGeneratedMethod method, final Path path) { final CompilationUnit deleteUnit = getCompilationUnit(path); final ExistingMethodsVisitor methodCollection = JavaSourceUtils.getMethodCollection(path); final List<EssentialsGeneratedMethod> generatedMethods = methodCollection.getGeneratedMethods(); final Map<String, EssentialsGeneratedMethod> deletedMethods = new HashMap<>(); final String oldReturnType = getReturnType(method.getReturnType()); deleteUnit.accept(new ASTVisitor() { @Override//www. ja v a2 s . c o m public boolean visit(MethodDeclaration node) { final String methodName = node.getName().getFullyQualifiedName(); final Type type = node.getReturnType2(); final String returnTypeName = getReturnType(type); if (returnTypeName != null) { final EssentialsGeneratedMethod method = extractMethod(methodName, generatedMethods); if (method == null) { return super.visit(node); } if (returnTypeName.equals(oldReturnType)) { node.delete(); deletedMethods.put(method.getMethodName(), method); return super.visit(node); } } return super.visit(node); } }); final int deletedSize = deletedMethods.size(); if (deletedSize > 0) { // rewrite source: final AST deleteAst = deleteUnit.getAST(); final String deletedSource = JavaSourceUtils.rewrite(deleteUnit, deleteAst); GlobalUtils.writeToFile(deletedSource, path); } return deletedSize > 0; }
From source file:sourcecodefilter.ExitCode.java
License:Open Source License
@SuppressWarnings("unchecked") private static void removeNonSerializedFieldsAndUnusedMethodsIn(ConverterRelevantCatroidSource source) { final List<AbstractTypeDeclaration> types = source.getSourceAst().types(); assert types.size() > 0; // TODO: add abstraction for field and method iteration for (AbstractTypeDeclaration abstractTypeDecl : types) { Set<FieldDeclaration> nonTransientFields = new HashSet<FieldDeclaration>(); // using AbstractTypeDeclaration to cover regular Types and Enums for (BodyDeclaration bodyDecl : new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) { if (bodyDecl.getNodeType() == ASTNode.FIELD_DECLARATION) { FieldDeclaration fieldDecl = (FieldDeclaration) bodyDecl; assert fieldDecl.fragments().size() == 1 : String .format("Unsupported multi field declaration: '%s'", fieldDecl.toString()); String fieldName = ((VariableDeclarationFragment) fieldDecl.fragments().get(0)).getName() .getIdentifier(); if (source.isRemovedField(fieldName)) { fieldDecl.delete();// w w w. j a v a 2 s . c o m } else if (Modifier.isTransient(fieldDecl.getModifiers()) && !(source.isPreservedField(fieldName))) { fieldDecl.delete(); } else { nonTransientFields.add(fieldDecl); } } } for (BodyDeclaration bodyDecl : new ArrayList<BodyDeclaration>(abstractTypeDecl.bodyDeclarations())) { if (bodyDecl.getNodeType() == ASTNode.METHOD_DECLARATION) { MethodDeclaration methodDeclaration = (MethodDeclaration) bodyDecl; if (!methodDeclaration.isConstructor()) { // removeOverrideAnnotation(methodDeclaration); final String methodName = methodDeclaration.getName().getIdentifier(); if (methodName.equals("init")) { Block body = methodDeclaration.getBody(); for (Iterator<Statement> iterator2 = body.statements().iterator(); iterator2 .hasNext();) { iterator2.next(); iterator2.remove(); } } else if (source.isRemovedMethod(methodName)) { methodDeclaration.delete(); } else if (!(source.isPreservedMethod(methodName))) { if (!(isRelatedToNonTransientFields(methodName, nonTransientFields)) || isOverriding(methodDeclaration)) { methodDeclaration.delete(); } } } } } } }