/**Returns new tree based on {@code original}, such that each visited subtree * that occurs as a key in {@code original2Translated} is replaced by the corresponding * value from {@code original2Translated}. The value is then translated using the same * algorithm. Each key from {@code original2Translated} is used at most once. * Unless the provided {@code original} tree is a key in {@code original2Translated}, * the resulting tree has the same type as {@code original}. * * Principally, the method inner workings are: * <pre> * translate(original, original2Translated) { * if (original2Translated.containsKey(original)) * return translate(original2Translated.remove(original)); * newTree = copyOf(original); * for (Tree child : allChildrenOf(original)) { * newTree.replace(child, translate(child, original2Translated)); * } * return newTree; * } * </pre> * * @param original the tree that should be translated * @param original2Translated map containing trees that should be translated * @return translated tree. * @since 0.124 */ public @NonNull DocTree translate(final @NonNull DocTree original, final @NonNull Map<? extends DocTree, ? extends DocTree> original2Translated) { return translate(original, original2Translated, new NoImports(info), null); }
/**Returns new tree based on {@code original}, such that each visited subtree * that occurs as a key in {@code original2Translated} is replaced by the corresponding * value from {@code original2Translated}. The value is then translated using the same * algorithm. Each key from {@code original2Translated} is used at most once. * Unless the provided {@code original} tree is a key in {@code original2Translated}, * the resulting tree has the same type as {@code original}. * * Principally, the method inner workings are: * <pre> * translate(original, original2Translated) { * if (original2Translated.containsKey(original)) * return translate(original2Translated.remove(original)); * newTree = copyOf(original); * for (Tree child : allChildrenOf(original)) { * newTree.replace(child, translate(child, original2Translated)); * } * return newTree; * } * </pre> * * @param original the tree that should be translated * @param original2Translated map containing trees that should be translated * @return translated tree. * @since 0.64 */ public @NonNull Tree translate(final @NonNull Tree original, final @NonNull Map<? extends Tree, ? extends Tree> original2Translated) { return translate(original, original2Translated, new NoImports(info), null); }
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); }
/** * 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); }
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); }