@Override public void run(WorkingCopy copy) throws IOException { copy.toPhase(JavaSource.Phase.RESOLVED); TreeMaker maker = copy.getTreeMaker(); ClassTree tree = getTopLevelClassTree(copy); if ( tree ==null ){ return; } Element element = copy.getTrees().getElement( copy.getTrees().getPath(copy.getCompilationUnit(), tree) ); ModifiersTree modifiers = tree.getModifiers(); modifiers = addAnnotation(INTERCEPTOR, maker, modifiers); TypeElement annotation = handle.resolve( copy ); if ( annotation != null ){ modifiers = addAnnotation(annotation.getQualifiedName().toString(), maker, modifiers); } copy.rewrite(tree.getModifiers(), modifiers); ElementOpen.open(copy.getClasspathInfo(), element); }
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); } } } } });
private TreePath getParentPath(TreePath tp, Tree t) { Tree parent; if (tp != null) { while (tp.getLeaf().getKind() != Kind.COMPILATION_UNIT && getTreeUtilities().isSynthetic(tp)) { tp = tp.getParentPath(); } parent = tp.getLeaf(); } else { parent = t; } TreePath c = tree2Path.get(parent); if (c == null) { c = tp != null ? tp : new TreePath((CompilationUnitTree) t); tree2Path.put(parent, c); } return c; }
private BlockTree addStatement(WorkingCopy wc, BlockTree blockTree, StatementTree stmtTree) { BlockTree newBlockTree = wc.getTreeMaker().addBlockStatement(blockTree, stmtTree); wc.rewrite(blockTree, newBlockTree); return newBlockTree; }
/** * Add a new import * */ private void addImport(WorkingCopy wc, String fqn) { ImportTree imprt = TreeMakerUtils.createImport(wc, fqn); CompilationUnitTree cunit = wc.getTreeMaker().addCompUnitImport(wc.getCompilationUnit(), imprt); wc.rewrite(wc.getCompilationUnit(), cunit); }
public Object run(WorkingCopy wc) { TreeMaker make = wc.getTreeMaker(); ExpressionTree arg = getArgument(wc); SourcePositions[] positions = new SourcePositions[1]; ExpressionTree newArg = wc.getTreeUtilities().parseExpression(valueSource, positions); wc.rewrite(arg, newArg); return null; } }, method.getJavaClass().getFileObject());
private Element getImportedElement(CompilationUnitTree cut, ImportTree imp) { Trees trees = copy.getTrees(); Tree qualIdent = imp.getQualifiedIdentifier(); if (qualIdent.getKind() != Tree.Kind.MEMBER_SELECT) { Element element = trees.getElement(TreePath.getPath(cut, qualIdent)); if (element == null) { String fqn = qualIdent.toString(); Element element = trees.getElement(TreePath.getPath(cut, ((MemberSelectTree)qualIdent).getExpression())); if (element == null) element = getElementByFQN(((MemberSelectTree)qualIdent).getExpression().toString()); Element parent = trees.getElement(TreePath.getPath(cut, ((MemberSelectTree)qualIdent).getExpression())); if (parent == null) parent = getElementByFQN(((MemberSelectTree)qualIdent).getExpression().toString()); if (parent != null && (parent.getKind().isClass() || parent.getKind().isInterface())) { Scope s = trees.getScope(new TreePath(cut)); for (Element e : parent.getEnclosedElements()) { if (name == e.getSimpleName() && e.getModifiers().contains(Modifier.STATIC) && trees.isAccessible(s, e, (DeclaredType)parent.asType())) return e;
private ExpressionTree qualIdentFor(Element e) { TreeMaker tm = copy.getTreeMaker(); if (e.getKind() == ElementKind.PACKAGE) { String name = ((PackageElement)e).getQualifiedName().toString(); if (e instanceof Symbol) { int lastDot = name.lastIndexOf('.'); if (lastDot < 0) return tm.Identifier(e); return tm.MemberSelect(qualIdentFor(name.substring(0, lastDot)), e); } return qualIdentFor(name); } Element ee = e.getEnclosingElement(); if (e instanceof Symbol) return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e) : tm.Identifier(e); return ee.getSimpleName().length() > 0 ? tm.MemberSelect(qualIdentFor(ee), e.getSimpleName()) : tm.Identifier(e.getSimpleName()); }
continue; Trees trees = copy.getTrees(); Elements elements = copy.getElements(); ElementUtilities elementUtilities = copy.getElementUtilities(); pkg = packageName != null ? (PackageElement)trees.getElement(TreePath.getPath(cut, packageName)) : null; if (pkg == null && packageName != null) { pkg = elements.getPackageElement(elements.getName(packageName.toString())); TreeMaker make = copy.getTreeMaker(); int currentToImport = elementsToImport.size() - 1; int currentExisting = imports.size() - 1; ExpressionTree qualIdent = qualIdentFor(currentToImportElement); if (isStar) { qualIdent = make.MemberSelect(qualIdent, elements.getName("*")); //NOI18N ImportTree nImport = make.Import(qualIdent, isStatic); while (currentExisting >= 0) { ImportTree imp = imports.get(currentExisting); return make.CompilationUnit(cut.getPackage(), imports, cut.getTypeDecls(), cut.getSourceFile());
final Scope scope = trees.getScope(context); String qName = fqn; StringBuilder sqName = new StringBuilder(); if (simple.contentEquals(e.getSimpleName())) { final Set<Element> elementsToImport = Collections.singleton(toImport); if (info instanceof WorkingCopy) { CompilationUnitTree nue = (CompilationUnitTree) ((WorkingCopy)info).resolveRewriteTarget(cut); ((WorkingCopy)info).rewrite(info.getCompilationUnit(), GeneratorUtilities.get((WorkingCopy)info).addImports(nue, elementsToImport)); } else { final ElementHandle handle = ElementHandle.create(toImport); if (toImport.getKind() == ElementKind.PACKAGE) { StarImportScope importScope = new StarImportScope(unit.packge); importScope.prependSubScope(unit.starImportScope);
public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); gu = GenerationUtils.newInstance(workingCopy); for (Tree typeDecl : cut.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { Element e = workingCopy.getTrees().getElement(new TreePath(new TreePath(workingCopy.getCompilationUnit()), typeDecl)); if (e != null && e.getKind().isClass()) { TypeElement te = (TypeElement) e; ClassTree ct = (ClassTree) typeDecl; workingCopy.rewrite(ct, generateInterfaces(workingCopy, te, ct, gu)); } } } } };
private MethodTree createMethod(final ExecutableElement element, final TypeElement clazz, final boolean isImplement) { final TreeMaker make = copy.getTreeMaker(); MethodTree prototype = createMethod((DeclaredType)clazz.asType(), element); ModifiersTree mt = prototype.getModifiers(); if (copy.getSourceVersion().compareTo(SourceVersion.RELEASE_5) >= 0) { boolean generate = true; if (copy.getSourceVersion().compareTo(SourceVersion.RELEASE_5) == 0) { generate = !element.getEnclosingElement().getKind().isInterface(); mt = make.addModifiersAnnotation(prototype.getModifiers(), make.Annotation(make.Identifier("Override"), Collections.<ExpressionTree>emptyList())); if (method.getBody() != null) { if (containsErrors(method.getBody())) { copy.rewrite(method.getBody(), make.Block(Collections.<StatementTree>emptyList(), false)); } else { Trees trees = copy.getTrees(); TreePath path = trees.getPath(clazz); if (path == null) { path = new TreePath(copy.getCompilationUnit()); Scope s = trees.getScope(path); BlockTree body = method.getBody(); copy.getTreeUtilities().attributeTree(body, s); body = importFQNs(body); copy.rewrite(method.getBody(), body);
/** * Creates a setter method for a field. * * @param clazz the class to create the setter within * @param field field to create setter for * @return the setter method * @since 0.20 */ public MethodTree createSetter(TypeElement clazz, VariableElement field) { assert clazz != null && field != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC); boolean isStatic = field.getModifiers().contains(Modifier.STATIC); if (isStatic) { mods.add(Modifier.STATIC); } CharSequence name = field.getSimpleName(); assert name.length() > 0; TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), field); String setterName = CodeStyleUtils.computeSetterName(field.getSimpleName(), isStatic, cs); String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(field, cs), cs); List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, make.Type(type), null)); BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(field.getEnclosingElement().getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), body, null); }
TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = method.getModifiers(); et = (ExecutableType) copy.getTypes().asMemberOf(asMemberOf, method); } catch (IllegalArgumentException iae) { ClassSymbol boundSymbol = (ClassSymbol) ((DeclaredType) bound).asElement(); if (boundSymbol.getSimpleName().length() == 0 && (boundSymbol.flags() & Flags.COMPOUND) != 0) { bounds.add((ExpressionTree) make.Type(boundSymbol.getSuperclass())); for (Type iface : boundSymbol.getInterfaces()) { bounds.add((ExpressionTree) make.Type(iface)); bounds.add((ExpressionTree) make.Type(bound)); typeParams.add(make.TypeParameter(typeVariable.asElement().getSimpleName(), bounds));
@Override public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); Trees trees = workingCopy.getTrees(); TypeElement classEl = (TypeElement) trees.getElement(trees.getPath(cut, classTree)); MethodTree method = null; ExecutableElement methodEl = null; TreeMaker make = workingCopy.getTreeMaker(); workingCopy.rewrite(annotation, newAnnotation); int start = (int) sp.getStartPosition(cut, body); int end = (int) sp.getEndPosition(cut, body); String bodyText = getMethodBodyWithoutBraces(workingCopy.getText().substring(start, end)); if (action.isTaskEnabled()) { // switch to Task if (newTaskName != null) { newMethod = make.Method( method.getModifiers(), method.getName(), make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Task")), // NOI18N method.getTypeParameters(), method.getParameters(), workingCopy.rewrite(method, newMethod);
@Override public void run(WorkingCopy wc) throws Exception { wc.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cu = wc.getCompilationUnit(); ClassTree clazz = null; for (Tree typeDecl : cu.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { ClassTree candidate = (ClassTree) typeDecl; if (candidate.getModifiers().getFlags().contains(javax.lang.model.element.Modifier.PUBLIC)) { TreeMaker make = wc.getTreeMaker(); TypeElement cascadeTypeElement = wc.getElements().getTypeElement("javax.persistence.CascadeType"); // NOI18N AssignmentTree cascadeParameter = make.Assignment(make.Identifier("cascade"), make.MemberSelect(make.QualIdent(cascadeTypeElement),"ALL")); // NOI18N parameters.add(cascadeParameter); parameters.add(mappedByParameter); TypeElement oneToManyElement = wc.getElements().getTypeElement("javax.persistence.OneToMany"); // NOI18N TypeElement collectionElement = wc.getElements().getTypeElement("java.util.Collection"); // NOI18N TypeElement detailElement = wc.getElements().getTypeElement(detailInfo[1]); wc.rewrite(clazz, modifiedClass); wc.rewrite(clazz, modifiedClass); wc.rewrite(clazz, modifiedClass);
@Override public void run(WorkingCopy wc) throws Exception { wc.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cu = wc.getCompilationUnit(); ClassTree clazz = null; for (Tree typeDecl : cu.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { ClassTree candidate = (ClassTree) typeDecl; if (candidate.getModifiers().getFlags().contains(javax.lang.model.element.Modifier.PUBLIC)) { TreeMaker make = wc.getTreeMaker(); ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST); TypeElement changeListenerElement = wc.getElements().getTypeElement("java.beans.PropertyChangeListener"); // NOI18N VariableTree par = make.Variable(parMods, "listener", make.QualIdent(changeListenerElement), null); // NOI18N TypeElement changeSupportElement = wc.getElements().getTypeElement("java.beans.PropertyChangeSupport"); // NOI18N VariableTree changeSupport = make.Variable(parMods, "changeSupport", make.QualIdent(changeSupportElement), null); // NOI18N MemberSelectTree removeCall = make.MemberSelect(make.Identifier(changeSupport.getName()), "removePropertyChangeListener"); // NOI18N ); ClassTree modifiedClass = make.addClassMember(clazz, removeMethod); wc.rewrite(clazz, modifiedClass);
private Tree appendToAnnotationValue(Tree/*CompilationUnitTree|ModifiersTree*/ modifiers, TypeElement annotation, String attributeName, ExpressionTree... attributeValuesToAdd) { TreeMaker make = copy.getTreeMaker(); if (modifiers.getKind() == Kind.MODIFIERS) { annotations = ((ModifiersTree) modifiers).getAnnotations(); } else if (modifiers.getKind() == Kind.COMPILATION_UNIT) { annotations = ((CompilationUnitTree) modifiers).getPackageAnnotations(); } else { TreePath tp = new TreePath(new TreePath(copy.getCompilationUnit()), at.getAnnotationType()); Element e = copy.getTrees().getElement(tp); NewArrayTree newAssignment = make.NewArray(null, Collections.<ExpressionTree>emptyList(), values); return copy.getTreeUtilities().translate(modifiers, Collections.singletonMap(expression, newAssignment)); AnnotationTree newAnnotation = make.addAnnotationAttrValue(at, make.Assignment(make.Identifier(attributeName), make.NewArray(null, Collections.<ExpressionTree>emptyList(), Arrays.asList(attributeValuesToAdd)))); return copy.getTreeUtilities().translate(modifiers, Collections.singletonMap(at, newAnnotation)); AnnotationTree newAnnotation = make.Annotation(make.QualIdent(annotation), Collections.singletonList(attributeAssignmentTree)); if (modifiers.getKind() == Kind.MODIFIERS) { return make.addModifiersAnnotation((ModifiersTree) modifiers, newAnnotation); } else if (modifiers.getKind() == Kind.COMPILATION_UNIT) {
@Override public void run(WorkingCopy wc) throws Exception { wc.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cu = wc.getCompilationUnit(); ClassTree clazz = null; for (Tree typeDecl : cu.getTypeDecls()) { if (TreeUtilities.CLASS_TREE_KINDS.contains(typeDecl.getKind())) { ClassTree candidate = (ClassTree) typeDecl; if (candidate.getModifiers().getFlags().contains(javax.lang.model.element.Modifier.PUBLIC)) { TreeMaker make = wc.getTreeMaker(); ClassTree modifiedClass = clazz; TypeElement transientElement = wc.getElements().getTypeElement("javax.persistence.Transient"); // NOI18N TypeMirror transientMirror = transientElement.asType(); Tree transientType = make.Type(transientMirror); AnnotationTree transientTree = make.Annotation(transientType, Collections.EMPTY_LIST); ModifiersTree modifiers = make.Modifiers(Modifier.PRIVATE, Collections.singletonList(transientTree)); TypeElement changeSupportElement = wc.getElements().getTypeElement("java.beans.PropertyChangeSupport"); // NOI18N TypeMirror changeSupportMirror = changeSupportElement.asType(); Tree changeSupportType = make.Type(changeSupportMirror); MethodInvocationTree notification = make.MethodInvocation(Collections.EMPTY_LIST, fireMethod, fireArgs); newBlock = make.addBlockStatement(newBlock, make.ExpressionStatement(notification)); wc.rewrite(block, newBlock); wc.rewrite(clazz, modifiedClass);
@Override public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = workingCopy.getCompilationUnit(); if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) { ClassTree classT = (ClassTree) t; if (sourceFile.getName().equals(classT.getSimpleName().toString())) { TreeMaker make = workingCopy.getTreeMaker(); AnnotationTree annotation = createAnnotation(action, make, workingCopy); ModifiersTree methodModifiers = make.Modifiers( Collections.<Modifier>singleton(Modifier.PUBLIC), Collections.<AnnotationTree>singletonList(annotation)); if (action.isTaskEnabled()) { // method returns a new Task boolean hasAppGetter = AppFrameworkSupport.isViewClass(classTree, workingCopy); newMethod = make.Method( methodModifiers, action.getMethodName(), make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Task")), // NOI18N Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), modifiedClassTree = make.insertClassMember(classTree, insertIndex, newMethod); workingCopy.rewrite(classTree, modifiedClassTree);