Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FISH-48 Generic type parser for Class and fields #15

Merged
merged 2 commits into from
Aug 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.glassfish.hk2.classmodel.reflect;

import java.util.Collection;
import java.util.Map;

/**
* An extensible type is a type that can be subclassed like an interface
Expand Down Expand Up @@ -74,8 +75,28 @@ public interface ExtensibleType<T extends ExtensibleType> extends Type {
*/
Collection<InterfaceModel> getInterfaces();

/**
*
* @return the list of parameterized type of superclass and implemented
* interface.
*/
Collection<ParameterizedInterfaceModel> getParameterizedInterfaces();

/**
*
* @param type the extensible type
* @return the parameterized type of the respective extensible type
*/
ParameterizedInterfaceModel getParameterizedInterface(ExtensibleType type);

/**
*
* Represents the raw generic type and bounded parameterized type.
*
* @return the list of generic type parameter declared on the class.
*/
Map<String, ParameterizedInterfaceModel> getFormalTypeParameters();

/**
* Returns an unmodifiable list of static fields defined by this type
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,5 @@ public interface FieldModel extends Member, AnnotatedElement, ParameterizedType
* @return true, if field is marked transient.
*/
boolean isTransient();

}
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 @@ -32,12 +32,19 @@ public interface ParameterizedInterfaceModel {
*/
String getName();

/**
* Returns the simple name.
*
* @return a declaration for this type
*/
String getRawInterfaceName();

/**
* Returns the raw interface for this parameterized type
*
* @return the interface model instance
*/
InterfaceModel getRawInterface();
ExtensibleType getRawInterface();

/**
* Returns the type parameters in order.
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 @@ -17,27 +17,33 @@
package org.glassfish.hk2.classmodel.reflect.impl;

import org.glassfish.hk2.classmodel.reflect.*;
import org.glassfish.hk2.classmodel.reflect.util.ParsingConfig;

import java.util.*;
import java.util.logging.Logger;

/**
* Implementation of an extensible type (Class or Interface)
*/
public abstract class ExtensibleTypeImpl<T extends ExtensibleType> extends TypeImpl implements ExtensibleType<T> {

private TypeProxy<?> parent;
private final List<FieldModel> staticFields = new ArrayList<FieldModel> ();
private final List<TypeProxy<InterfaceModel>> implementedIntf = new ArrayList<TypeProxy<InterfaceModel>>();
private final List<ParameterizedInterfaceModel> implementedParameterizedIntf =
new ArrayList<ParameterizedInterfaceModel>();
protected TypeProxy<?> parent;
private final List<FieldModel> staticFields = new ArrayList<>();
private final List<TypeProxy<InterfaceModel>> implementedIntf = new ArrayList<>();
private final List<ParameterizedInterfaceModel> implementedParameterizedIntf = new ArrayList<>();
private Map<String, ParameterizedInterfaceModel> formalTypeParameters;

public ExtensibleTypeImpl(String name, TypeProxy<Type> sink, TypeProxy parent) {
super(name, sink);
this.parent = parent;
}

@Override
public Map<String, ParameterizedInterfaceModel> getFormalTypeParameters() {
return formalTypeParameters;
}

public void setFormalTypeParameters(Map<String, ParameterizedInterfaceModel> typeParameters) {
this.formalTypeParameters = typeParameters;
}

@Override
public T getParent() {
if (parent != null) {
Expand Down Expand Up @@ -82,7 +88,9 @@ synchronized void isImplementing(TypeProxy<InterfaceModel> intf) {
}

synchronized void isImplementing(ParameterizedInterfaceModelImpl pim) {
implementedIntf.add(pim.rawInterface);
if (pim.getRawInterface() instanceof InterfaceModel) {
implementedIntf.add((TypeProxy<InterfaceModel>) pim.getRawInterfaceProxy());
}
implementedParameterizedIntf.add(pim);
}

Expand All @@ -96,6 +104,16 @@ public Collection<ParameterizedInterfaceModel> getParameterizedInterfaces() {
return Collections.unmodifiableCollection(implementedParameterizedIntf);
}

@Override
public ParameterizedInterfaceModel getParameterizedInterface(ExtensibleType type) {
for (ParameterizedInterfaceModel interfaceModel : implementedParameterizedIntf) {
if (interfaceModel.getRawInterface() == type) {
return interfaceModel;
}
}
return null;
}

@Override
public Collection<T> subTypes() {
List<T> subTypes = new ArrayList<T>();
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,13 +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 final List<ParameterizedType> genericTypes = new ArrayList<>();
private String formalType;

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

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

public FieldModelImpl(String name, TypeProxy typeProxy, ExtensibleType declaringType) {
super(name);
Expand All @@ -60,12 +62,30 @@ public String getDeclaringTypeName() {

@Override
public ExtensibleType getType() {
return (ExtensibleType) typeProxy.get();
if (typeProxy != null) {
return (ExtensibleType) typeProxy.get();
}
return null;
}

@Override
public String getTypeName() {
return typeProxy.getName();
if (typeProxy != null) {
return typeProxy.getName();
} 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() {
Expand All @@ -76,6 +96,10 @@ public void setAccess(int access) {
this.access = access;
}

public void setTypeProxy(TypeProxy typeProxy) {
this.typeProxy = typeProxy;
}

public void setType(org.objectweb.asm.Type type) {
this.type = type;
}
Expand All @@ -87,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
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.hk2.classmodel.reflect.impl;

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
*
* @author [email protected]
*/
public class FieldSignatureVisitorImpl extends SignatureVisitor {

private final TypeBuilder typeBuilder;
private final ArrayDeque<ParameterizedType> parentType = new ArrayDeque<>();

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

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

@Override
public void visitTypeVariable(String 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);
}
}
}

@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();
}

}
Loading