Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
#32 added generic method parameters, return type and declared throwables
Browse files Browse the repository at this point in the history
  • Loading branch information
DirkMahler committed May 27, 2021
1 parent aaafdd6 commit d2ce751
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import com.buschmais.jqassistant.plugin.common.api.model.ValueDescriptor;
import com.buschmais.jqassistant.plugin.java.api.model.generics.BoundDescriptor;
import com.buschmais.jqassistant.plugin.java.api.report.Java;
import com.buschmais.xo.neo4j.api.annotation.Label;
import com.buschmais.xo.neo4j.api.annotation.Relation;
Expand Down Expand Up @@ -36,6 +37,16 @@ public interface MethodDescriptor extends MemberDescriptor, AbstractDescriptor {

void setReturns(TypeDescriptor returns);

/**
* Return the generic return type of this method.
*
* @return The generic return type.
*/
@Relation("RETURNS_GENERIC")
BoundDescriptor getReturnsGeneric();

void setReturnsGeneric(BoundDescriptor returnsGeneric);

@Relation("HAS_DEFAULT")
ValueDescriptor<?> getHasDefault();

Expand All @@ -47,7 +58,15 @@ public interface MethodDescriptor extends MemberDescriptor, AbstractDescriptor {
* @return The declared throwables.
*/
@Relation("THROWS")
List<TypeDescriptor> getDeclaredThrowables();
List<TypeDescriptor> getThrows();

/**
* Return all declared generic throwables of this method.
*
* @return The declared generic throwables.
*/
@Relation("THROWS_GENERIC")
List<BoundDescriptor> getThrowsGeneric();

/**
* Return all read accesses to fields this method performs.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
public class ClassVisitor extends org.objectweb.asm.ClassVisitor {

private TypeCache.CachedType<? extends ClassFileDescriptor> cachedType;
private DependentTypeSignatureVisitor dependentTypeSignatureVisitor;
private FileDescriptor fileDescriptor;
private VisitorHelper visitorHelper;

Expand Down Expand Up @@ -51,7 +50,6 @@ public void visit(final int version, final int access, final String name, final
Class<? extends ClassFileDescriptor> javaType = getJavaType(access);
String fullQualifiedName = SignatureHelper.getObjectType(name);
cachedType = visitorHelper.createType(fullQualifiedName, fileDescriptor, javaType);
dependentTypeSignatureVisitor = new DependentTypeSignatureVisitor(cachedType, visitorHelper);
ClassFileDescriptor classFileDescriptor = cachedType.getTypeDescriptor();
classFileDescriptor.setByteCodeVersion(version);
if (hasFlag(access, Opcodes.ACC_ABSTRACT) && !hasFlag(access, Opcodes.ACC_INTERFACE)) {
Expand Down Expand Up @@ -124,13 +122,13 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri
parameterDescriptor.setType(typeDescriptor);
}
} else {
new SignatureReader(signature).accept(new MethodSignatureVisitor(cachedType, methodDescriptor, visitorHelper, dependentTypeSignatureVisitor));
new SignatureReader(signature).accept(new MethodSignatureVisitor(cachedType, methodDescriptor, visitorHelper));
}
for (int i = 0; exceptions != null && i < exceptions.length; i++) {
TypeDescriptor exceptionType = visitorHelper.resolveType(SignatureHelper.getObjectType(exceptions[i]), cachedType).getTypeDescriptor();
methodDescriptor.getDeclaredThrowables().add(exceptionType);
methodDescriptor.getThrows().add(exceptionType);
}
return new MethodVisitor(cachedType, methodDescriptor, visitorHelper, dependentTypeSignatureVisitor);
return new MethodVisitor(cachedType, methodDescriptor, visitorHelper);
}

private void setModifiers(final int access, AccessModifierDescriptor descriptor) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

import com.buschmais.jqassistant.plugin.java.api.model.MethodDescriptor;
import com.buschmais.jqassistant.plugin.java.api.model.ParameterDescriptor;
import com.buschmais.jqassistant.plugin.java.api.model.TypeDescriptor;
import com.buschmais.jqassistant.plugin.java.api.model.generics.BoundDescriptor;
import com.buschmais.jqassistant.plugin.java.api.scanner.TypeCache;
import com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.generics.AbstractBoundVisitor;

import org.objectweb.asm.signature.SignatureVisitor;

Expand All @@ -12,77 +13,50 @@
*/
public class MethodSignatureVisitor extends SignatureVisitor {

private TypeCache.CachedType containingType;
private MethodDescriptor methodDescriptor;
private VisitorHelper visitorHelper;
private DependentTypeSignatureVisitor dependentTypeSignatureVisitor;
private final TypeCache.CachedType containingType;
private final MethodDescriptor methodDescriptor;
private final VisitorHelper visitorHelper;
private int parameterIndex = 0;

MethodSignatureVisitor(TypeCache.CachedType containingType, MethodDescriptor methodDescriptor, VisitorHelper visitorHelper,
DependentTypeSignatureVisitor dependentTypeSignatureVisitor) {
MethodSignatureVisitor(TypeCache.CachedType containingType, MethodDescriptor methodDescriptor, VisitorHelper visitorHelper) {
super(VisitorHelper.ASM_OPCODES);
this.containingType = containingType;
this.methodDescriptor = methodDescriptor;
this.visitorHelper = visitorHelper;
this.dependentTypeSignatureVisitor = dependentTypeSignatureVisitor;
}

@Override
public SignatureVisitor visitClassBound() {
return dependentTypeSignatureVisitor;
}

@Override
public SignatureVisitor visitInterfaceBound() {
return dependentTypeSignatureVisitor;
}

@Override
public SignatureVisitor visitParameterType() {
final ParameterDescriptor parameterDescriptor = visitorHelper.addParameterDescriptor(methodDescriptor, parameterIndex);
parameterIndex++;
return new AbstractTypeSignatureVisitor(containingType, visitorHelper) {

@Override
public SignatureVisitor visitArrayType() {
return dependentTypeSignatureVisitor;
}

return new AbstractBoundVisitor<BoundDescriptor>(null, visitorHelper, containingType) {
@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
return dependentTypeSignatureVisitor;
}

@Override
public void visitEnd(TypeDescriptor resolvedTypeDescriptor) {
parameterDescriptor.setType(resolvedTypeDescriptor);
protected void apply(BoundDescriptor bound) {
parameterDescriptor.setType(bound.getRawType());
parameterDescriptor.setGenericType(bound);
}
};
}

@Override
public SignatureVisitor visitReturnType() {
return new AbstractTypeSignatureVisitor(containingType, visitorHelper) {

@Override
public SignatureVisitor visitArrayType() {
return dependentTypeSignatureVisitor;
}

return new AbstractBoundVisitor<BoundDescriptor>(null, visitorHelper, containingType) {
@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
return dependentTypeSignatureVisitor;
}

@Override
public void visitEnd(TypeDescriptor resolvedTypeDescriptor) {
methodDescriptor.setReturns(resolvedTypeDescriptor);
protected void apply(BoundDescriptor bound) {
methodDescriptor.setReturns(bound.getRawType());
methodDescriptor.setReturnsGeneric(bound);
}
};
}

@Override
public SignatureVisitor visitExceptionType() {
return dependentTypeSignatureVisitor;
return new AbstractBoundVisitor<BoundDescriptor>(null, visitorHelper, containingType) {
@Override
protected void apply(BoundDescriptor bound) {
methodDescriptor.getThrows().add(bound.getRawType());
methodDescriptor.getThrowsGeneric().add(bound);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
import java.util.Set;

import com.buschmais.jqassistant.plugin.java.api.model.*;
import com.buschmais.jqassistant.plugin.java.api.model.generics.BoundDescriptor;
import com.buschmais.jqassistant.plugin.java.api.scanner.SignatureHelper;
import com.buschmais.jqassistant.plugin.java.api.scanner.TypeCache;
import com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.generics.AbstractBoundVisitor;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.signature.SignatureReader;
import org.objectweb.asm.signature.SignatureVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -29,21 +30,18 @@ public class MethodVisitor extends org.objectweb.asm.MethodVisitor {
private TypeCache.CachedType containingType;
private MethodDescriptor methodDescriptor;
private VisitorHelper visitorHelper;
private DependentTypeSignatureVisitor dependentTypeSignatureVisitor;
private int syntheticParameters = 0;
private int cyclomaticComplexity = 1;
private Integer lineNumber = null;
private Integer firstLineNumber = null;
private Integer lastLineNumber = null;
private Set<Integer> effectiveLines = new HashSet<>();

protected MethodVisitor(TypeCache.CachedType containingType, MethodDescriptor methodDescriptor, VisitorHelper visitorHelper,
DependentTypeSignatureVisitor dependentTypeSignatureVisitor) {
protected MethodVisitor(TypeCache.CachedType containingType, MethodDescriptor methodDescriptor, VisitorHelper visitorHelper) {
super(VisitorHelper.ASM_OPCODES);
this.containingType = containingType;
this.methodDescriptor = methodDescriptor;
this.visitorHelper = visitorHelper;
this.dependentTypeSignatureVisitor = dependentTypeSignatureVisitor;
}

@Override
Expand Down Expand Up @@ -114,21 +112,11 @@ public void visitLocalVariable(final String name, final String desc, final Strin
TypeDescriptor type = visitorHelper.resolveType(SignatureHelper.getType((desc)), containingType).getTypeDescriptor();
variableDescriptor.setType(type);
} else {
new SignatureReader(signature).accept(new AbstractTypeSignatureVisitor(containingType, visitorHelper) {

@Override
public SignatureVisitor visitArrayType() {
return dependentTypeSignatureVisitor;
}

@Override
public SignatureVisitor visitTypeArgument(char wildcard) {
return dependentTypeSignatureVisitor;
}

new SignatureReader(signature).accept(new AbstractBoundVisitor<BoundDescriptor>(null, visitorHelper, containingType) {
@Override
public void visitEnd(TypeDescriptor resolvedTypeDescriptor) {
variableDescriptor.setType(resolvedTypeDescriptor);
protected void apply(BoundDescriptor bound) {
variableDescriptor.setType(bound.getRawType());
variableDescriptor.setGenericType(bound);
}
});
}
Expand Down

0 comments on commit d2ce751

Please sign in to comment.