24 */ 25 26 package jdk.jshell; 27 28 29 import com.sun.source.tree.ClassTree; 30 import com.sun.source.tree.CompilationUnitTree; 31 import com.sun.source.tree.ExpressionTree; 32 import com.sun.source.tree.MethodTree; 33 import com.sun.source.tree.ReturnTree; 34 import com.sun.source.tree.StatementTree; 35 import com.sun.source.tree.Tree; 36 import com.sun.source.tree.VariableTree; 37 import com.sun.source.util.SourcePositions; 38 import com.sun.source.util.TreePath; 39 import com.sun.source.util.Trees; 40 import com.sun.tools.javac.code.Type; 41 import com.sun.tools.javac.code.Type.MethodType; 42 import com.sun.tools.javac.code.Types; 43 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 44 import com.sun.tools.javac.util.JavacMessages; 45 import com.sun.tools.javac.util.Name; 46 import static jdk.jshell.Util.isDoIt; 47 import jdk.jshell.Wrap.Range; 48 import java.util.List; 49 import java.util.Locale; 50 import java.util.function.BinaryOperator; 51 import java.util.function.Predicate; 52 import java.util.stream.Stream; 53 import javax.lang.model.type.TypeMirror; 54 import jdk.jshell.Util.Pair; 55 56 /** 57 * Utilities for analyzing compiler API parse trees. 58 * @author Robert Field 59 */ 60 61 class TreeDissector { 62 63 private static final String OBJECT_TYPE = "Object"; 64 65 static class ExpressionInfo { 66 67 boolean isNonVoid; 68 String typeName; 69 ExpressionTree tree; 70 String signature; 71 } 72 73 private final TaskFactory.BaseTask bt; 74 private final ClassTree targetClass; 75 private final CompilationUnitTree targetCompilationUnit; 192 } 193 } 194 } 195 } 196 return null; 197 } 198 199 VariableTree firstVariable() { 200 if (targetClass != null) { 201 for (Tree mem : targetClass.getMembers()) { 202 if (mem.getKind() == Tree.Kind.VARIABLE) { 203 VariableTree vt = (VariableTree) mem; 204 return vt; 205 } 206 } 207 } 208 return null; 209 } 210 211 212 ExpressionInfo typeOfReturnStatement(JavacMessages messages, BinaryOperator<String> fullClassNameAndPackageToClass) { 213 ExpressionInfo ei = new ExpressionInfo(); 214 Tree unitTree = firstStatement(); 215 if (unitTree instanceof ReturnTree) { 216 ei.tree = ((ReturnTree) unitTree).getExpression(); 217 if (ei.tree != null) { 218 TreePath viPath = trees().getPath(targetCompilationUnit, ei.tree); 219 if (viPath != null) { 220 TypeMirror tm = trees().getTypeMirror(viPath); 221 if (tm != null) { 222 Type type = (Type)tm; 223 TypePrinter tp = new TypePrinter(messages, fullClassNameAndPackageToClass, type); 224 ei.typeName = tp.visit(type, Locale.getDefault()); 225 switch (tm.getKind()) { 226 case VOID: 227 case NONE: 228 case ERROR: 229 case OTHER: 230 break; 231 case NULL: 232 ei.isNonVoid = true; 233 ei.typeName = OBJECT_TYPE; 234 break; 235 default: { 236 ei.isNonVoid = true; 237 break; 238 239 } 240 } 241 } 242 } 243 } 244 } | 24 */ 25 26 package jdk.jshell; 27 28 29 import com.sun.source.tree.ClassTree; 30 import com.sun.source.tree.CompilationUnitTree; 31 import com.sun.source.tree.ExpressionTree; 32 import com.sun.source.tree.MethodTree; 33 import com.sun.source.tree.ReturnTree; 34 import com.sun.source.tree.StatementTree; 35 import com.sun.source.tree.Tree; 36 import com.sun.source.tree.VariableTree; 37 import com.sun.source.util.SourcePositions; 38 import com.sun.source.util.TreePath; 39 import com.sun.source.util.Trees; 40 import com.sun.tools.javac.code.Type; 41 import com.sun.tools.javac.code.Type.MethodType; 42 import com.sun.tools.javac.code.Types; 43 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; 44 import com.sun.tools.javac.util.Name; 45 import static jdk.jshell.Util.isDoIt; 46 import jdk.jshell.Wrap.Range; 47 48 import java.util.List; 49 50 import java.util.function.Predicate; 51 import java.util.stream.Stream; 52 import javax.lang.model.type.TypeMirror; 53 import jdk.jshell.Util.Pair; 54 55 import jdk.jshell.TaskFactory.AnalyzeTask; 56 57 /** 58 * Utilities for analyzing compiler API parse trees. 59 * @author Robert Field 60 */ 61 62 class TreeDissector { 63 64 private static final String OBJECT_TYPE = "Object"; 65 66 static class ExpressionInfo { 67 68 boolean isNonVoid; 69 String typeName; 70 ExpressionTree tree; 71 String signature; 72 } 73 74 private final TaskFactory.BaseTask bt; 75 private final ClassTree targetClass; 76 private final CompilationUnitTree targetCompilationUnit; 193 } 194 } 195 } 196 } 197 return null; 198 } 199 200 VariableTree firstVariable() { 201 if (targetClass != null) { 202 for (Tree mem : targetClass.getMembers()) { 203 if (mem.getKind() == Tree.Kind.VARIABLE) { 204 VariableTree vt = (VariableTree) mem; 205 return vt; 206 } 207 } 208 } 209 return null; 210 } 211 212 213 ExpressionInfo typeOfReturnStatement(AnalyzeTask at, JShell state) { 214 ExpressionInfo ei = new ExpressionInfo(); 215 Tree unitTree = firstStatement(); 216 if (unitTree instanceof ReturnTree) { 217 ei.tree = ((ReturnTree) unitTree).getExpression(); 218 if (ei.tree != null) { 219 TreePath viPath = trees().getPath(targetCompilationUnit, ei.tree); 220 if (viPath != null) { 221 TypeMirror tm = trees().getTypeMirror(viPath); 222 if (tm != null) { 223 ei.typeName = TypePrinter.printType(at, state, tm); 224 switch (tm.getKind()) { 225 case VOID: 226 case NONE: 227 case ERROR: 228 case OTHER: 229 break; 230 case NULL: 231 ei.isNonVoid = true; 232 ei.typeName = OBJECT_TYPE; 233 break; 234 default: { 235 ei.isNonVoid = true; 236 break; 237 238 } 239 } 240 } 241 } 242 } 243 } |