Skip to content

Commit

Permalink
correcting module core to use new classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ftomassetti committed Sep 29, 2017
1 parent 5d8d0cb commit e15d5a8
Show file tree
Hide file tree
Showing 35 changed files with 399 additions and 449 deletions.
Expand Up @@ -28,12 +28,13 @@
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.ImportDeclaration;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedTypeDeclaration;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade;
import com.github.javaparser.symbolsolver.model.declarations.TypeDeclaration;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -97,13 +98,13 @@ public int getKo() {
}

private void solveTypeDecl(ClassOrInterfaceDeclaration node) {
TypeDeclaration typeDeclaration = JavaParserFacade.get(typeSolver).getTypeDeclaration(node);
ResolvedTypeDeclaration typeDeclaration = JavaParserFacade.get(typeSolver).getTypeDeclaration(node);
if (typeDeclaration.isClass()) {
out.println("\n[ Class " + typeDeclaration.getQualifiedName() + " ]");
for (ReferenceType sc : typeDeclaration.asClass().getAllSuperClasses()) {
for (ResolvedReferenceType sc : typeDeclaration.asClass().getAllSuperClasses()) {
out.println(" superclass: " + sc.getQualifiedName());
}
for (ReferenceType sc : typeDeclaration.asClass().getAllInterfaces()) {
for (ResolvedReferenceType sc : typeDeclaration.asClass().getAllInterfaces()) {
out.println(" interface: " + sc.getQualifiedName());
}
}
Expand All @@ -119,7 +120,7 @@ private void solve(Node node) {
// skip
} else if ((getParentNode(node) instanceof Statement) || (getParentNode(node) instanceof VariableDeclarator)) {
try {
Type ref = JavaParserFacade.get(typeSolver).getType(node);
ResolvedType ref = JavaParserFacade.get(typeSolver).getType(node);
out.println(" Line " + node.getRange().get().begin.line + ") " + node + " ==> " + ref.describe());
ok++;
} catch (UnsupportedOperationException upe) {
Expand Down Expand Up @@ -156,7 +157,7 @@ private String toString(MethodCallExpr node) {
}
}

private String toString(SymbolReference<com.github.javaparser.symbolsolver.model.declarations.MethodDeclaration> methodDeclarationSymbolReference) {
private String toString(SymbolReference<ResolvedMethodDeclaration> methodDeclarationSymbolReference) {
if (methodDeclarationSymbolReference.isSolved()) {
return methodDeclarationSymbolReference.getCorrespondingDeclaration().getQualifiedSignature();
} else {
Expand Down
Expand Up @@ -19,20 +19,16 @@
import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.ast.Node;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedValueDeclaration;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.core.resolution.Context;
import com.github.javaparser.symbolsolver.javaparsermodel.LambdaArgumentTypePlaceholder;
import com.github.javaparser.symbolsolver.logic.AbstractClassDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.methods.MethodUsage;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;
import javassist.CtClass;
import javassist.CtField;
Expand Down Expand Up @@ -70,7 +66,7 @@ public JavassistClassDeclaration(CtClass ctClass, TypeSolver typeSolver) {
}

@Override
protected ReferenceType object() {
protected ResolvedReferenceType object() {
return new ReferenceTypeImpl(typeSolver.solveType(Object.class.getCanonicalName()), typeSolver);
}

Expand All @@ -80,7 +76,7 @@ public boolean hasDirectlyAnnotation(String canonicalName) {
}

@Override
public Set<MethodDeclaration> getDeclaredMethods() {
public Set<ResolvedMethodDeclaration> getDeclaredMethods() {
return javassistTypeDeclarationAdapter.getDeclaredMethods();
}

Expand Down Expand Up @@ -161,12 +157,12 @@ public SymbolReference<? extends ResolvedValueDeclaration> solveSymbol(String na
throw new RuntimeException(e);
}

return SymbolReference.unsolved(ValueDeclaration.class);
return SymbolReference.unsolved(ResolvedValueDeclaration.class);
}

@Override
public List<ReferenceType> getAncestors() {
List<ReferenceType> ancestors = new LinkedList<>();
public List<ResolvedReferenceType> getAncestors() {
List<ResolvedReferenceType> ancestors = new LinkedList<>();
if (getSuperClass() != null) {
ancestors.add(getSuperClass());
}
Expand All @@ -175,8 +171,8 @@ public List<ReferenceType> getAncestors() {
}

@Deprecated
public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> argumentsTypes, boolean staticOnly) {
List<MethodDeclaration> candidates = new ArrayList<>();
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
List<ResolvedMethodDeclaration> candidates = new ArrayList<>();
Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (CtMethod method : ctClass.getDeclaredMethods()) {
boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
Expand All @@ -189,7 +185,7 @@ public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> ar
try {
CtClass superClass = ctClass.getSuperclass();
if (superClass != null) {
SymbolReference<MethodDeclaration> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
SymbolReference<ResolvedMethodDeclaration> ref = new JavassistClassDeclaration(superClass, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
if (ref.isSolved()) {
candidates.add(ref.getCorrespondingDeclaration());
}
Expand All @@ -200,7 +196,7 @@ public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> ar

try {
for (CtClass interfaze : ctClass.getInterfaces()) {
SymbolReference<MethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
SymbolReference<ResolvedMethodDeclaration> ref = new JavassistInterfaceDeclaration(interfaze, typeSolver).solveMethod(name, argumentsTypes, staticOnly);
if (ref.isSolved()) {
candidates.add(ref.getCorrespondingDeclaration());
}
Expand All @@ -212,12 +208,12 @@ public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> ar
return MethodResolutionLogic.findMostApplicable(candidates, name, argumentsTypes, typeSolver);
}

public Type getUsage(Node node) {
public ResolvedType getUsage(Node node) {
return new ReferenceTypeImpl(this, typeSolver);
}

@Override
public boolean isAssignableBy(Type type) {
public boolean isAssignableBy(ResolvedType type) {
if (type.isNull()) {
return true;
}
Expand Down Expand Up @@ -252,7 +248,7 @@ public boolean isTypeParameter() {
}

@Override
public List<FieldDeclaration> getAllFields() {
public List<ResolvedFieldDeclaration> getAllFields() {
return javassistTypeDeclarationAdapter.getDeclaredFields();
}

Expand Down Expand Up @@ -283,7 +279,7 @@ public boolean isClass() {
}

@Override
public ReferenceType getSuperClass() {
public ResolvedReferenceType getSuperClass() {
try {
if (ctClass.getSuperclass() == null) {
return new ReferenceTypeImpl(typeSolver.solveType(Object.class.getCanonicalName()), typeSolver);
Expand All @@ -302,7 +298,7 @@ public ReferenceType getSuperClass() {
}

@Override
public List<ReferenceType> getInterfaces() {
public List<ResolvedReferenceType> getInterfaces() {
try {
if (ctClass.getGenericSignature() == null) {
return Arrays.stream(ctClass.getInterfaces())
Expand Down Expand Up @@ -333,17 +329,17 @@ public String toString() {
}

@Override
public List<TypeParameterDeclaration> getTypeParameters() {
public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
return javassistTypeDeclarationAdapter.getTypeParameters();
}

@Override
public AccessSpecifier accessLevel() {
public AccessSpecifier accessSpecifier() {
return JavassistFactory.modifiersToAccessLevel(ctClass.getModifiers());
}

@Override
public List<ConstructorDeclaration> getConstructors() {
public List<ResolvedConstructorDeclaration> getConstructors() {
return javassistTypeDeclarationAdapter.getConstructors();
}

Expand Down Expand Up @@ -371,7 +367,7 @@ public ResolvedReferenceTypeDeclaration getInternalType(String name) {
The name of the ReferenceTypeDeclaration could be composed of the internal class and the outer class, e.g. A$B. That's why we search the internal type in the ending part.
In case the name is composed of the internal type only, i.e. f.getName() returns B, it will also works.
*/
Optional<ReferenceTypeDeclaration> type =
Optional<ResolvedReferenceTypeDeclaration> type =
this.internalTypes().stream().filter(f -> f.getName().endsWith(name)).findFirst();
return type.orElseThrow(() ->
new UnsolvedSymbolException("Internal type not found: " + name));
Expand Down
Expand Up @@ -17,9 +17,9 @@
package com.github.javaparser.symbolsolver.javassistmodel;

import com.github.javaparser.ast.AccessSpecifier;
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import javassist.CtConstructor;
import javassist.NotFoundException;
import javassist.bytecode.BadBytecode;
Expand All @@ -33,7 +33,7 @@
/**
* @author Fred Lefévère-Laoide
*/
public class JavassistConstructorDeclaration implements ConstructorDeclaration {
public class JavassistConstructorDeclaration implements ResolvedConstructorDeclaration {
private CtConstructor ctConstructor;
private TypeSolver typeSolver;

Expand Down Expand Up @@ -70,7 +70,7 @@ public boolean isType() {
}

@Override
public ClassDeclaration declaringType() {
public ResolvedClassDeclaration declaringType() {
return new JavassistClassDeclaration(ctConstructor.getDeclaringClass(), typeSolver);
}

Expand All @@ -84,7 +84,7 @@ public int getNumberOfParams() {
}

@Override
public ParameterDeclaration getParam(int i) {
public ResolvedParameterDeclaration getParam(int i) {
try {
boolean variadic = false;
if ((ctConstructor.getModifiers() & javassist.Modifier.VARARGS) > 0) {
Expand All @@ -105,7 +105,7 @@ public ParameterDeclaration getParam(int i) {
}

@Override
public List<TypeParameterDeclaration> getTypeParameters() {
public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
try {
if (ctConstructor.getGenericSignature() == null) {
return Collections.emptyList();
Expand All @@ -118,7 +118,7 @@ public List<TypeParameterDeclaration> getTypeParameters() {
}

@Override
public AccessSpecifier accessLevel() {
public AccessSpecifier accessSpecifier() {
return JavassistFactory.modifiersToAccessLevel(ctConstructor.getModifiers());
}

Expand All @@ -132,7 +132,7 @@ public int getNumberOfSpecifiedExceptions() {
}

@Override
public Type getSpecifiedException(int index) {
public ResolvedType getSpecifiedException(int index) {
if (index < 0 || index >= getNumberOfSpecifiedExceptions()) {
throw new IllegalArgumentException(String.format("No exception with index %d. Number of exceptions: %d",
index, getNumberOfSpecifiedExceptions()));
Expand Down
Expand Up @@ -20,18 +20,13 @@
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.resolution.MethodUsage;
import com.github.javaparser.resolution.UnsolvedSymbolException;
import com.github.javaparser.resolution.declarations.ResolvedMethodDeclaration;
import com.github.javaparser.resolution.declarations.ResolvedReferenceTypeDeclaration;
import com.github.javaparser.resolution.declarations.*;
import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.core.resolution.Context;
import com.github.javaparser.symbolsolver.logic.AbstractTypeDeclaration;
import com.github.javaparser.symbolsolver.model.declarations.*;
import com.github.javaparser.symbolsolver.model.methods.MethodUsage;
import com.github.javaparser.symbolsolver.model.resolution.SymbolReference;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.resolution.UnsolvedSymbolException;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceType;
import com.github.javaparser.symbolsolver.model.typesystem.Type;
import com.github.javaparser.symbolsolver.resolution.MethodResolutionLogic;
import javassist.CtClass;
import javassist.CtMethod;
Expand All @@ -47,7 +42,7 @@
/**
* @author Federico Tomassetti
*/
public class JavassistEnumDeclaration extends AbstractTypeDeclaration implements EnumDeclaration {
public class JavassistEnumDeclaration extends AbstractTypeDeclaration implements ResolvedEnumDeclaration {

private CtClass ctClass;
private TypeSolver typeSolver;
Expand All @@ -66,7 +61,7 @@ public JavassistEnumDeclaration(CtClass ctClass, TypeSolver typeSolver) {
}

@Override
public AccessSpecifier accessLevel() {
public AccessSpecifier accessSpecifier() {
return JavassistFactory.modifiersToAccessLevel(ctClass.getModifiers());
}

Expand All @@ -90,23 +85,23 @@ public String getQualifiedName() {
}

@Override
public List<ReferenceType> getAncestors() {
public List<ResolvedReferenceType> getAncestors() {
// Direct ancestors of an enum are java.lang.Enum and interfaces
List<ReferenceType> ancestors = new LinkedList<>();
List<ResolvedReferenceType> ancestors = new LinkedList<>();

try {
CtClass superClass = ctClass.getSuperclass();

if (superClass != null) {
Type superClassTypeUsage = JavassistFactory.typeUsageFor(superClass, typeSolver);
ResolvedType superClassTypeUsage = JavassistFactory.typeUsageFor(superClass, typeSolver);

if (superClassTypeUsage.isReferenceType()) {
ancestors.add(superClassTypeUsage.asReferenceType());
}
}

for (CtClass interfaze : ctClass.getInterfaces()) {
Type interfazeTypeUsage = JavassistFactory.typeUsageFor(interfaze, typeSolver);
ResolvedType interfazeTypeUsage = JavassistFactory.typeUsageFor(interfaze, typeSolver);

if (interfazeTypeUsage.isReferenceType()) {
ancestors.add(interfazeTypeUsage.asReferenceType());
Expand All @@ -120,8 +115,8 @@ public List<ReferenceType> getAncestors() {
}

@Override
public FieldDeclaration getField(String name) {
Optional<FieldDeclaration> field = javassistTypeDeclarationAdapter.getDeclaredFields().stream().filter(f -> f.getName().equals(name)).findFirst();
public ResolvedFieldDeclaration getField(String name) {
Optional<ResolvedFieldDeclaration> field = javassistTypeDeclarationAdapter.getDeclaredFields().stream().filter(f -> f.getName().equals(name)).findFirst();

return field.orElseThrow(() -> new RuntimeException("Field " + name + " does not exist in " + ctClass.getName() + "."));
}
Expand All @@ -132,17 +127,17 @@ public boolean hasField(String name) {
}

@Override
public List<FieldDeclaration> getAllFields() {
public List<ResolvedFieldDeclaration> getAllFields() {
return javassistTypeDeclarationAdapter.getDeclaredFields();
}

@Override
public Set<MethodDeclaration> getDeclaredMethods() {
public Set<ResolvedMethodDeclaration> getDeclaredMethods() {
return javassistTypeDeclarationAdapter.getDeclaredMethods();
}

@Override
public boolean isAssignableBy(Type type) {
public boolean isAssignableBy(ResolvedType type) {
throw new UnsupportedOperationException();
}

Expand All @@ -163,17 +158,17 @@ public String getName() {
}

@Override
public List<TypeParameterDeclaration> getTypeParameters() {
public List<ResolvedTypeParameterDeclaration> getTypeParameters() {
return javassistTypeDeclarationAdapter.getTypeParameters();
}

@Override
public Optional<ReferenceTypeDeclaration> containerType() {
public Optional<ResolvedReferenceTypeDeclaration> containerType() {
return javassistTypeDeclarationAdapter.containerType();
}

public SymbolReference<MethodDeclaration> solveMethod(String name, List<Type> argumentsTypes, boolean staticOnly) {
List<MethodDeclaration> candidates = new ArrayList<>();
public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<ResolvedType> argumentsTypes, boolean staticOnly) {
List<ResolvedMethodDeclaration> candidates = new ArrayList<>();
Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (CtMethod method : ctClass.getDeclaredMethods()) {
boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
Expand Down

0 comments on commit e15d5a8

Please sign in to comment.