/** * Adds the specified interface to the implements clause of * {@link #getClassTree()}. * * @param classTree the class to add the implements clause to. * @param interfaceType the fully-qualified name of the interface; cannot be null. * @return the class implementing the new interface. */ public ClassTree addImplementsClause(ClassTree classTree, String interfaceType) { Parameters.notNull("classTree", classTree); // NOI18N Parameters.notNull("interfaceType", interfaceType); // NOI18N ExpressionTree interfaceTree = createQualIdent(interfaceType); return getTreeMaker().addClassImplementsClause(classTree, interfaceTree); }
ClassTree javaClass = SourceUtils.getPublicTopLevelTree(workingCopy); IdentifierTree id = make.Identifier("javax.ejb.SessionBean"); ClassTree modifiedClass = make.addClassImplementsClause(javaClass, id);
public void run (WorkingCopy workingCopy) throws Exception { workingCopy.toPhase (JavaSource.Phase.ELEMENTS_RESOLVED); CompilationUnitTree compilationUnit = workingCopy.getCompilationUnit (); TreeMaker treeMaker = workingCopy.getTreeMaker (); Trees trees = workingCopy.getTrees (); ContainsMethodTreeVisitor visitor = new ContainsMethodTreeVisitor (trees, methodName, parameters); visitor.scan (compilationUnit, null); TreePath classTreePath = visitor.getClassTreePath (); if (classTreePath != null) { ExpressionTree expressionTree = findImplementIdentifier (trees, classTreePath, className); if (visitor.isMethodExists ()) { if (expressionTree == null) { TypeElement typeElement = workingCopy.getElements ().getTypeElement (className); ExpressionTree implementsClause = typeElement != null ? treeMaker.QualIdent (typeElement) : treeMaker.Identifier (className); ClassTree oldClassTree = (ClassTree) classTreePath.getLeaf (); ClassTree newClassTree = treeMaker.addClassImplementsClause (oldClassTree, implementsClause); workingCopy.rewrite (oldClassTree, newClassTree); } } else { if (expressionTree != null) { ClassTree oldClassTree = (ClassTree) classTreePath.getLeaf (); ClassTree newClassTree = treeMaker.removeClassImplementsClause (oldClassTree, expressionTree); workingCopy.rewrite (oldClassTree, newClassTree); } } } } });