Skip to content

Commit

Permalink
FISH-48 Generic types nested signature parser
Browse files Browse the repository at this point in the history
Signed-off-by: Gaurav Gupta <[email protected]>
  • Loading branch information
jGauravGupta committed Aug 12, 2020
1 parent c1b9174 commit 3d6b917
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.glassfish.hk2.classmodel.reflect;

import java.util.List;

/**
* Model that represent the field of a class
*
Expand Down Expand Up @@ -59,10 +57,4 @@ public interface FieldModel extends Member, AnnotatedElement, ParameterizedType
*/
boolean isTransient();

/**
* The list of raw generic types
*
* @return
*/
List<String> getFormalTypeVariable();
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ public interface Parameter extends AnnotatedElement, ParameterizedType {
*/
public MethodModel getMethod();

/**
* Returns the parameter type
* @return parameter type
*/
public Type getType();

/**
* Returns the parameter index
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,48 @@
import java.util.List;

/**
* Denote a Generic type that is parameterized over types
*
* @author [email protected]
*/
public interface ParameterizedType {

Type getType();
/**
* Returns the parameter type
*
* @return parameter type
*/
public Type getType();

/**
* Returns the parameter type name
*
* @return parameter type name
*/
String getTypeName();

List<ParameterizedType> getGenericTypes();
/**
* Returns the formal type name
*
* @return the formal type name
*/
String getFormalType();

/**
* @return the true value for formal type parameters and false value for
* parameterized type with actual type arguments.
*/
boolean isFormalType();

/**
*
* @return true if type is array
*/
boolean isArray();

/**
*
* @return the list of parameterized subtype
*/
List<ParameterizedType> getParameterizedTypes();

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public ExtensibleTypeImpl(String name, TypeProxy<Type> sink, TypeProxy parent) {
this.parent = parent;
}

@Override
public Map<String, ParameterizedInterfaceModel> getFormalTypeParameters() {
return formalTypeParameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.List;
import org.glassfish.hk2.classmodel.reflect.ExtensibleType;
import org.glassfish.hk2.classmodel.reflect.FieldModel;
import org.glassfish.hk2.classmodel.reflect.ParameterizedType;
import org.objectweb.asm.Opcodes;
import org.glassfish.hk2.classmodel.reflect.ParameterizedType;

/**
* Implementation of a field model
Expand All @@ -29,15 +29,15 @@ public class FieldModelImpl extends AnnotatedElementImpl implements FieldModel {

private final ExtensibleType declaringType;

private final TypeProxy typeProxy;
private TypeProxy typeProxy;

private int access;
private org.objectweb.asm.Type type;

private List<String> formalTypeVariable;
private String formalType;

private final List<ParameterizedType> genericTypes = new ArrayList<>();
private int access;

private org.objectweb.asm.Type type;
private final List<ParameterizedType> parameterizedTypes = new ArrayList<>();

public FieldModelImpl(String name, TypeProxy typeProxy, ExtensibleType declaringType) {
super(name);
Expand Down Expand Up @@ -72,11 +72,22 @@ public ExtensibleType getType() {
public String getTypeName() {
if (typeProxy != null) {
return typeProxy.getName();
} else {
} else if (type != null) {
return type.getClassName();
} else {
return null;
}
}

@Override
public String getFormalType() {
return formalType;
}

public void setFormalType(String formalType) {
this.formalType = formalType;
}

public TypeProxy<?> getTypeProxy() {
return typeProxy;
}
Expand All @@ -85,16 +96,12 @@ public void setAccess(int access) {
this.access = access;
}

public void setType(org.objectweb.asm.Type type) {
this.type = type;
}

public List<String> getFormalTypeVariable() {
return formalTypeVariable;
public void setTypeProxy(TypeProxy typeProxy) {
this.typeProxy = typeProxy;
}

public void setFormalTypeVariable(List<String> formalTypeVariable) {
this.formalTypeVariable = formalTypeVariable;
public void setType(org.objectweb.asm.Type type) {
this.type = type;
}

@Override
Expand All @@ -104,8 +111,13 @@ protected void print(StringBuffer sb) {
}

@Override
public List<ParameterizedType> getGenericTypes() {
return genericTypes;
public List<ParameterizedType> getParameterizedTypes() {
return parameterizedTypes;
}

@Override
public boolean isFormalType() {
return formalType != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
*/
package org.glassfish.hk2.classmodel.reflect.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.ArrayDeque;
import org.glassfish.hk2.classmodel.reflect.FieldModel;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.signature.SignatureVisitor;
import org.glassfish.hk2.classmodel.reflect.ParameterizedType;

/**
* Signature visitor to visit field and respective generic types
Expand All @@ -27,19 +28,53 @@
*/
public class FieldSignatureVisitorImpl extends SignatureVisitor {

private final List<String> typeVariable = new ArrayList<>();
private final TypeBuilder typeBuilder;
private final ArrayDeque<ParameterizedType> parentType = new ArrayDeque<>();

public FieldSignatureVisitorImpl() {
public FieldSignatureVisitorImpl(TypeBuilder typeBuilder, FieldModel fieldModel) {
super(Opcodes.ASM7);

this.typeBuilder = typeBuilder;
parentType.add(fieldModel);
}

@Override
public void visitTypeVariable(String typeVariable) {
this.typeVariable.add(typeVariable);
if (!parentType.isEmpty()) {
ParameterizedType current = parentType.peekLast();
if (current instanceof FieldModelImpl
&& ((FieldModelImpl) current).getTypeProxy() == null
&& ((FieldModelImpl) current).getFormalType() == null) {
((FieldModelImpl) current).setFormalType(typeVariable);
} else {
ParameterizedTypeImpl parameterizedType = new ParameterizedTypeImpl(typeVariable);
current.getParameterizedTypes().add(parameterizedType);
}
}
}

public List<String> getTypeVariable() {
return typeVariable;
@Override
public void visitClassType(String name) {
String className = org.objectweb.asm.Type.getObjectType(name).getClassName();
TypeProxy typeProxy = typeBuilder.getHolder(className);
if (typeProxy != null) {
if (!parentType.isEmpty()) {
ParameterizedType current = parentType.peekLast();
if (current instanceof FieldModelImpl
&& ((FieldModelImpl) current).getTypeProxy() == null) {
((FieldModelImpl) current).setTypeProxy(typeProxy);
} else {
ParameterizedTypeImpl parameterizedType = new ParameterizedTypeImpl(typeProxy);
current.getParameterizedTypes().add(parameterizedType);
parentType.add(parameterizedType);
}
}
}
}

@Override
public void visitEnd() {
parentType.pollLast();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.glassfish.hk2.classmodel.reflect.impl;

import org.glassfish.hk2.classmodel.reflect.InterfaceModel;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.signature.SignatureVisitor;

Expand Down Expand Up @@ -68,6 +67,25 @@ public SignatureVisitor visitReturnType() {
return this;
}

@Override
public void visitTypeVariable(String typeVariable) {
if (!parentType.isEmpty()) {
ParameterizedType current = parentType.peekLast();
if (current instanceof ParameterImpl
&& ((ParameterImpl) current).getTypeProxy() == null
&& ((ParameterImpl) current).getFormalType() == null) {
((ParameterImpl) current).setFormalType(typeVariable);
} else if (current instanceof ParameterizedTypeImpl
&& ((ParameterizedTypeImpl) current).getTypeProxy() == null
&& ((ParameterizedTypeImpl) current).getFormalType() == null) {
((ParameterizedTypeImpl) current).setFormalType(typeVariable);
} else {
ParameterizedTypeImpl parameterizedType = new ParameterizedTypeImpl(typeVariable);
current.getParameterizedTypes().add(parameterizedType);
}
}
}

@Override
public void visitClassType(String name) {
String className = org.objectweb.asm.Type.getObjectType(name).getClassName();
Expand All @@ -82,9 +100,9 @@ public void visitClassType(String name) {
&& ((ParameterizedTypeImpl) current).getTypeProxy() == null) {
((ParameterizedTypeImpl) current).setTypeProxy(typeProxy);
} else {
ParameterizedTypeImpl genericType = new ParameterizedTypeImpl(typeProxy);
current.getGenericTypes().add(genericType);
parentType.add(genericType);
ParameterizedTypeImpl parameterizedType = new ParameterizedTypeImpl(typeProxy);
current.getParameterizedTypes().add(parameterizedType);
parentType.add(parameterizedType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,18 +225,18 @@ public FieldVisitor visitField(int access, final String name, final String desc,
}
cm = (ExtensibleTypeImpl) type;

org.objectweb.asm.Type asmType = org.objectweb.asm.Type.getType(desc);

TypeProxy<?> fieldType = typeBuilder.getHolder(asmType.getClassName());
final FieldModelImpl field = typeBuilder.getFieldModel(name, fieldType, cm);
final FieldModelImpl field = typeBuilder.getFieldModel(name, null, cm);

SignatureReader reader = new SignatureReader(signature == null ? desc : signature);
FieldSignatureVisitorImpl visitor = new FieldSignatureVisitorImpl();
FieldSignatureVisitorImpl visitor = new FieldSignatureVisitorImpl(typeBuilder, field);
reader.accept(visitor);
field.setFormalTypeVariable(visitor.getTypeVariable());

org.objectweb.asm.Type type = org.objectweb.asm.Type.getType(desc);
field.setType(type);
org.objectweb.asm.Type asmType = org.objectweb.asm.Type.getType(desc);
field.setType(asmType);
if (field.getTypeProxy() == null) {
field.setTypeProxy(typeBuilder.getHolder(asmType.getClassName()));
}

field.setAccess(access);
fieldVisitor.getContext().field = field;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ public class ParameterImpl extends AnnotatedElementImpl implements Parameter {

private org.objectweb.asm.Type type;

private String formalType;

private final int index;

private final List<ParameterizedType> genericTypes = new ArrayList<>();
private final List<ParameterizedType> parameterizedTypes = new ArrayList<>();

public ParameterImpl(int index, String name, TypeProxy<?> typeProxy, MethodModel methodModel) {
super(name);
Expand Down Expand Up @@ -65,8 +67,10 @@ public Type getType() {
public String getTypeName() {
if (typeProxy != null) {
return typeProxy.getName();
} else {
} else if (type != null) {
return type.getClassName();
} else {
return null;
}
}

Expand All @@ -83,8 +87,22 @@ public void setType(org.objectweb.asm.Type type) {
}

@Override
public List<ParameterizedType> getGenericTypes() {
return genericTypes;
public String getFormalType() {
return formalType;
}

public void setFormalType(String formalType) {
this.formalType = formalType;
}

@Override
public List<ParameterizedType> getParameterizedTypes() {
return parameterizedTypes;
}

@Override
public boolean isFormalType() {
return formalType != null;
}

@Override
Expand Down
Loading

0 comments on commit 3d6b917

Please sign in to comment.