@Override public Void scan(Tree node, Void p) { if (node == null) return null; boolean oldSynthetic = synthetic; try { synthetic |= getTreeUtilities().isSynthetic(diffContext.origUnit, node); if (!synthetic) { oldTrees.add(node); } addSyntheticTrees(diffContext, node); return super.scan(node, p); } finally { synthetic = oldSynthetic; } }
private void renameIfMatch(TreePath path, Tree tree) { if (workingCopy.getTreeUtilities().isSynthetic(path)) { return; } Element el = workingCopy.getTrees().getElement(path); if(el != null && elementAndNames.containsKey(el)) { Tree nju = make.setLabel(tree, elementAndNames.get(el)); workingCopy.rewrite(tree, nju); } } }
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());
if (typeTree == null) { TypeMirror typeMirror = copy.getTreeUtilities().parseType(typeName, scope); typeTree = make.Type(typeMirror);
/** * Creates a new Tree for a given String type specification. * * @param type String type specification * @see com.sun.source.tree.ExpressionTree * @since 0.65 */ public @NonNull Tree Type(@NonNull String type) { Parameters.notNull("type", type); Tree typeTree = copy.getTreeUtilities().parseType(type); final Map<Tree, Tree> translate = new HashMap<Tree, Tree>(); new TreeScanner<Void, Void>() { @Override public Void visitMemberSelect(MemberSelectTree node, Void p) { translate.put(node, QualIdent(node.toString())); return null; } @Override public Void visitIdentifier(IdentifierTree node, Void p) { translate.put(node, QualIdent(node.toString())); return null; } }.scan(typeTree, null); return copy.getTreeUtilities().translate(typeTree, translate); }
private void transformStringLiteral(TreePath literalTreePath, LiteralTree literalTree) { if (workingCopy.getTreeUtilities().isSynthetic(literalTreePath)) { return; } Object valueObject = literalTree.getValue(); if (valueObject instanceof String) { String value = (String) valueObject; switch (matchKind) { case EXACT: if (value.equals(oldString)) { Tree newLiteralTree = make.Literal(newString); workingCopy.rewrite(literalTree, newLiteralTree); } break; case PREFIX: if (value.startsWith(oldString)) { Tree newLiteralTree = make.Literal(newString + value.substring(oldString.length())); workingCopy.rewrite(literalTree, newLiteralTree); } break; case SUFFIX: if (value.endsWith(oldString)) { Tree newLiteralTree = make.Literal(value.substring(0, value.length() - oldString.length()) + newString); workingCopy.rewrite(literalTree, newLiteralTree); } break; } } } }
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 void addSyntheticTrees(DiffContext diffContext, Tree node) { if (node == null) return ; if (((JCTree) node).pos == (-1)) { diffContext.syntheticTrees.add(node); return ; } if (node.getKind() == Kind.EXPRESSION_STATEMENT) { ExpressionTree est = ((ExpressionStatementTree) node).getExpression(); if (est.getKind() == Kind.METHOD_INVOCATION) { ExpressionTree select = ((MethodInvocationTree) est).getMethodSelect(); if (select.getKind() == Kind.IDENTIFIER && ((IdentifierTree) select).getName().contentEquals("super")) { if (getTreeUtilities().isSynthetic(diffContext.origUnit, node)) { diffContext.syntheticTrees.add(node); } } } } }
private void renameIfMatch(TreePath path, Tree tree, Element elemToRename) { if (workingCopy.getTreeUtilities().isSynthetic(path)) return; Element el = workingCopy.getTrees().getElement(path); if (el != null && el.equals(elemToRename)) { Tree nju = workingCopy.getTreeMaker().setLabel(tree, newName); workingCopy.rewrite(tree, nju); } } }
private BlockTree addStatement(WorkingCopy wc, BlockTree blockTree, String beanName, String methodName, String valueSource) { TreeMaker make = wc.getTreeMaker(); ExecutableElement elem = execElementHandle.resolve(wc); ArrayList<ExpressionTree> args = new ArrayList<ExpressionTree>(); if (valueSource != null) { SourcePositions[] positions = new SourcePositions[1]; args.add(wc.getTreeUtilities().parseExpression(valueSource, positions)); } ExpressionTree exprTree = TreeMakerUtils.createMethodInvocation(wc, beanName, methodName, args); ExpressionStatementTree exprStatTree = wc.getTreeMaker().ExpressionStatement(exprTree); return wc.getTreeMaker().addBlockStatement(blockTree, exprStatTree); }
private static void moveCommentsBeforeOffset(WorkingCopy wc, Tree from, Tree to, int offset, Document doc) { List<Comment> toMove = new LinkedList<>(); int idx = 0; for (Comment comment : wc.getTreeUtilities().getComments(from, true)) { if (comment.pos() >= offset || comment.endPos() > offset) { break; } DocumentGuards guards = LineDocumentUtils.as(doc, DocumentGuards.class); if (guards != null) { int epAfterBlock = guards.adjustPosition(comment.pos(), true); // comment that ends exactly at the GB boundary cannot be really // reassigned from the previous member. if (epAfterBlock >= comment.endPos()) { // set new offset, after the guarded block break; } } toMove.add(comment); idx++; } if (toMove.size() > 0) { doMoveComments(wc, from, to, offset, toMove, 0, idx); } }
/** * 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 }
int idx = 0; int firstToRemove = -1; for (Comment comment : wc.getTreeUtilities().getComments(from, false)) { if (comment.endPos() <= offset) {
private Tree resolveWildcard(TypeMirror type) { TreeMaker make = copy.getTreeMaker(); Tree result; if (type != null && type.getKind() == TypeKind.WILDCARD) { WildcardType wt = (WildcardType) type; TypeMirror bound = wt.getSuperBound(); if (bound == null) { bound = wt.getExtendsBound(); } if (bound == null) { return make.Type("java.lang.Object"); } result = make.Type(bound); } else { result = make.Type(type); } final Map<Tree, Tree> translate = new IdentityHashMap<Tree, Tree>(); new TreeScanner<Void, Void>() { @Override public Void visitWildcard(WildcardTree node, Void p) { Tree bound = node.getBound(); if (bound != null && (bound.getKind() == Kind.EXTENDS_WILDCARD || bound.getKind() == Kind.SUPER_WILDCARD)) { translate.put(bound, ((WildcardTree) bound).getBound()); } return super.visitWildcard(node, p); } }.scan(result, null); return copy.getTreeUtilities().translate(result, translate); }
return copy.getTreeUtilities().translate(modifiers, Collections.singletonMap(expression, newAssignment)); return copy.getTreeUtilities().translate(modifiers, Collections.singletonMap(at, newAnnotation));
public static <T extends Tree> T importFQNs(WorkingCopy copy, T tree) { if (tree == null) return null; TranslateIdentifier ti = new TranslateIdentifier(copy); //XXX: the TreePath constructed below below depends on javac internals (that elements are attributes of a tree, not a tree path): ti.scan(tree.getKind() == Kind.COMPILATION_UNIT ? new TreePath((CompilationUnitTree) tree) : new TreePath(new TreePath(copy.getCompilationUnit()), tree), null); return (T) copy.getTreeUtilities().translate(tree, ti.translateMap); }
/** * Creates a new BlockTree for provided <tt>bodyText</tt>. * * @param method figures out the scope for attribution. * @param bodyText text which will be used for method body creation. * @return a new tree for <tt>bodyText</tt>. */ public BlockTree createMethodBody(MethodTree method, String bodyText) { SourcePositions[] positions = new SourcePositions[1]; final TreeUtilities treeUtils = copy.getTreeUtilities(); StatementTree body = treeUtils.parseStatement(bodyText, positions); assert Tree.Kind.BLOCK == body.getKind() : "Not a statement block!"; Scope scope = copy.getTrees().getScope(TreePath.getPath(copy.getCompilationUnit(), method)); treeUtils.attributeTree(body, scope); mapComments((BlockTree) body, bodyText, copy, handler, positions[0]); new TreePosCleaner().scan(body, null); return (BlockTree) body; }
/** * Creates a new BlockTree for provided <tt>bodyText</tt>. * * @param lambda figures out the scope for attribution. * @param bodyText text which will be used for lambda body creation. * @return a new tree for <tt>bodyText</tt>. * @since 2.19 */ public BlockTree createLambdaBody(LambdaExpressionTree lambda, String bodyText) { SourcePositions[] positions = new SourcePositions[1]; final TreeUtilities treeUtils = copy.getTreeUtilities(); StatementTree body = treeUtils.parseStatement(bodyText, positions); assert Tree.Kind.BLOCK == body.getKind() : "Not a statement block!"; Scope scope = copy.getTrees().getScope(TreePath.getPath(copy.getCompilationUnit(), lambda)); treeUtils.attributeTree(body, scope); mapComments((BlockTree) body, bodyText, copy, handler, positions[0]); new TreePosCleaner().scan(body, null); return (BlockTree) body; }
copy.getTreeUtilities().attributeTree(body, s); body = importFQNs(body); copy.rewrite(method.getBody(), body);
ClassMemberComparator comparator = new ClassMemberComparator(codeStyle); SourcePositions sp = copy.getTrees().getSourcePositions(); TreeUtilities utils = copy.getTreeUtilities(); CompilationUnitTree compilationUnit = copy.getCompilationUnit(); Tree lastMember = null;