Skip to content

Commit

Permalink
issue91: solve compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Oct 19, 2016
1 parent 3c22c2a commit 341e081
Show file tree
Hide file tree
Showing 33 changed files with 159 additions and 181 deletions.
Expand Up @@ -19,7 +19,7 @@
import com.github.javaparser.JavaParser;
import com.github.javaparser.ParseException;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.imports.ImportDeclaration;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.PackageDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
Expand Down
Expand Up @@ -244,10 +244,10 @@ private MethodUsage toMethodUsage(MethodReferenceExpr methodReferenceExpr) {
throw new UnsupportedOperationException(typeExpr.getType().getClass().getCanonicalName());
}
com.github.javaparser.ast.type.ReferenceType referenceType = (com.github.javaparser.ast.type.ReferenceType)typeExpr.getType();
if (!(referenceType.getType() instanceof ClassOrInterfaceType)) {
throw new UnsupportedOperationException(referenceType.getType().getClass().getCanonicalName());
if (!(referenceType instanceof ClassOrInterfaceType)) {
throw new UnsupportedOperationException(referenceType.getClass().getCanonicalName());
}
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType)referenceType.getType();
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType)referenceType;
SymbolReference<TypeDeclaration> typeDeclarationSymbolReference = JavaParserFactory.getContext(classOrInterfaceType, typeSolver).solveType(classOrInterfaceType.getName(), typeSolver);
if (!typeDeclarationSymbolReference.isSolved()) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -394,10 +394,10 @@ private Type getTypeConcrete(Node node, boolean solveLambdas) {
} else if (node instanceof VariableDeclarator) {
if (node.getParentNode() instanceof FieldDeclaration) {
FieldDeclaration parent = (FieldDeclaration) node.getParentNode();
return JavaParserFacade.get(typeSolver).convertToUsage(parent.getType(), parent);
return JavaParserFacade.get(typeSolver).convertToUsage(parent.getElementType(), parent);
} else if (node.getParentNode() instanceof VariableDeclarationExpr) {
VariableDeclarationExpr parent = (VariableDeclarationExpr) node.getParentNode();
return JavaParserFacade.get(typeSolver).convertToUsage(parent.getType(), parent);
return JavaParserFacade.get(typeSolver).convertToUsage(parent.getElementType(), parent);
} else {
throw new UnsupportedOperationException(node.getParentNode().getClass().getCanonicalName());
}
Expand Down Expand Up @@ -458,10 +458,10 @@ private Type getTypeConcrete(Node node, boolean solveLambdas) {
return getTypeConcrete(unaryExpr.getExpr(), solveLambdas);
case not:
return PrimitiveType.BOOLEAN;
case posIncrement:
case postIncrement:
case preIncrement:
case preDecrement:
case posDecrement:
case postDecrement:
return getTypeConcrete(unaryExpr.getExpr(), solveLambdas);
default:
throw new UnsupportedOperationException(unaryExpr.getOperator().name());
Expand Down Expand Up @@ -489,12 +489,12 @@ private Type getTypeConcrete(Node node, boolean solveLambdas) {
}
} else if (node instanceof VariableDeclarationExpr) {
VariableDeclarationExpr expr = (VariableDeclarationExpr) node;
return convertToUsage(expr.getType(), JavaParserFactory.getContext(node, typeSolver));
return convertToUsage(expr.getElementType(), JavaParserFactory.getContext(node, typeSolver));
} else if (node instanceof InstanceOfExpr) {
return PrimitiveType.BOOLEAN;
} else if (node instanceof EnclosedExpr) {
EnclosedExpr enclosedExpr = (EnclosedExpr) node;
return getTypeConcrete(enclosedExpr.getInner(), solveLambdas);
return getTypeConcrete(enclosedExpr.getInner().get(), solveLambdas);
} else if (node instanceof CastExpr) {
CastExpr enclosedExpr = (CastExpr) node;
return convertToUsage(enclosedExpr.getType(), JavaParserFactory.getContext(node, typeSolver));
Expand All @@ -509,7 +509,7 @@ private Type getTypeConcrete(Node node, boolean solveLambdas) {
} else if (node instanceof ArrayCreationExpr) {
ArrayCreationExpr arrayCreationExpr = (ArrayCreationExpr) node;
Type res = convertToUsage(arrayCreationExpr.getType(), JavaParserFactory.getContext(node, typeSolver));
for (int i=0; i<arrayCreationExpr.getArrayCount();i++) {
for (int i=0; i<arrayCreationExpr.getLevels().size();i++) {
res = new ArrayType(res);
}
return res;
Expand Down Expand Up @@ -554,7 +554,7 @@ public Type convertToUsage(com.github.javaparser.ast.type.Type type, Node contex
private String qName(ClassOrInterfaceType classOrInterfaceType) {
String name = classOrInterfaceType.getName();
if (classOrInterfaceType.getScope() != null) {
return qName(classOrInterfaceType.getScope()) + "." + name;
return qName(classOrInterfaceType.getScope().get()) + "." + name;
} else {
return name;
}
Expand All @@ -563,10 +563,7 @@ private String qName(ClassOrInterfaceType classOrInterfaceType) {
public Type convertToUsage(com.github.javaparser.ast.type.Type type, Context context) {
if (type instanceof com.github.javaparser.ast.type.ReferenceType) {
com.github.javaparser.ast.type.ReferenceType referenceType = (com.github.javaparser.ast.type.ReferenceType) type;
Type typeUsage = convertToUsage(referenceType.getType(), context);
for (int i = 0; i < referenceType.getArrayCount(); i++) {
typeUsage = new ArrayType(typeUsage);
}
Type typeUsage = convertToUsage(referenceType, context);
return typeUsage;
} else if (type instanceof ClassOrInterfaceType) {
ClassOrInterfaceType classOrInterfaceType = (ClassOrInterfaceType) type;
Expand All @@ -577,8 +574,8 @@ public Type convertToUsage(com.github.javaparser.ast.type.Type type, Context con
}
TypeDeclaration typeDeclaration = ref.getCorrespondingDeclaration();
List<Type> typeParameters = Collections.emptyList();
if (classOrInterfaceType.getTypeArgs() != null) {
typeParameters = classOrInterfaceType.getTypeArgs().stream().map((pt) -> convertToUsage(pt, context)).collect(Collectors.toList());
if (classOrInterfaceType.getTypeArguments() != null) {
typeParameters = classOrInterfaceType.getTypeArguments().get().stream().map((pt) -> convertToUsage(pt, context)).collect(Collectors.toList());
}
if (typeDeclaration.isTypeVariable()) {
if (typeDeclaration instanceof TypeParameterDeclaration) {
Expand All @@ -594,11 +591,11 @@ public Type convertToUsage(com.github.javaparser.ast.type.Type type, Context con
return PrimitiveType.byName(((com.github.javaparser.ast.type.PrimitiveType) type).getType().name());
} else if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
if (wildcardType.getExtends() != null && wildcardType.getSuper() == null) {
return Wildcard.extendsBound((ReferenceTypeImpl) convertToUsage(wildcardType.getExtends(), context));
} else if (wildcardType.getExtends() == null && wildcardType.getSuper() != null) {
return Wildcard.extendsBound((ReferenceTypeImpl) convertToUsage(wildcardType.getSuper(), context));
} else if (wildcardType.getExtends() == null && wildcardType.getSuper() == null) {
if (wildcardType.getExtends().isPresent() && wildcardType.getSuper().isPresent()) {
return Wildcard.extendsBound((ReferenceTypeImpl) convertToUsage(wildcardType.getExtends().get(), context));
} else if (!wildcardType.getExtends().isPresent() && wildcardType.getSuper().isPresent()) {
return Wildcard.extendsBound((ReferenceTypeImpl) convertToUsage(wildcardType.getSuper().get(), context));
} else if (wildcardType.getExtends().isPresent() && !wildcardType.getSuper().isPresent()) {
return Wildcard.UNBOUNDED;
} else {
throw new UnsupportedOperationException();
Expand Down
Expand Up @@ -109,7 +109,7 @@ public Optional<Value> solveSymbolAsValue(String name, TypeSolver typeSolver) {

@Override
public Optional<Type> solveGenericType(String name, TypeSolver typeSolver) {
for (com.github.javaparser.ast.TypeParameter tp : wrappedNode.getTypeParameters()) {
for (com.github.javaparser.ast.type.TypeParameter tp : wrappedNode.getTypeParameters()) {
if (tp.getName().equals(name)) {
return Optional.of(new TypeVariable(new JavaParserTypeParameter(tp, typeSolver)));
}
Expand Down
Expand Up @@ -17,17 +17,17 @@
package me.tomassetti.symbolsolver.javaparsermodel.contexts;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.TypeDeclaration;
import com.github.javaparser.ast.expr.QualifiedNameExpr;
import com.github.javaparser.ast.imports.*;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
import me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration;
import me.tomassetti.symbolsolver.model.declarations.MethodDeclaration;
import me.tomassetti.symbolsolver.model.declarations.ValueDeclaration;
import me.tomassetti.symbolsolver.model.resolution.SymbolReference;
import me.tomassetti.symbolsolver.model.resolution.TypeSolver;
import me.tomassetti.symbolsolver.model.usages.typesystem.Type;
import me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import me.tomassetti.symbolsolver.javaparsermodel.declarations.JavaParserInterfaceDeclaration;
import me.tomassetti.symbolsolver.resolution.MethodResolutionLogic;
import me.tomassetti.symbolsolver.resolution.SymbolSolver;

Expand Down Expand Up @@ -63,32 +63,24 @@ public SymbolReference<? extends ValueDeclaration> solveSymbol(String name, Type
// Look among statically imported values
if (wrappedNode.getImports() != null) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (importDecl.isStatic()) {
if (importDecl.isAsterisk()) {
if (importDecl.getName() instanceof QualifiedNameExpr) {
String qName = importDecl.getName().toString();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration importedType = typeSolver.solveType(qName);
SymbolReference<? extends ValueDeclaration> ref = new SymbolSolver(typeSolver).solveSymbolInType(importedType, name);
if (ref.isSolved()) {
return ref;
}
} else {
throw new UnsupportedOperationException("B");
}
} else {
if (importDecl.getName() instanceof QualifiedNameExpr) {
String qName = importDecl.getName().toString();
// split in field/method name and type name
String typeName = getType(qName);
String memberName = getMember(qName);

if (memberName.equals(name)) {
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration importedType = typeSolver.solveType(typeName);
return new SymbolSolver(typeSolver).solveSymbolInType(importedType, memberName);
}
} else {
throw new UnsupportedOperationException("C");
}
if (importDecl instanceof StaticImportOnDemandDeclaration) {
ClassOrInterfaceType classOrInterfaceType = ((StaticImportOnDemandDeclaration) importDecl).getType();
String qName = classOrInterfaceType.getName();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration importedType = typeSolver.solveType(qName);
SymbolReference<? extends ValueDeclaration> ref = new SymbolSolver(typeSolver).solveSymbolInType(importedType, name);
if (ref.isSolved()) {
return ref;
}
} else if (importDecl instanceof SingleStaticImportDeclaration){
ClassOrInterfaceType classOrInterfaceType = ((StaticImportOnDemandDeclaration) importDecl).getType();
String qName = classOrInterfaceType.getName();
// split in field/method name and type name
String typeName = getType(qName);
String memberName = getMember(qName);

if (memberName.equals(name)) {
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration importedType = typeSolver.solveType(typeName);
return new SymbolSolver(typeSolver).solveSymbolInType(importedType, memberName);
}
}
}
Expand Down Expand Up @@ -135,33 +127,29 @@ public SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclara

if (wrappedNode.getImports() != null) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (!importDecl.isStatic()) {
if (!importDecl.isAsterisk()) {
if (importDecl.getName() instanceof QualifiedNameExpr) {
String qName = importDecl.getName().toString();
if (qName.equals(name) || qName.endsWith("." + name)) {
SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return ref;
}
}
} else {
throw new UnsupportedOperationException();
}
} else {
String qName = importDecl.getName().toString() + "." + name;
if (importDecl instanceof SingleTypeImportDeclaration) {
ClassOrInterfaceType importedType = ((SingleTypeImportDeclaration)importDecl).getType();
String qName = importedType.getName();
if (qName.equals(name) || qName.endsWith("." + name)) {
SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return ref;
}
}
} else if (importDecl instanceof TypeImportOnDemandDeclaration) {
String packageName = ((TypeImportOnDemandDeclaration)importDecl).getName().getQualifiedName();
String qName = packageName + "." + name;
SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return ref;
}
}
}
}

// Look in current package
if (this.wrappedNode.getPackage() != null) {
String qName = this.wrappedNode.getPackage().getName().toString() + "." + name;
String qName = this.wrappedNode.getPackage().get().getName().toString() + "." + name;
SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclaration> ref = typeSolver.tryToSolveType(qName);
if (ref.isSolved()) {
return ref;
Expand All @@ -186,31 +174,25 @@ public SymbolReference<me.tomassetti.symbolsolver.model.declarations.TypeDeclara
public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> argumentsTypes, TypeSolver typeSolver) {
if (wrappedNode.getImports() != null) {
for (ImportDeclaration importDecl : wrappedNode.getImports()) {
if (importDecl.isStatic()) {
if (importDecl.isAsterisk()) {
if (importDecl.getName() instanceof QualifiedNameExpr) {
String qName = importDecl.getName().toString();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(qName);
SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, typeSolver);
if (method.isSolved()) {
return method;
}
} else {
throw new UnsupportedOperationException();
}
} else {
if (importDecl.getName() instanceof QualifiedNameExpr) {
String qName = importDecl.getName().toString();
if (qName.equals(name) || qName.endsWith("." + name)) {
String typeName = getType(qName);
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(typeName);
SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, typeSolver);
if (method.isSolved()) {
return method;
}
}
} else {
throw new UnsupportedOperationException();
if (importDecl instanceof StaticImportOnDemandDeclaration) {
StaticImportOnDemandDeclaration staticImportOnDemandDeclaration = (StaticImportOnDemandDeclaration)importDecl;

String qName = staticImportOnDemandDeclaration.getType().getName();
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(qName);
SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, typeSolver);
if (method.isSolved()) {
return method;
}
} else if (importDecl instanceof SingleStaticImportDeclaration) {
SingleStaticImportDeclaration staticImportOnDemandDeclaration = (SingleStaticImportDeclaration)importDecl;

String qName = staticImportOnDemandDeclaration.getType().getName();
if (qName.equals(name) || qName.endsWith("." + name)) {
String typeName = getType(qName);
me.tomassetti.symbolsolver.model.declarations.TypeDeclaration ref = typeSolver.solveType(typeName);
SymbolReference<MethodDeclaration> method = MethodResolutionLogic.solveMethodInType(ref, name, argumentsTypes, typeSolver);
if (method.isSolved()) {
return method;
}
}
}
Expand Down

0 comments on commit 341e081

Please sign in to comment.