Skip to content

Commit

Permalink
extracted logic to separate method, fixed similar NotFoundException, …
Browse files Browse the repository at this point in the history
…fixed warnings
  • Loading branch information
daans committed Jul 30, 2018
1 parent b13a056 commit 3ec944c
Showing 1 changed file with 23 additions and 19 deletions.
Expand Up @@ -35,7 +35,10 @@
import javassist.CtField;
import javassist.CtMethod;
import javassist.NotFoundException;
import javassist.bytecode.*;
import javassist.bytecode.AccessFlag;
import javassist.bytecode.BadBytecode;
import javassist.bytecode.SignatureAttribute;
import javassist.bytecode.SyntheticAttribute;

import java.lang.reflect.Modifier;
import java.util.*;
Expand All @@ -47,8 +50,6 @@
*/
public class JavassistClassDeclaration extends AbstractClassDeclaration {



private CtClass ctClass;
private TypeSolver typeSolver;
private JavassistTypeDeclarationAdapter javassistTypeDeclarationAdapter;
Expand Down Expand Up @@ -92,9 +93,7 @@ public boolean equals(Object o) {

JavassistClassDeclaration that = (JavassistClassDeclaration) o;

if (!ctClass.equals(that.ctClass)) return false;

return true;
return ctClass.equals(that.ctClass);
}

@Override
Expand Down Expand Up @@ -184,7 +183,7 @@ public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<
Predicate<CtMethod> staticOnlyCheck = m -> !staticOnly || (staticOnly && Modifier.isStatic(m.getModifiers()));
for (CtMethod method : ctClass.getDeclaredMethods()) {
boolean isSynthetic = method.getMethodInfo().getAttribute(SyntheticAttribute.tag) != null;
boolean isNotBridge = (method.getMethodInfo().getAccessFlags() & AccessFlag.BRIDGE) == 0;
boolean isNotBridge = (method.getMethodInfo().getAccessFlags() & AccessFlag.BRIDGE) == 0;
if (method.getName().equals(name) && !isSynthetic && isNotBridge && staticOnlyCheck.test(method)) {
candidates.add(new JavassistMethodDeclaration(method, typeSolver));
}
Expand All @@ -199,7 +198,7 @@ public SymbolReference<ResolvedMethodDeclaration> solveMethod(String name, List<
}
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
candidates.addAll(solveSuperClass().getAllMethods());
}

try {
Expand Down Expand Up @@ -257,7 +256,7 @@ public boolean isTypeParameter() {

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

@Override
Expand Down Expand Up @@ -299,17 +298,24 @@ public ResolvedReferenceType getSuperClass() {
SignatureAttribute.ClassSignature classSignature = SignatureAttribute.toClassSignature(ctClass.getGenericSignature());
return JavassistUtils.signatureTypeToType(classSignature.getSuperClass(), typeSolver, this).asReferenceType();
} catch (NotFoundException e) {
SymbolReference<ResolvedReferenceTypeDeclaration> reference = typeSolver.tryToSolveType(ctClass.getClassFile().getSuperclass());
if (reference.isSolved()) {
return new ReferenceTypeImpl(reference.getCorrespondingDeclaration(), typeSolver);
} else {
throw new RuntimeException(e);
}
return solveSuperClass();
} catch (BadBytecode e) {
throw new RuntimeException(e);
}
}

/**
* Solve the fqn of the super class with the {@link #typeSolver}.
*/
private ReferenceTypeImpl solveSuperClass() {
String superclass = ctClass.getClassFile().getSuperclass();
SymbolReference<ResolvedReferenceTypeDeclaration> reference = typeSolver.tryToSolveType(superclass);
if (reference.isSolved()) {
return new ReferenceTypeImpl(reference.getCorrespondingDeclaration(), typeSolver);
}
throw new RuntimeException("Unable to find " + superclass);
}

@Override
public List<ResolvedReferenceType> getInterfaces() {
try {
Expand All @@ -324,9 +330,7 @@ public List<ResolvedReferenceType> getInterfaces() {
.map(i -> JavassistUtils.signatureTypeToType(i, typeSolver, this).asReferenceType())
.collect(Collectors.toList());
}
} catch (NotFoundException e) {
throw new RuntimeException(e);
} catch (BadBytecode e) {
} catch (NotFoundException | BadBytecode e) {
throw new RuntimeException(e);
}
}
Expand All @@ -353,7 +357,7 @@ public AccessSpecifier accessSpecifier() {

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

@Override
Expand Down

0 comments on commit 3ec944c

Please sign in to comment.