|
| 1 | +/* |
| 2 | + * Copyright (C) 2014 Google, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.testing.compile; |
| 17 | + |
| 18 | +import com.google.common.base.Function; |
| 19 | +import com.google.common.base.Joiner; |
| 20 | +import com.google.common.collect.FluentIterable; |
| 21 | +import com.google.common.collect.Lists; |
| 22 | + |
| 23 | +import com.sun.source.tree.BlockTree; |
| 24 | +import com.sun.source.tree.BreakTree; |
| 25 | +import com.sun.source.tree.ClassTree; |
| 26 | +import com.sun.source.tree.ContinueTree; |
| 27 | +import com.sun.source.tree.IdentifierTree; |
| 28 | +import com.sun.source.tree.ImportTree; |
| 29 | +import com.sun.source.tree.LabeledStatementTree; |
| 30 | +import com.sun.source.tree.LiteralTree; |
| 31 | +import com.sun.source.tree.MemberSelectTree; |
| 32 | +import com.sun.source.tree.MethodTree; |
| 33 | +import com.sun.source.tree.ModifiersTree; |
| 34 | +import com.sun.source.tree.PrimitiveTypeTree; |
| 35 | +import com.sun.source.tree.Tree; |
| 36 | +import com.sun.source.tree.TypeParameterTree; |
| 37 | +import com.sun.source.tree.VariableTree; |
| 38 | +import com.sun.source.util.SimpleTreeVisitor; |
| 39 | +import com.sun.source.util.TreePath; |
| 40 | + |
| 41 | +import java.util.List; |
| 42 | + |
| 43 | +/** |
| 44 | + * This class contains methods for providing breadcrumb {@code String}s which describe the contents |
| 45 | + * of a {@code TreePath}. |
| 46 | + * |
| 47 | + * @author Stephen Pratt |
| 48 | + */ |
| 49 | +final class Breadcrumbs { |
| 50 | + private static final BreadcrumbVisitor BREADCRUMB_VISITOR = new BreadcrumbVisitor(); |
| 51 | + private Breadcrumbs() {} |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns a string describing the {@link TreePath} given. |
| 55 | + */ |
| 56 | + static String describeTreePath(TreePath path) { |
| 57 | + // TODO(spratt) The number of extra strings this creates by building a list of strings, |
| 58 | + // and then joining it is at least a little wasteful. Consider modifying the BreadcrumbVisitor |
| 59 | + // to take a StringBuilder and traverse the whole path. |
| 60 | + return Joiner.on("->").join(getBreadcrumbList(path)); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns a list of breadcrumb strings describing the {@link TreePath} given. |
| 65 | + */ |
| 66 | + static List<String> getBreadcrumbList(TreePath path) { |
| 67 | + return Lists.reverse(FluentIterable.from(path) |
| 68 | + .transform(new Function<Tree, String>() { |
| 69 | + @Override public String apply(Tree t) { |
| 70 | + return t.accept(BREADCRUMB_VISITOR, null); |
| 71 | + } |
| 72 | + }).toList()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * A {@link SimpleTreeVisitor} for providing a breadcrumb {@code String} for a {@link Tree} node. |
| 77 | + * The breadcrumb {@code String} will not be unique, but can be used to give context about the |
| 78 | + * node as it exists within a {@code TreePath}. |
| 79 | + */ |
| 80 | + @SuppressWarnings("restriction") // Sun APIs usage intended |
| 81 | + static final class BreadcrumbVisitor extends SimpleTreeVisitor<String, Void> { |
| 82 | + |
| 83 | + /** Returns a {@code String} describing the {@code Tree.Kind} of the given {@code Tree}. */ |
| 84 | + private String kindString(Tree t) { |
| 85 | + return t.getKind().toString(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Returns a {@code String} describing the {@code Tree.Kind} of the given {@code Tree}. |
| 90 | + * The string will be specified by the {@code toString()} value of the detail object given. |
| 91 | + */ |
| 92 | + private String detailedKindString(Tree t, Object detail) { |
| 93 | + return String.format("%s(%s)", kindString(t), detail); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String defaultAction(Tree t, Void v) { |
| 98 | + return (t != null) ? kindString(t) : ""; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String visitBlock(BlockTree reference, Void v) { |
| 103 | + return (reference != null) |
| 104 | + ? detailedKindString(reference, reference.isStatic() ? "static" : "non-static") : ""; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String visitBreak(BreakTree reference, Void v) { |
| 109 | + return (reference != null) ? detailedKindString(reference, reference.getLabel()) : ""; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public String visitClass(ClassTree reference, Void v) { |
| 114 | + return (reference != null) ? detailedKindString(reference, reference.getSimpleName()) : ""; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public String visitContinue(ContinueTree reference, Void v) { |
| 119 | + return (reference != null) ? detailedKindString(reference, reference.getLabel()) : ""; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String visitIdentifier(IdentifierTree reference, Void v) { |
| 124 | + return (reference != null) ? detailedKindString(reference, reference.getName()) : ""; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String visitImport(ImportTree reference, Void v) { |
| 129 | + return (reference != null) ? |
| 130 | + detailedKindString(reference, reference.isStatic() ? "static" : "non-static") : ""; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public String visitLabeledStatement(LabeledStatementTree reference, Void v) { |
| 135 | + return (reference != null) ? detailedKindString(reference, reference.getLabel()) : ""; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public String visitLiteral(LiteralTree reference, Void v) { |
| 140 | + return (reference != null) ? detailedKindString(reference, reference.getValue()) : ""; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String visitMethod(MethodTree reference, Void v) { |
| 145 | + return (reference != null) ? detailedKindString(reference, reference.getName()) : ""; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public String visitModifiers(ModifiersTree reference, Void v) { |
| 150 | + return (reference != null) ? detailedKindString(reference, reference.getFlags()) : ""; |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public String visitMemberSelect(MemberSelectTree reference, Void v) { |
| 155 | + return (reference != null) ? detailedKindString(reference, reference.getIdentifier()) : ""; |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public String visitPrimitiveType(PrimitiveTypeTree reference, Void v) { |
| 160 | + return (reference != null) |
| 161 | + ? detailedKindString(reference, reference.getPrimitiveTypeKind()) : ""; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public String visitTypeParameter(TypeParameterTree reference, Void v) { |
| 166 | + return (reference != null) ? detailedKindString(reference, reference.getName()) : ""; |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public String visitVariable(VariableTree reference, Void v) { |
| 171 | + return (reference != null) ? detailedKindString(reference, reference.getName()) : ""; |
| 172 | + } |
| 173 | + } |
| 174 | +} |
0 commit comments