Refine search
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 MethodInvocationTree removeInvocation = make.MethodInvocation(Collections.EMPTY_LIST, removeCall, Collections.singletonList(make.Identifier(par.getName()))); MethodTree removeMethod = make.Method( make.Modifiers(Modifier.PUBLIC, Collections.EMPTY_LIST), "removePropertyChangeListener", // NOI18N make.PrimitiveType(TypeKind.VOID), Collections.EMPTY_LIST, Collections.singletonList(par), Collections.EMPTY_LIST, make.Block(Collections.singletonList(make.ExpressionStatement(removeInvocation)), false), null ); ClassTree modifiedClass = make.addClassMember(clazz, removeMethod); wc.rewrite(clazz, modifiedClass);
public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(Phase.RESOLVED); TreeMaker make = workingCopy.getTreeMaker(); ClassTree javaClass = SourceUtils.getPublicTopLevelTree(workingCopy); IdentifierTree id = make.Identifier("javax.ejb.SessionBean"); ClassTree modifiedClass = make.addClassImplementsClause(javaClass, id); if(implClause.getKind() == Kind.MEMBER_SELECT){ if (((MemberSelectTree)implClause).getIdentifier().contentEquals("Remote") ){ modifiedClass = make.removeClassImplementsClause(modifiedClass, implClause); break; VariableTree var = make.Variable(make.Modifiers(Collections.<Modifier>singleton(Modifier.PRIVATE), Collections.<AnnotationTree>emptyList()), "context", make.Identifier("javax.ejb.SessionContext"), null); modifiedClass = make.insertClassMember(modifiedClass, 0, var); ModifiersTree modifiersTree = make.Modifiers( Collections.<Modifier>singleton(Modifier.PUBLIC), Collections.<AnnotationTree>emptyList() ); List<VariableTree> params = new ArrayList<VariableTree>(); params.add(make.Variable( make.Modifiers( Collections.<Modifier>emptySet(), Collections.<AnnotationTree>emptyList() ), "aContext", make.Identifier("javax.ejb.SessionContext"), // parameter type MethodTree infMethod = make.Method(modifiersTree,
private static AnnotationTree createAnnotation(ProxyAction action, TreeMaker make, WorkingCopy workingCopy) { List<AssignmentTree> annAttrs = new LinkedList<AssignmentTree>(); for (String attrName : ProxyAction.getAnnotationAttributeNames()) { if (action.isAnnotationAttributeSet(attrName)) { Object value = action.getAnnotationAttributeValue(attrName); ExpressionTree expTree; if (value instanceof String) { expTree = make.Literal(value); } else if (value instanceof ProxyAction.BlockingType) { expTree = make.MemberSelect( make.MemberSelect(make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Task")), // NOI18N "BlockingScope"), // NOI18N value.toString()); } else { continue; } ExpressionTree identTree = make.Identifier(attrName); AssignmentTree attrTree = make.Assignment(identTree, expTree); annAttrs.add(attrTree); } } return make.Annotation(make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Action")), annAttrs); // NOI18N }
private ModifiersTree addAnnotation( String annotationFqn , TreeMaker maker,ModifiersTree modifiers ) { AnnotationTree newAnnotation = maker.Annotation( maker.QualIdent(annotationFqn) , Collections.<ExpressionTree>emptyList()); if (modifiers != null) { modifiers = maker.addModifiersAnnotation(modifiers, newAnnotation); } return modifiers; }
public static MethodInvocationTree createMethodInvocation(WorkingCopy wc, String beanName, String methodName, List<ExpressionTree> args) { TreeMaker treeMaker = wc.getTreeMaker(); return treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.MemberSelect(treeMaker.Identifier(beanName), methodName), args); }
public static MethodInvocationTree createMethodInvocation(WorkingCopy wc, String methodName, String[] pNames) { TreeMaker treeMaker = wc.getTreeMaker(); List<ExpressionTree> args = new ArrayList<ExpressionTree>(); for (int i = 0 ; pNames != null && i < pNames.length; i++) { ExpressionTree tree = treeMaker.Identifier(pNames[i]); args.add(tree); } return treeMaker.MethodInvocation(Collections.<ExpressionTree>emptyList(), treeMaker.Identifier(methodName), args); }
public Void visitIdentifier(IdentifierTree node, Void p) { TreePath path = getCurrentPath(); Element element = info.getTrees().getElement(path); if (element != null && element.asType().getKind() != TypeKind.ERROR) { // solve the imports only when declared type!!! if (element.getKind().isClass() || element.getKind().isInterface() || (element.getKind().isField() && ((Symbol) element).isStatic())) { Tree parent = path.getParentPath() != null ? path.getParentPath().getLeaf() : null; if ( (parent != null && parent.getKind() == Kind.CASE && ((CaseTree) parent).getExpression() == node && element.getKind() == ElementKind.ENUM_CONSTANT) || (path.getCompilationUnit() != null && ((Symbol) element).enclClass() != null && path.getCompilationUnit().getSourceFile() == ((Symbol) element).enclClass().sourcefile)) { translateMap.put(node, make.Identifier(element.getSimpleName())); } else { translateMap.put(node, make.QualIdent(element)); } } } return null; }
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; switch (e.getKind()) { case METHOD: case ENUM_CONSTANT: case FIELD: StringBuilder name = new StringBuilder(((TypeElement)e.getEnclosingElement()).getQualifiedName()).append('.').append(e.getSimpleName()); if (!staticImportNames.add(name.toString())) break; 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())); 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());
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())); mt = make.addModifiersModifier(mt, Modifier.DEFAULT); mt = make.addModifiersModifier(mt, Modifier.ABSTRACT); MethodTree method = make.Method(mt, prototype.getName(), prototype.getReturnType(), prototype.getTypeParameters(), prototype.getParameters(), prototype.getThrows(), bodyTemplate, null); 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());
/** * 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); }
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)); bounds.add((ExpressionTree) make.Type(bound)); typeParams.add(make.TypeParameter(typeVariable.asElement().getSimpleName(), bounds)); Tree returnType = make.Type(et.getReturnType()); Iterator<? extends VariableElement> formArgNames = method.getParameters().iterator(); Iterator<? extends TypeMirror> formArgTypes = et.getParameterTypes().iterator(); ModifiersTree parameterModifiers = make.Modifiers(EnumSet.noneOf(Modifier.class)); while (formArgNames.hasNext() && formArgTypes.hasNext()) { VariableElement formArgName = formArgNames.next(); TypeMirror formArgType = formArgTypes.next(); if (isVarArgs && !formArgNames.hasNext()) { parameterModifiers = make.Modifiers(1L << 34, Collections.<AnnotationTree>emptyList()); params.add(make.Variable(parameterModifiers, paramName, resolveWildcard(formArgType), null)); throwsList.add((ExpressionTree) make.Type(tm)); ModifiersTree mt = make.Modifiers(flags, Collections.<AnnotationTree>emptyList());
if (TreeUtilities.CLASS_TREE_KINDS.contains(t.getKind())) { ClassTree classT = (ClassTree) t; if (sourceFile.getName().equals(classT.getSimpleName().toString())) { && el.getModifiers().contains(Modifier.PUBLIC)) { MethodTree mt = trees.getTree(el); for (AnnotationTree at : mt.getModifiers().getAnnotations()) { TypeElement annEl = (TypeElement) trees.getElement( newMethod = make.Method( method.getModifiers(), method.getName(), make.QualIdent(workingCopy.getElements().getTypeElement("org.jdesktop.application.Task")), // NOI18N method.getTypeParameters(), method.getParameters(), null); } else { // switch to void newMethod = make.Method( method.getModifiers(), method.getName(), make.PrimitiveType(TypeKind.VOID), method.getTypeParameters(), method.getParameters(),
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 AssignmentTree mappedByParameter = make.Assignment(make.Identifier("mappedBy"), make.Literal(df[0])); // NOI18N List<ExpressionTree> parameters = new ArrayList<ExpressionTree>(); parameters.add(cascadeParameter); AnnotationTree oneToManyTree = make.Annotation(make.QualIdent(oneToManyElement), parameters); ModifiersTree modifiers = make.Modifiers(Collections.singleton(Modifier.PRIVATE), Collections.singletonList(oneToManyTree)); ParameterizedTypeTree collectionTree = make.ParameterizedType(make.QualIdent(collectionElement), Collections.singletonList(make.QualIdent(detailElement))); VariableTree field = make.Variable(modifiers, masterFieldName, collectionTree, null); ClassTree modifiedClass = make.insertClassMember(clazz, fieldIndex, field); ReturnTree returnExp = make.Return(make.Identifier(masterFieldName)); MethodTree getMethod = make.Method( make.Modifiers(Collections.singleton(Modifier.PUBLIC), Collections.EMPTY_LIST), "get" + masterMethodSuffix, // NOI18N collectionTree, Collections.EMPTY_LIST, Collections.EMPTY_LIST, make.Block(Collections.singletonList(returnExp), false), null ); modifiedClass = make.addClassMember(modifiedClass, getMethod); ModifiersTree parMods = make.Modifiers(Collections.EMPTY_SET, Collections.EMPTY_LIST); VariableTree par = make.Variable(parMods, masterFieldName, collectionTree, null);
private Tree appendToAnnotationValue(Tree/*CompilationUnitTree|ModifiersTree*/ modifiers, TypeElement annotation, String attributeName, ExpressionTree... attributeValuesToAdd) { TreeMaker make = copy.getTreeMaker(); 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)); attribute = make.NewArray(null, Collections.<ExpressionTree>emptyList(), Arrays.asList(attributeValuesToAdd)); attributeAssignmentTree = attribute; } else { attributeAssignmentTree = make.Assignment(make.Identifier(attributeName), attribute); 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) { return make.addPackageAnnotation((CompilationUnitTree) modifiers, newAnnotation); } else { throw new IllegalStateException();
if (Tree.Kind.VARIABLE == member.getKind()) { VariableTree variable = (VariableTree)member; String type = variable.getType().toString(); TreeMaker make = wc.getTreeMaker(); ClassTree modifiedClass = clazz; Tree transientType = make.Type(transientMirror); AnnotationTree transientTree = make.Annotation(transientType, Collections.EMPTY_LIST); ModifiersTree modifiers = make.Modifiers(Modifier.PRIVATE, Collections.singletonList(transientTree)); Tree changeSupportType = make.Type(changeSupportMirror); NewClassTree changeSupportConstructor = make.NewClass(null, Collections.EMPTY_LIST, make.QualIdent(changeSupportElement), Collections.singletonList(make.Identifier("this")), null); VariableTree changeSupport = make.Variable(modifiers, "changeSupport", changeSupportType, changeSupportConstructor); // NOI18N modifiedClass = make.insertClassMember(clazz, 0, changeSupport); String oldParameterName = "old" + Character.toUpperCase(parameterName.charAt(0)) + parameterName.substring(1); // NOI18N Tree parameterTree = parameter.getType(); VariableTree oldParameter = make.Variable(make.Modifiers(Collections.EMPTY_SET), oldParameterName, parameterTree, persistentVariable); BlockTree newBlock = make.insertBlockStatement(block, 0, oldParameter); MemberSelectTree fireMethod = make.MemberSelect(make.Identifier("changeSupport"), "firePropertyChange"); // NOI18N List<ExpressionTree> fireArgs = new LinkedList<ExpressionTree>(); fireArgs.add(make.Literal(propName)); fireArgs.add(make.Identifier(oldParameterName)); fireArgs.add(make.Identifier(parameterName)); MethodInvocationTree notification = make.MethodInvocation(Collections.EMPTY_LIST, fireMethod, fireArgs); newBlock = make.addBlockStatement(newBlock, make.ExpressionStatement(notification));
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(), null); } else { newMethod = make.Method( methodModifiers, action.getMethodName(), make.PrimitiveType(TypeKind.VOID), Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), make.Block(Collections.<StatementTree>emptyList(), false), // "// put your action code here", null); modifiedClassTree = make.addClassMember(classTree, newMethod); } else { modifiedClassTree = make.insertClassMember(classTree, insertIndex, newMethod);
Document doc = null; try { doc = copy.getDocument(); if (doc == null) { doc = copy.getSnapshot().getSource().getDocument(true); SourcePositions sp = copy.getTrees().getSourcePositions(); TreeUtilities utils = copy.getTreeUtilities(); CompilationUnitTree compilationUnit = copy.getCompilationUnit(); if (member.getKind() == Tree.Kind.VARIABLE) { if (member.getKind() == Tree.Kind.BLOCK) { v = new FieldRefVisitor(clazz, ((BlockTree)member).isStatic()); TreePath classTP = new TreePath(new TreePath(compilationUnit), clazz); v.scan(classTP, null); if (!utils.isSynthetic(compilationUnit, tree)) { if (gsnames != null && gsidx < 0) { for (String name : gsnames) { idx = Math.min(maxIndex, idx); return copy.getTreeMaker().insertClassMember(clazz, idx, member);
public void run(WorkingCopy workingCopy) throws Exception { workingCopy.toPhase(Phase.RESOLVED); TreeMaker make = workingCopy.getTreeMaker(); GenerationUtils gu = GenerationUtils.newInstance(workingCopy); TypeElement typeElement = SourceUtils.getPublicTopLevelElement(workingCopy); ClassTree oldClassTree = workingCopy.getTrees().getTree(typeElement); ClassTree classTree = addFields(gu, make, typeElement, oldClassTree); if (isBodyTag) { classTree = make.addClassMember(classTree, addBodyEvaluatorCheck(evaluateBody, make)); } classTree = addSetters(gu, make, typeElement, classTree); workingCopy.rewrite(oldClassTree, classTree); } };
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); } } } } });