for (MethodTree t : GeneratorUtilities.get(wc).createAbstractMethodImplementations(te, methods)) { newClassTree = GeneratorUtilities.get(wc).insertClassMember(newClassTree, t);
/** * Returns the instance of this class * * @param copy * @return the {@link GeneratorUtilities} instance * @since 0.20 */ public static GeneratorUtilities get(WorkingCopy copy) { return new GeneratorUtilities(copy); }
return (CompilationUnitTree) appendToAnnotationValue((Tree) compilationUnit, annotation, attributeName, attributeValuesToAdd);
N result = asReplacementOf(setLabelImpl(node, aLabel), node, true); GeneratorUtilities gu = GeneratorUtilities.get(copy); gu.copyComments(node, result, true); gu.copyComments(node, result, false);
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 (supportsOverride(copy)) { if (isImplement || clazz.getKind().isClass() && (!isAbstract || !clazz.getModifiers().contains(Modifier.ABSTRACT))) { try { bodyTemplate = "{" + readFromTemplate(isAbstract ? GENERATED_METHOD_BODY : OVERRIDDEN_METHOD_BODY, createBindings(clazz, element)) + "\n}"; //NOI18N } catch (Exception e) { bodyTemplate = "{}"; //NOI18N if (containsErrors(method.getBody())) { copy.rewrite(method.getBody(), make.Block(Collections.<StatementTree>emptyList(), false)); } else { BlockTree body = method.getBody(); copy.getTreeUtilities().attributeTree(body, s); body = importFQNs(body); copy.rewrite(method.getBody(), body);
if (cnt >= staticTreshold) cnt = -1; } else if (cnt >= treshold || checkPackagesForStarImport(((PackageElement)el).getQualifiedName().toString(), cs)) { cnt = -1; Element e = getImportedElement(cut, imp); if (!elementsToImport.contains(e)) { if (imp.isStatic()) { if (isStarImport(imp) && threshold == Integer.MAX_VALUE) { Map map = imp.isStatic() ? typeCounts : pkgCounts; Integer cnt = (Integer)map.get(e); if (!isStarImport(imp)) { continue; Element e = getImportedElement(cut, imp); Integer cnt; if (imp.isStatic()) { } else { if (usedTypes == null) { usedTypes = getUsedTypes(cut); } else { if (usedTypes == null) { usedTypes = getUsedTypes(cut); ExpressionTree qualIdent = qualIdentFor(currentToImportElement);
return GeneratorUtilities.get(copy).insertClassMembers(clazz, members); moveCommentsAfterOffset(copy, lastMember, members.get(0), offset, doc); moveCommentsBeforeOffset(copy, nextMember, members.get(members.size() - 1), offset, doc);
Tree newTree = null; if(!map.containsKey(tree)) { Tree importComments = GeneratorUtilities.get(WorkingCopy.this).importComments(tree, getCompilationUnit()); newTree = importComments.accept(duplicator, null); map.put(tree, newTree);
TypeMirror type = copy.getTypes().asMemberOf((DeclaredType)clazz.asType(), ve); if (isDefault) { statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), ve.getSimpleName()), make.Literal(defaultValue(type))))); //NOI18N } else { String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(ve, cs), cs); parameters.add(make.Variable(parameterModifiers, paramName, make.Type(type), null)); statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), ve.getSimpleName()), make.Identifier(paramName)))); //NOI18N TypeMirror type = parameterTypes != null ? parameterTypes.next() : ve.asType(); if (isDefault) { arguments.add(make.Literal(defaultValue(type))); } else { String paramName = addParamPrefixSuffix(removeParamPrefixSuffix(ve, cs), cs); parameters.add(make.Variable(parameterModifiers, paramName, make.Type(type), null)); arguments.add(make.Identifier(paramName));
protected final CompilationUnitTree rewriteChildren(CompilationUnitTree tree) { ExpressionTree pid = (ExpressionTree)translate(tree.getPackageName()); importAnalysis.setCompilationUnit(tree); importAnalysis.setPackage(tree.getPackageName()); List<? extends ImportTree> imps = translate(tree.getImports()); importAnalysis.setImports(imps); List<? extends AnnotationTree> annotations = translate(tree.getPackageAnnotations()); List<? extends Tree> types = translate(tree.getTypeDecls()); Set<? extends Element> newImports = importAnalysis.getImports(); if (copy != null && newImports != null && !newImports.isEmpty()) { imps = GeneratorUtilities.get(copy).addImports(tree, newImports).getImports(); } if (!annotations.equals(tree.getPackageAnnotations()) || pid!=tree.getPackageName() || !imps.equals(tree.getImports()) || !types.equals(tree.getTypeDecls())) { CompilationUnitTree n = make.CompilationUnit(annotations, pid, imps, types, tree.getSourceFile()); model.setElement(n, model.getElement(tree)); copyCommentTo(tree,n); model.setPos(n, model.getPos(tree)); tree = n; } return tree; }
if (toImport == null || GeneratorUtilities.checkPackagesForStarImport(qName, cs)) { toImport = element; 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);
/** * 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); }
Collections.<AnnotationTree>emptyList()); String paramName = addParamPrefixSuffix(removeParamPrefixSuffix(formArgName, cs), cs); params.add(make.Variable(parameterModifiers, paramName, resolveWildcard(formArgType), null));
/** * Inserts members to a class. Using the rules specified in the {@link CodeStyle} * it finds the proper place for each of the members and calls {@link TreeMaker.insertClassMember} * * @param clazz the class to insert the members to * @param members the members to insert * @return the modified class * @since 0.20 */ public ClassTree insertClassMembers(ClassTree clazz, Iterable<? extends Tree> members) { assert members != null; for (Tree member : members) clazz = insertClassMember(clazz, member); return clazz; }
/** * Creates implementations of the all abstract methods within a class. * * @param clazz the class to create the implementations within * @return the abstract method implementations * @since 0.20 */ public List<? extends MethodTree> createAllAbstractMethodImplementations(TypeElement clazz) { return createAbstractMethodImplementations(clazz, copy.getElementUtilities().findUnimplementedMethods(clazz)); }
for (Iterator<? extends VariableElement> it = element.getParameters().iterator(); it.hasNext();) { VariableElement ve = it.next(); sb.append(addParamPrefixSuffix(removeParamPrefixSuffix(ve, cs), cs)); if (it.hasNext()) sb.append(","); //NOI18N
private List<Difference> processExternalCUs(Map<?, int[]> tag2Span, Set<Tree> syntheticTrees) { if (externalChanges == null) { return Collections.<Difference>emptyList(); } List<Difference> result = new LinkedList<Difference>(); for (CompilationUnitTree t : externalChanges.values()) { try { FileObject targetFile = doCreateFromTemplate(t); CompilationUnitTree templateCUT = impl.getJavacTask().parse(FileObjects.sourceFileObject(targetFile, targetFile.getParent())).iterator().next(); CompilationUnitTree importComments = GeneratorUtilities.get(this).importComments(templateCUT, templateCUT); rewrite(importComments, getTreeMaker().asRemoved(t)); //changes.put(importComments, t); StringWriter target = new StringWriter(); ModificationResult.commit(targetFile, processCurrentCompilationUnit(new DiffContext(this, templateCUT, codeForCompilationUnit(templateCUT), new PositionConverter(), targetFile, syntheticTrees, getFileObject() != null ? getCompilationUnit() : null, getFileObject() != null ? getText() : null), tag2Span), target); result.add(new CreateChange(t.getSourceFile(), target.toString())); target.close(); } catch (BadLocationException ex) { Exceptions.printStackTrace(ex); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } return result; }
CompilationUnitTree ncut = GeneratorUtilities.get(this).addImports(diffContext.origUnit, nueImports); additionalDiffs.addAll(CasualDiff.diff(getContext(), diffContext, getTreeUtilities(), diffContext.origUnit.getImports(), ncut.getImports(), userInfo, tree2Tag, tree2Doc, tag2Span, oldTrees));
case BLOCK: tree = tm.Block(((BlockTree)from).getStatements(), ((BlockTree)from).isStatic()); GeneratorUtilities gu = GeneratorUtilities.get(wc); gu.copyComments(from, tree, true); gu.copyComments(from, tree, false); break;
/** * Creates a class constructor. * * @param clazz the class to create the constructor for * @param fields fields to be initialized by the constructor * @return the constructor * @since 0.20 */ public MethodTree createConstructor(ClassTree clazz, Iterable<? extends VariableTree> fields) { assert clazz != null && fields != null; TreeMaker make = copy.getTreeMaker(); CodeStyle cs = DiffContext.getCodeStyle(copy); Set<Modifier> mods = EnumSet.of(copy.getTreeUtilities().isEnum(clazz) ? Modifier.PRIVATE : Modifier.PUBLIC); List<VariableTree> parameters = new ArrayList<VariableTree>(); List<StatementTree> statements = new ArrayList<StatementTree>(); ModifiersTree parameterModifiers = make.Modifiers(EnumSet.noneOf(Modifier.class)); for (VariableTree vt : fields) { String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(vt, cs), cs); parameters.add(make.Variable(parameterModifiers, paramName, vt.getType(), null)); statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), vt.getName()), make.Identifier(paramName)))); //NOI18N } BlockTree body = make.Block(statements, false); return make.Method(make.Modifiers(mods), "<init>", null, Collections.<TypeParameterTree> emptyList(), parameters, Collections.<ExpressionTree>emptyList(), body, null); //NOI18N }