use of com.github.javaparser.symbolsolver.resolution.SymbolSolver in project javaparser by javaparser.
the class JavaParserTypeAdapter method solveType.
public SymbolReference<ResolvedTypeDeclaration> solveType(String name, TypeSolver typeSolver) {
if (wrappedNode instanceof NodeWithTypeParameters<?>) {
NodeList<TypeParameter> typeParameters = ((NodeWithTypeParameters<?>) wrappedNode).getTypeParameters();
for (com.github.javaparser.ast.type.TypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().getId().equals(name)) {
return SymbolReference.solved(new JavaParserTypeVariableDeclaration(typeParameter, typeSolver));
}
}
}
// Internal classes
for (BodyDeclaration<?> member : this.wrappedNode.getMembers()) {
if (member instanceof com.github.javaparser.ast.body.TypeDeclaration) {
com.github.javaparser.ast.body.TypeDeclaration<?> internalType = (com.github.javaparser.ast.body.TypeDeclaration<?>) member;
String prefix = internalType.getName() + ".";
if (internalType.getName().getId().equals(name)) {
if (internalType instanceof ClassOrInterfaceDeclaration) {
return SymbolReference.solved(new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver));
} else if (internalType instanceof EnumDeclaration) {
return SymbolReference.solved(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver));
} else {
throw new UnsupportedOperationException();
}
} else if (name.startsWith(prefix) && name.length() > prefix.length()) {
if (internalType instanceof ClassOrInterfaceDeclaration) {
return new JavaParserClassDeclaration((com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) internalType, typeSolver).solveType(name.substring(prefix.length()), typeSolver);
} else if (internalType instanceof EnumDeclaration) {
return new SymbolSolver(typeSolver).solveTypeInType(new JavaParserEnumDeclaration((com.github.javaparser.ast.body.EnumDeclaration) internalType, typeSolver), name.substring(prefix.length()));
} else {
throw new UnsupportedOperationException();
}
}
}
}
return SymbolReference.unsolved(ResolvedTypeDeclaration.class);
}
use of com.github.javaparser.symbolsolver.resolution.SymbolSolver in project javaparser by javaparser.
the class TypeExtractor method visit.
@Override
public ResolvedType visit(NameExpr node, Boolean solveLambdas) {
logger.finest("getType on name expr " + node);
Optional<Value> value = new SymbolSolver(typeSolver).solveSymbolAsValue(node.getName().getId(), node);
if (!value.isPresent()) {
throw new com.github.javaparser.resolution.UnsolvedSymbolException("Solving " + node, node.getName().getId());
} else {
return value.get().getType();
}
}
use of com.github.javaparser.symbolsolver.resolution.SymbolSolver in project javaparser by javaparser.
the class CompilationUnitContext method solveSymbol.
// /
// / Public methods
// /
@Override
public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String name, TypeSolver typeSolver) {
// solve absolute references
String itName = name;
while (itName.contains(".")) {
String typeName = getType(itName);
String memberName = getMember(itName);
SymbolReference<ResolvedTypeDeclaration> type = this.solveType(typeName, typeSolver);
if (type.isSolved()) {
return new SymbolSolver(typeSolver).solveSymbolInType(type.getCorrespondingDeclaration(), memberName);
} else {
itName = typeName;
}
}
// Look among statically imported values
if (wrappedNode.getImports() != null) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (importDecl.isStatic()) {
if (importDecl.isAsterisk()) {
String qName = importDecl.getNameAsString();
ResolvedTypeDeclaration importedType = typeSolver.solveType(qName);
SymbolReference<? extends ResolvedValueDeclaration> ref = new SymbolSolver(typeSolver).solveSymbolInType(importedType, name);
if (ref.isSolved()) {
return ref;
}
} else {
String whole = importDecl.getNameAsString();
// split in field/method name and type name
String memberName = getMember(whole);
String typeName = getType(whole);
if (memberName.equals(name)) {
ResolvedTypeDeclaration importedType = typeSolver.solveType(typeName);
return new SymbolSolver(typeSolver).solveSymbolInType(importedType, memberName);
}
}
}
}
}
return SymbolReference.unsolved(ResolvedValueDeclaration.class);
}
use of com.github.javaparser.symbolsolver.resolution.SymbolSolver in project javaparser by javaparser.
the class JavaParserEnumDeclaration method getAncestors.
@Override
public List<ResolvedReferenceType> getAncestors() {
List<ResolvedReferenceType> ancestors = new ArrayList<>();
ResolvedReferenceType enumClass = ReflectionFactory.typeUsageFor(Enum.class, typeSolver).asReferenceType();
ResolvedTypeParameterDeclaration eTypeParameter = enumClass.getTypeDeclaration().getTypeParameters().get(0);
enumClass = enumClass.deriveTypeParameters(new ResolvedTypeParametersMap.Builder().setValue(eTypeParameter, new ReferenceTypeImpl(this, typeSolver)).build());
ancestors.add(enumClass);
if (wrappedNode.getImplementedTypes() != null) {
for (ClassOrInterfaceType implementedType : wrappedNode.getImplementedTypes()) {
SymbolReference<ResolvedTypeDeclaration> implementedDeclRef = new SymbolSolver(typeSolver).solveTypeInType(this, implementedType.getName().getId());
if (!implementedDeclRef.isSolved()) {
throw new UnsolvedSymbolException(implementedType.getName().getId());
}
ancestors.add(new ReferenceTypeImpl((ResolvedReferenceTypeDeclaration) implementedDeclRef.getCorrespondingDeclaration(), typeSolver));
}
}
return ancestors;
}
use of com.github.javaparser.symbolsolver.resolution.SymbolSolver in project javaparser by javaparser.
the class JavassistUtils method parseTypeParameters.
private static List<ResolvedType> parseTypeParameters(String signature, TypeSolver typeSolver, Context invokationContext) {
String originalSignature = signature;
if (signature.contains("<")) {
signature = signature.substring(signature.indexOf('<') + 1);
if (!signature.endsWith(">")) {
throw new IllegalArgumentException();
}
signature = signature.substring(0, signature.length() - 1);
if (signature.contains(",")) {
throw new UnsupportedOperationException();
}
if (signature.startsWith("?")) {
// TODO: check bounds
List<ResolvedType> types = new ArrayList<>();
types.add(ResolvedWildcard.UNBOUNDED);
return types;
}
List<ResolvedType> typeParameters = parseTypeParameters(signature, typeSolver, invokationContext);
if (signature.contains("<")) {
signature = signature.substring(0, signature.indexOf('<'));
}
if (signature.contains(">")) {
throw new UnsupportedOperationException();
}
ResolvedType type = new SymbolSolver(typeSolver).solveTypeUsage(signature, invokationContext);
if (type.isReferenceType() && typeParameters.size() > 0) {
type = type.asReferenceType().transformTypeParameters(tp -> typeParameters.remove(0));
}
List<ResolvedType> types = new ArrayList<>();
types.add(type);
return types;
} else {
return Collections.emptyList();
}
}
Aggregations