|
| 1 | +/* |
| 2 | + * Copyright 2014 The Kythe Authors. All rights reserved. |
| 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 | + |
| 17 | +package com.google.devtools.kythe.platform.java.helpers; |
| 18 | + |
| 19 | +import com.google.common.flogger.FluentLogger; |
| 20 | +import com.sun.source.tree.BlockTree; |
| 21 | +import com.sun.source.tree.CatchTree; |
| 22 | +import com.sun.source.tree.ClassTree; |
| 23 | +import com.sun.source.tree.CompilationUnitTree; |
| 24 | +import com.sun.source.tree.EnhancedForLoopTree; |
| 25 | +import com.sun.source.tree.ForLoopTree; |
| 26 | +import com.sun.source.tree.MethodTree; |
| 27 | +import com.sun.source.tree.Tree; |
| 28 | +import com.sun.source.util.TreePath; |
| 29 | +import com.sun.source.util.TreeScanner; |
| 30 | +import com.sun.tools.javac.tree.JCTree; |
| 31 | +import com.sun.tools.javac.tree.JCTree.JCBlock; |
| 32 | +import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
| 33 | +import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Map; |
| 36 | +import javax.lang.model.element.Element; |
| 37 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 38 | + |
| 39 | +/** |
| 40 | + * This class is responsible for generating signatures for blocks and anonymous classes. The maps |
| 41 | + * used for caching in this class are specific through out the life of analyzer and they can be used |
| 42 | + * for one or more compilation units which are part of one compilation context. |
| 43 | + */ |
| 44 | +public class BlockAnonymousSignatureGenerator |
| 45 | + extends TreeScanner<Void, BlockAnonymousSignatureGenerator.BlockAnonymousData> { |
| 46 | + /* |
| 47 | + * Blocks are numbered sequentially from zero within a class scope or a method scope. |
| 48 | + * Here is an example of block numbering: |
| 49 | + * <code><pre> |
| 50 | + * class A {// block 0 |
| 51 | + * {// block 1 |
| 52 | + * {// block 2 |
| 53 | + * } |
| 54 | + * } |
| 55 | + * {// block 3 |
| 56 | + * } |
| 57 | + * void method() {// block 0 |
| 58 | + * {// block 1 |
| 59 | + * } |
| 60 | + * if (true) {// block 2 |
| 61 | + * } else {// block 3 |
| 62 | + * } |
| 63 | + * } |
| 64 | + * } |
| 65 | + * </code></pre> |
| 66 | + */ |
| 67 | + |
| 68 | + static class BlockAnonymousData { |
| 69 | + int blockNumber; |
| 70 | + int anonymousNumber; |
| 71 | + final String topLevelSignature; |
| 72 | + |
| 73 | + public BlockAnonymousData(int blockNumber, int anonymousNumber, String topLevelSignature) { |
| 74 | + this.blockNumber = blockNumber; |
| 75 | + this.anonymousNumber = anonymousNumber; |
| 76 | + this.topLevelSignature = topLevelSignature; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 81 | + private final SignatureGenerator signatureGenerator; |
| 82 | + |
| 83 | + BlockAnonymousSignatureGenerator(SignatureGenerator signatureGenerator) { |
| 84 | + this.signatureGenerator = signatureGenerator; |
| 85 | + } |
| 86 | + |
| 87 | + // Indicates whether the compilation unit has already been processed or not. |
| 88 | + private boolean unitProcessed = false; |
| 89 | + |
| 90 | + // Holds the signature for each block visited. |
| 91 | + // Holds the signature for each anonymous class. |
| 92 | + // Anonymous classes are numbered with respect to their first enclosing class declaration or |
| 93 | + // block. They are numbered from zero. |
| 94 | + private final Map<Tree, String> blockAnonymousMap = new HashMap<>(); |
| 95 | + |
| 96 | + /** Returns the block signature of the corresponding element */ |
| 97 | + String getBlockSignature(Element element) { |
| 98 | + TreePath path = signatureGenerator.getPath(element); |
| 99 | + if (path == null) { |
| 100 | + throw new IllegalStateException("path was null for element: " + element); |
| 101 | + } |
| 102 | + return getBlockSignature(path); |
| 103 | + } |
| 104 | + |
| 105 | + /** Returns the block signature of the corresponding path */ |
| 106 | + String getBlockSignature(TreePath path) { |
| 107 | + this.process(path.getCompilationUnit()); |
| 108 | + path = path.getParentPath(); |
| 109 | + JCTree parent = (JCTree) path.getLeaf(); |
| 110 | + while (!(parent.getTag() == JCTree.Tag.BLOCK |
| 111 | + || parent.getTag() == JCTree.Tag.CLASSDEF |
| 112 | + || parent.getTag() == JCTree.Tag.FORLOOP |
| 113 | + || parent.getTag() == JCTree.Tag.FOREACHLOOP |
| 114 | + || parent.getTag() == JCTree.Tag.METHODDEF |
| 115 | + || parent.getTag() == JCTree.Tag.CATCH)) { |
| 116 | + path = path.getParentPath(); |
| 117 | + parent = (JCTree) path.getLeaf(); |
| 118 | + } |
| 119 | + if (parent.getTag() == JCTree.Tag.BLOCK |
| 120 | + || parent.getTag() == JCTree.Tag.FORLOOP |
| 121 | + || parent.getTag() == JCTree.Tag.FOREACHLOOP |
| 122 | + || parent.getTag() == JCTree.Tag.CATCH) { |
| 123 | + return blockAnonymousMap.get(parent); |
| 124 | + } else if (parent.getTag() == JCTree.Tag.METHODDEF) { |
| 125 | + JCMethodDecl methodDecl = (JCMethodDecl) parent; |
| 126 | + StringBuilder methodSignature = new StringBuilder(); |
| 127 | + methodDecl.sym.accept(this.signatureGenerator, methodSignature); |
| 128 | + return methodSignature.toString(); |
| 129 | + } else { |
| 130 | + JCClassDecl classDecl = (JCClassDecl) parent; |
| 131 | + StringBuilder classSignature = new StringBuilder(); |
| 132 | + classDecl.sym.accept(this.signatureGenerator, classSignature); |
| 133 | + return classSignature.toString(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + boolean isInBlock(Element e) { |
| 138 | + TreePath tp = signatureGenerator.getPath(e); |
| 139 | + JCTree parent = null; |
| 140 | + do { |
| 141 | + if (tp != null) { |
| 142 | + parent = (JCTree) tp.getLeaf(); |
| 143 | + } else { |
| 144 | + return false; |
| 145 | + } |
| 146 | + tp = tp.getParentPath(); |
| 147 | + } while (parent.getTag() != JCTree.Tag.BLOCK); |
| 148 | + return parent.getTag() == JCTree.Tag.BLOCK; |
| 149 | + } |
| 150 | + |
| 151 | + // Returns the anonymous signature of the corresponding element. Element is supposed to be an |
| 152 | + // anonymous class definition. |
| 153 | + @Nullable String getAnonymousSignature(Element e) { |
| 154 | + TreePath tp = signatureGenerator.getPath(e); |
| 155 | + if (tp == null) { |
| 156 | + return null; |
| 157 | + } |
| 158 | + this.process(tp.getCompilationUnit()); |
| 159 | + return blockAnonymousMap.get(tp.getLeaf()); |
| 160 | + } |
| 161 | + |
| 162 | + private void process(CompilationUnitTree unit) { |
| 163 | + if (!unitProcessed) { |
| 164 | + unitProcessed = true; |
| 165 | + unit.accept(this, new BlockAnonymousData(0, 0, "")); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public Void visitMethod(MethodTree methodTree, BlockAnonymousData blockData) { |
| 171 | + JCMethodDecl methodDecl = (JCMethodDecl) methodTree; |
| 172 | + StringBuilder methodSignature = new StringBuilder(); |
| 173 | + if (methodDecl.sym == null) { |
| 174 | + logger.atInfo().log("methodDecl symbol was null"); |
| 175 | + return null; |
| 176 | + } |
| 177 | + methodDecl.sym.accept(signatureGenerator, methodSignature); |
| 178 | + return super.visitMethod(methodTree, new BlockAnonymousData(-1, 0, methodSignature.toString())); |
| 179 | + } |
| 180 | + |
| 181 | + @Override |
| 182 | + public Void visitClass(ClassTree classTree, BlockAnonymousData blockData) { |
| 183 | + JCClassDecl classDecl = (JCClassDecl) classTree; |
| 184 | + if (classTree.getSimpleName().contentEquals("")) { |
| 185 | + StringBuilder anonymousClassSignature = new StringBuilder(); |
| 186 | + if (classDecl.sym != null) { |
| 187 | + anonymousClassSignature.append(getBlockSignature(classDecl.sym)); |
| 188 | + } else { |
| 189 | + String name = classDecl.name == null ? "" : classDecl.name.toString(); |
| 190 | + logger.atWarning().log( |
| 191 | + "BlockAnonymous class symbol was null: %s#%d", name, blockData.anonymousNumber); |
| 192 | + } |
| 193 | + anonymousClassSignature.append(SignatureGenerator.ANONYMOUS); |
| 194 | + anonymousClassSignature.append("#"); |
| 195 | + anonymousClassSignature.append(blockData.anonymousNumber); |
| 196 | + blockAnonymousMap.put(classDecl, anonymousClassSignature.toString()); |
| 197 | + blockData.anonymousNumber++; |
| 198 | + return super.visitClass( |
| 199 | + classTree, new BlockAnonymousData(0, 0, blockAnonymousMap.get(classDecl))); |
| 200 | + } else { |
| 201 | + StringBuilder classSignature = new StringBuilder(); |
| 202 | + classDecl.sym.accept(this.signatureGenerator, classSignature); |
| 203 | + return super.visitClass(classTree, new BlockAnonymousData(0, 0, classSignature.toString())); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public Void visitEnhancedForLoop( |
| 209 | + EnhancedForLoopTree enhancedForLoopTree, BlockAnonymousData blockData) { |
| 210 | + blockAnonymousMap.put( |
| 211 | + enhancedForLoopTree, blockData.topLevelSignature + ".{}" + (blockData.blockNumber + 1)); |
| 212 | + if (enhancedForLoopTree.getStatement().getKind() != Tree.Kind.BLOCK) { |
| 213 | + blockData.blockNumber++; |
| 214 | + } |
| 215 | + return super.visitEnhancedForLoop(enhancedForLoopTree, blockData); |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public Void visitForLoop(ForLoopTree forLoopTree, BlockAnonymousData blockData) { |
| 220 | + blockAnonymousMap.put( |
| 221 | + forLoopTree, blockData.topLevelSignature + ".{}" + (blockData.blockNumber + 1)); |
| 222 | + if (forLoopTree.getStatement().getKind() != Tree.Kind.BLOCK) { |
| 223 | + blockData.blockNumber++; |
| 224 | + } |
| 225 | + return super.visitForLoop(forLoopTree, blockData); |
| 226 | + } |
| 227 | + |
| 228 | + @Override |
| 229 | + public Void visitCatch(CatchTree catchTree, BlockAnonymousData blockData) { |
| 230 | + blockAnonymousMap.put( |
| 231 | + catchTree, blockData.topLevelSignature + ".{}" + (blockData.blockNumber + 1)); |
| 232 | + return super.visitCatch(catchTree, blockData); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public Void visitBlock(BlockTree b, BlockAnonymousData blockData) { |
| 237 | + blockData.blockNumber++; |
| 238 | + JCBlock block = (JCBlock) b; |
| 239 | + blockAnonymousMap.put(block, blockData.topLevelSignature + ".{}" + blockData.blockNumber); |
| 240 | + BlockAnonymousData newBlockAnonymousData = |
| 241 | + new BlockAnonymousData(blockData.blockNumber, 0, blockData.topLevelSignature); |
| 242 | + super.visitBlock(block, newBlockAnonymousData); |
| 243 | + blockData.blockNumber = newBlockAnonymousData.blockNumber; |
| 244 | + return null; |
| 245 | + } |
| 246 | +} |
0 commit comments