1 /* 2 * Copyright 2016 Federico Tomassetti 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.github.javaparser.symbolsolver.resolution; 18 19 import com.github.javaparser.ast.Node; 20 import com.github.javaparser.ast.type.ClassOrInterfaceType; 21 import com.github.javaparser.resolution.MethodUsage; 22 import com.github.javaparser.resolution.UnsolvedSymbolException; 23 import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration; 24 import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration; 25 import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration; 26 import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration; 27 import com.github.javaparser.resolution.types.ResolvedType; 28 import com.github.javaparser.symbolsolver.core.resolution.Context; 29 import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFactory; 30 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration; 31 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserEnumDeclaration; 32 import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration; 33 import com.github.javaparser.symbolsolver.javassistmodel.JavassistClassDeclaration; 34 import com.github.javaparser.symbolsolver.javassistmodel.JavassistEnumDeclaration; 35 import com.github.javaparser.symbolsolver.javassistmodel.JavassistInterfaceDeclaration; 36 import com.github.javaparser.symbolsolver.model.resolution.SymbolReference; 37 import com.github.javaparser.symbolsolver.model.resolution.TypeSolver; 38 import com.github.javaparser.symbolsolver.model.resolution.Value; 39 import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl; 40 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration; 41 import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionInterfaceDeclaration; 42 43 import java.util.List; 44 import java.util.Optional; 45 46 /** 47 * @author Federico Tomassetti 48 */ 49 public class SymbolSolver { 50 51 private TypeSolver typeSolver; 52 53 public SymbolSolver(TypeSolver typeSolver) { 54 if (typeSolver == null) throw new IllegalArgumentException(); 55 56 this.typeSolver = typeSolver; 57 } 58 59 public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, Context context) { 60 return context.solveSymbol(name, typeSolver); 61 } 62 63 public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, Node node) { 64 return solveSymbol(name, JavaParserFactory.getContext(node, typeSolver)); 65 } 66 67 public Optional<Value> solveSymbolAsValue(String name, Context context) { 68 return context.solveSymbolAsValue(name, typeSolver); 69 } 70 71 public Optional<Value> solveSymbolAsValue(String name, Node node) { 72 Context context = JavaParserFactory.getContext(node, typeSolver); 73 return solveSymbolAsValue(name, context); 74 } 75 76 public SymbolReference<? extends ResolvedTypeDeclaration> solveType(String name, Context context) { 77 return context.solveType(name, typeSolver); 78 } 79 80 public SymbolReference<? extends ResolvedTypeDeclaration> solveType(String name, Node node) { 81 return solveType(name, JavaParserFactory.getContext(node, typeSolver)); 82 } 83 84 public MethodUsage solveMethod(String methodName, List<ResolvedType> argumentsTypes, Context context) { 85 SymbolReference<ResolvedMethodDeclaration> decl = context.solveMethod(methodName, argumentsTypes, false, typeSolver); 86 if (!decl.isSolved()) { 87 throw new UnsolvedSymbolException(context.toString(), methodName); 88 } 89 return new MethodUsage(decl.getCorrespondingDeclaration()); 90 } 91 92 public MethodUsage solveMethod(String methodName, List<ResolvedType> argumentsTypes, Node node) { 93 return solveMethod(methodName, argumentsTypes, JavaParserFactory.getContext(node, typeSolver)); 94 } 95 96 public ResolvedTypeDeclaration solveType(com.github.javaparser.ast.type.Type type) { 97 if (type instanceof ClassOrInterfaceType) { 98 99 // FIXME should call typesolver here! 100 101 String name = ((ClassOrInterfaceType) type).getName().getId(); 102 SymbolReference<ResolvedTypeDeclaration> ref = JavaParserFactory.getContext(type, typeSolver).solveType(name, typeSolver); 103 if (!ref.isSolved()) { 104 throw new UnsolvedSymbolException(JavaParserFactory.getContext(type, typeSolver).toString(), name); 105 } 106 return ref.getCorrespondingDeclaration(); 107 } else { 108 throw new UnsupportedOperationException(type.getClass().getCanonicalName()); 109 } 110 } 111 112 public ResolvedType solveTypeUsage(String name, Context context) { 113 Optional<ResolvedType> genericType = context.solveGenericType(name, typeSolver); 114 if (genericType.isPresent()) { 115 return genericType.get(); 116 } 117 ResolvedReferenceTypeDeclaration typeDeclaration = typeSolver.solveType(name); 118 return new ReferenceTypeImpl(typeDeclaration, typeSolver); 119 } 120 121 /** 122 * Solve any possible visible symbols including: fields, internal types, type variables, the type itself or its 123 * containers. 124 * <p> 125 * It should contain its own private fields but not inherited private fields. 126 */ 127 public SymbolReference<? extends ResolvedValueDeclaration> solveSymbolInType(ResolvedTypeDeclaration typeDeclaration, String name) { 128 if (typeDeclaration instanceof JavaParserClassDeclaration) { 129 Context ctx = ((JavaParserClassDeclaration) typeDeclaration).getContext(); 130 return ctx.solveSymbol(name, typeSolver); 131 } 132 if (typeDeclaration instanceof JavaParserInterfaceDeclaration) { 133 Context ctx = ((JavaParserInterfaceDeclaration) typeDeclaration).getContext(); 134 return ctx.solveSymbol(name, typeSolver); 135 } 136 if (typeDeclaration instanceof JavaParserEnumDeclaration) { 137 Context ctx = ((JavaParserEnumDeclaration) typeDeclaration).getContext(); 138 return ctx.solveSymbol(name, typeSolver); 139 } 140 if (typeDeclaration instanceof ReflectionClassDeclaration) { 141 return ((ReflectionClassDeclaration) typeDeclaration).solveSymbol(name, typeSolver); 142 } 143 if (typeDeclaration instanceof ReflectionInterfaceDeclaration) { 144 return ((ReflectionInterfaceDeclaration) typeDeclaration).solveSymbol(name, typeSolver); 145 } 146 if (typeDeclaration instanceof JavassistClassDeclaration) { 147 return ((JavassistClassDeclaration) typeDeclaration).solveSymbol(name, typeSolver); 148 } 149 if (typeDeclaration instanceof JavassistEnumDeclaration) { 150 return ((JavassistEnumDeclaration) typeDeclaration).solveSymbol(name, typeSolver); 151 } 152 if (typeDeclaration instanceof JavassistInterfaceDeclaration) { 153 return ((JavassistInterfaceDeclaration) typeDeclaration).solveSymbol(name, typeSolver); 154 } 155 return SymbolReference.unsolved(ResolvedValueDeclaration.class); 156 } 157 158 /** 159 * Try to solve a symbol just in the declaration, it does not delegate to the container. 160 */ 161 @Deprecated 162 public SymbolReference<ResolvedTypeDeclaration> solveTypeInType(ResolvedTypeDeclaration typeDeclaration, String name) { 163 if (typeDeclaration instanceof JavaParserClassDeclaration) { 164 return ((JavaParserClassDeclaration) typeDeclaration).solveType(name, typeSolver); 165 } 166 if (typeDeclaration instanceof JavaParserInterfaceDeclaration) { 167 return ((JavaParserInterfaceDeclaration) typeDeclaration).solveType(name, typeSolver); 168 } 169 return SymbolReference.unsolved(ResolvedReferenceTypeDeclaration.class); 170 } 171 } 172