下面列出了java.lang.annotation.Inherited#java.lang.reflect.GenericDeclaration 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parses the generic signature of a class and creates the data structure
* representing the signature.
*
* @param genericDecl the GenericDeclaration calling this method
* @param signature the generic signature of the class
*/
public void parseForClass(GenericDeclaration genericDecl, String signature) {
setInput(genericDecl, signature);
if (!eof) {
parseClassSignature();
} else {
if(genericDecl instanceof Class) {
Class c = (Class) genericDecl;
this.formalTypeParameters = EmptyArray.TYPE_VARIABLE;
this.superclassType = c.getSuperclass();
Class<?>[] interfaces = c.getInterfaces();
if (interfaces.length == 0) {
this.interfaceTypes = ListOfTypes.EMPTY;
} else {
this.interfaceTypes = new ListOfTypes(interfaces);
}
} else {
this.formalTypeParameters = EmptyArray.TYPE_VARIABLE;
this.superclassType = Object.class;
this.interfaceTypes = ListOfTypes.EMPTY;
}
}
}
public Class<?> getGenericType(Method method) {
Type genericReturnType = method.getGenericReturnType();
if (method.getGenericReturnType() instanceof ParameterizedType) {
ParameterizedType parameterizedTypeImpl = (ParameterizedType)genericReturnType;
Type first = parameterizedTypeImpl.getActualTypeArguments()[0];
if (first instanceof WildcardType) {
return null;
} else if (first instanceof ParameterizedType) {
return null;
} else if (first instanceof TypeVariable) {
TypeVariable<?> typeVariableImpl = (TypeVariable<?>)first;
GenericDeclaration genericDeclaration = typeVariableImpl.getGenericDeclaration();
if (genericDeclaration instanceof Class) {
return (Class<?>) genericDeclaration;
}
} else {
return (Class<?>) first;
}
}
Type genericReturnType2 = method.getGenericReturnType();
if (genericReturnType2 instanceof Class) {
return (Class<?>)genericReturnType2;
}
return null;
}
public static TypeParamInfo create(java.lang.reflect.TypeVariable typeVariable) {
GenericDeclaration decl = typeVariable.getGenericDeclaration();
TypeVariable<?>[] typeParams = decl.getTypeParameters();
for (int index = 0;index < typeParams.length;index++) {
if (typeParams[index].equals(typeVariable)) {
if (decl instanceof java.lang.Class) {
java.lang.Class classDecl = (java.lang.Class) decl;
return new Class(classDecl.getName(), index, typeVariable.getName());
} else if (decl instanceof java.lang.reflect.Method) {
java.lang.reflect.Method methodDecl = (java.lang.reflect.Method) decl;
return new Method(methodDecl.getDeclaringClass().getName(), methodDecl.getName(), index, typeVariable.getName());
} else {
throw new UnsupportedOperationException();
}
}
}
throw new AssertionError();
}
private static void printGenericsOfExceptions(Type genericType) {
if (genericType instanceof TypeVariable) {
TypeVariable typeVariable = (TypeVariable) genericType;
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
System.out.println("Generic declaration: " + genericDeclaration);
System.out.println("Bounds: ");
for (Type type: typeVariable.getBounds()) {
System.out.println(type);
}
}
}
/**
* Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by
* a class.
*/
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
return genericDeclaration instanceof Class
? (Class<?>) genericDeclaration
: null;
}
public static Type getSuperType(Class<?> serviceClass, TypeVariable<?> var) {
int pos = 0;
GenericDeclaration genericDeclaration = var.getGenericDeclaration();
TypeVariable<?>[] vars = genericDeclaration.getTypeParameters();
for (; pos < vars.length; pos++) {
if (vars[pos].getName().equals(var.getName())) {
break;
}
}
ParameterizedType genericSubtype = findGenericDeclaration(genericDeclaration, serviceClass);
Type result = null;
if (genericSubtype != null) {
result = genericSubtype.getActualTypeArguments()[pos];
}
if (result instanceof TypeVariable) {
result = getSuperType(serviceClass, (TypeVariable<?>) result);
}
if (result == null || result == Object.class) {
for (Type bound : var.getBounds()) {
if (bound != Object.class) {
result = bound;
break;
}
}
}
return result;
}
static TypeVariable findFormalVar(GenericDeclaration layer, String name) {
TypeVariable[] formalVars = layer.getTypeParameters();
for (TypeVariable var : formalVars) {
if (name.equals(var.getName())) {
return var;
}
}
// resolve() looks up the next level only, if null is returned
return null;
}
/**
* Parses the generic signature of a field and creates the data structure
* representing the signature.
*
* @param genericDecl the GenericDeclaration calling this method
* @param signature the generic signature of the class
*/
public void parseForField(GenericDeclaration genericDecl,
String signature) {
setInput(genericDecl, signature);
if (!eof) {
this.fieldType = parseFieldTypeSignature();
}
}
/**
* Prints the Java representation of the type
*/
@SuppressWarnings("unchecked")
public void printType(Type type)
throws IOException
{
if (type instanceof Class<?>) {
printTypeClass((Class<?>) type);
}
else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
printParameterizedType(parameterizedType);
}
else if (type instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) type;
printWildcardType(wildcardType);
}
else if (type instanceof TypeVariable<?>) {
TypeVariable<? extends GenericDeclaration> typeVariable = (TypeVariable<? extends GenericDeclaration>) type;
printTypeVariable(typeVariable);
}
else if (type instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) type;
printType(genericArrayType.getGenericComponentType());
print("[]");
}
else {
throw new UnsupportedOperationException(type.getClass().getName() + " "
+ String.valueOf(type));
}
}
protected void enhanceGenericDeclaration(JvmExecutable result, GenericDeclaration declaration) {
TypeVariable<?>[] typeParameters = declaration.getTypeParameters();
if (typeParameters.length != 0) {
InternalEList<JvmTypeParameter> jvmTypeParameters = (InternalEList<JvmTypeParameter>)result.getTypeParameters();
for (TypeVariable<?> variable : typeParameters) {
jvmTypeParameters.addUnique(createTypeParameter(variable, result));
}
}
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
/**
* Resolves the actual type of the given type variable that comes from a field type based on the
* given context list.
*
* <p>In case the type variable can be resolved partially, it will return the partially resolved
* type variable.
*
* @param context context list, ordering from least specific to most specific type context, for
* example container class and then its field
* @param typeVariable type variable
* @return resolved or partially resolved actual type (type variable, class, parameterized type,
* or generic array type, but not wildcard type) or {@code null} if unable to resolve at all
*/
public static Type resolveTypeVariable(List<Type> context, TypeVariable<?> typeVariable) {
// determine where the type variable was declared
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
if (genericDeclaration instanceof Class<?>) {
Class<?> rawGenericDeclaration = (Class<?>) genericDeclaration;
// check if the context extends that declaration
int contextIndex = context.size();
ParameterizedType parameterizedType = null;
while (parameterizedType == null && --contextIndex >= 0) {
parameterizedType =
getSuperParameterizedType(context.get(contextIndex), rawGenericDeclaration);
}
if (parameterizedType != null) {
// find the type variable's index in the declaration's type parameters
TypeVariable<?>[] typeParameters = genericDeclaration.getTypeParameters();
int index = 0;
for (; index < typeParameters.length; index++) {
TypeVariable<?> typeParameter = typeParameters[index];
if (typeParameter.equals(typeVariable)) {
break;
}
}
// use that index to get the actual type argument
Type result = parameterizedType.getActualTypeArguments()[index];
if (result instanceof TypeVariable<?>) {
// attempt to resolve type variable
Type resolve = resolveTypeVariable(context, (TypeVariable<?>) result);
if (resolve != null) {
return resolve;
}
// partially resolved type variable is okay
}
return result;
}
}
return null;
}
private static TypeRef visitParameterizedType(TypeVisitor visitor,
Type rawType,
Type []typeArguments,
Map<String,? extends Type> parentMap)
{
if (rawType instanceof GenericDeclaration) {
GenericDeclaration decl = (GenericDeclaration) rawType;
TypeVariable<?> []vars = decl.getTypeParameters();
Map<String,Type> varMap = new LinkedHashMap<>();
for (int i = 0; i < vars.length; i++) {
Type typeArg = typeArguments[i];
if (typeArg instanceof TypeVariable) {
TypeVariable<?> typeVar = (TypeVariable<?>) typeArg;
Type parentType = parentMap.get(typeVar.getName());
if (parentType != null) {
varMap.put(vars[i].getName(), parentType);
}
else {
varMap.put(vars[i].getName(), typeVar.getBounds()[0]);
}
}
else {
varMap.put(vars[i].getName(), typeArg);
}
}
return visit(visitor, rawType, varMap);
}
else {
System.out.println("UNKNOWN2: " + rawType);
return null;
}
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
/**
* Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by
* a class.
*/
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
return genericDeclaration instanceof Class
? (Class<?>) genericDeclaration
: null;
}
protected boolean isLocal(Type parameterType, GenericDeclaration member) {
if (parameterType instanceof TypeVariable<?>) {
return member.equals(((TypeVariable<?>) parameterType).getGenericDeclaration());
} else if (parameterType instanceof GenericArrayType) {
return isLocal(((GenericArrayType) parameterType).getGenericComponentType(), member);
}
return false;
}
@Override
public boolean equals(Object o) {
if (o instanceof TypeVariable &&
o.getClass() == TypeVariableImpl.class) {
TypeVariable<?> that = (TypeVariable<?>) o;
GenericDeclaration thatDecl = that.getGenericDeclaration();
String thatName = that.getName();
return Objects.equals(genericDeclaration, thatDecl) &&
Objects.equals(name, thatName);
} else
return false;
}
/**
* Factory method.
* @param decl - the reflective object that declared the type variable
* that this method should create
* @param name - the name of the type variable to be returned
* @param bs - an array of ASTs representing the bounds for the type
* variable to be created
* @param f - a factory that can be used to manufacture reflective
* objects that represent the bounds of this type variable
* @return A type variable with name, bounds, declaration and factory
* specified
*/
public static <T extends GenericDeclaration>
TypeVariableImpl<T> make(T decl, String name,
FieldTypeSignature[] bs,
GenericsFactory f) {
if (!((decl instanceof Class) ||
(decl instanceof Method) ||
(decl instanceof Constructor))) {
throw new AssertionError("Unexpected kind of GenericDeclaration" +
decl.getClass().toString());
}
return new TypeVariableImpl<T>(decl, name, bs, f);
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
protected Class<?> resolveParameterType(Type type) {
if(type instanceof Class) {
return (Class<?>) type;
} else if(type instanceof ParameterizedType
&& ((ParameterizedType) type).getRawType() instanceof Class
&& Collection.class.isAssignableFrom((Class<?>) ((ParameterizedType) type).getRawType())) {
// GenericDAO中的集合泛型参数
return resolveParameterType(((ParameterizedType) type).getActualTypeArguments()[0]);
} else if(type instanceof TypeVariable) {
// GenericDAO中的非集合泛型参数,或许是主键或许是实体。
GenericDeclaration genericDeclaration = ((TypeVariable<?>) type).getGenericDeclaration();
if(genericDeclaration == GenericDAO.class) {
Type[] bounds = ((TypeVariable<?>) type).getBounds();
if(bounds[0] == Object.class) {
String name = ((TypeVariable<?>) type).getName();
if("E".equals(name)) {
return entityType;
} else if("ID".equals(name)) {
return primaryKeyType;
} else {
throw new MappingException("Unknown type variable \"" + type + "\".");
}
} else {
return resolveParameterType(bounds[0]);
}
} else {
throw new MappingException("Unsupported generic declaration \"" + genericDeclaration + "\".");
}
}
throw new MappingException("Unknown type \"" + type + "\".");
}
/**
* Return the type of a type parameter element of a given type element when that type parameter
* element is parameterized by a sub type, directly or indirectly. When the type parameter cannot
* be resolve, null is returned.
*
* @param type the sub type for which the type parameter is parameterized
* @param typeParam the type parameter to resolve
* @return the type parameterizing the type parameter
*/
public static <T> Type resolveTypeParameter(Type type, java.lang.reflect.TypeVariable<java.lang.Class<T>> typeParam) {
if (type instanceof Class<?>) {
Class<?> classType = (Class<?>) type;
if (Stream.of(classType.getTypeParameters()).filter(tp -> tp.equals(typeParam)).findFirst().isPresent()) {
return typeParam;
}
List<Type> superTypes = new ArrayList<>();
if (classType.getGenericSuperclass() != null) {
superTypes.add(classType.getGenericSuperclass());
}
Collections.addAll(superTypes, classType.getGenericInterfaces());
for (Type superType : superTypes) {
Type resolved = resolveTypeParameter(superType, typeParam);
if (resolved != null) {
return resolved;
}
}
} else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
Type rawType = parameterizedType.getRawType();
Type resolvedType = resolveTypeParameter(rawType, typeParam);
if (resolvedType instanceof java.lang.reflect.TypeVariable<?>) {
GenericDeclaration owner = ((java.lang.reflect.TypeVariable) resolvedType).getGenericDeclaration();
if (owner.equals(rawType)) {
java.lang.reflect.TypeVariable<?>[] typeParams = owner.getTypeParameters();
for (int i = 0;i < typeParams.length;i++) {
if (typeParams[i].equals(resolvedType)) {
return parameterizedType.getActualTypeArguments()[i];
}
}
}
}
} else {
throw new UnsupportedOperationException("Todo " + type.getTypeName() + " " + type.getClass().getName());
}
return null;
}
private UnknownGenericException(final Class<?> contextType,
final String genericName, final GenericDeclaration genericSource,
final Throwable cause) {
super(String.format("Generic '%s'%s is not declared %s",
genericName, formatSource(genericSource),
contextType == null ? "" : "on type " + contextType.getName()), cause);
this.contextType = contextType;
this.genericName = genericName;
this.genericSource = genericSource;
}
@Override
public boolean equals(Object o) {
if (o instanceof TypeVariable &&
o.getClass() == TypeVariableImpl.class) {
TypeVariable<?> that = (TypeVariable<?>) o;
GenericDeclaration thatDecl = that.getGenericDeclaration();
String thatName = that.getName();
return Objects.equals(genericDeclaration, thatDecl) &&
Objects.equals(name, thatName);
} else
return false;
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
private static void check2(Type t, String what) {
if (t instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) t;
check(pt.getActualTypeArguments(), "type argument", what);
} else if (t instanceof TypeVariable) {
TypeVariable<?> tv = (TypeVariable<?>) t;
check(tv.getBounds(), "bound", what);
GenericDeclaration gd = tv.getGenericDeclaration();
if (gd instanceof Type)
check((Type) gd, "declaration containing " + what);
} else if (t instanceof WildcardType) {
WildcardType wt = (WildcardType) t;
check(wt.getLowerBounds(), "lower bound", "wildcard type in " + what);
check(wt.getUpperBounds(), "upper bound", "wildcard type in " + what);
} else if (t instanceof Class<?>) {
Class<?> c = (Class<?>) t;
check(c.getGenericInterfaces(), "superinterface", c.toString());
check(c.getGenericSuperclass(), "superclass of " + c);
check(c.getTypeParameters(), "type parameter", c.toString());
} else if (t instanceof GenericArrayType) {
GenericArrayType gat = (GenericArrayType) t;
Type comp = gat.getGenericComponentType();
if (comp instanceof Class) {
fail("Type " + t + " uses GenericArrayType when plain " +
"array would do, in " + what);
} else
check(comp, "component type of " + what);
} else {
fail("TEST BUG: mutant Type " + t + " (a " + t.getClass().getName() + ")");
}
}
/**
* Returns the declaring class of {@code typeVariable}, or {@code null} if it was not declared by
* a class.
*/
private static Class<?> declaringClassOf(TypeVariable<?> typeVariable) {
GenericDeclaration genericDeclaration = typeVariable.getGenericDeclaration();
return genericDeclaration instanceof Class
? (Class<?>) genericDeclaration
: null;
}
private CoreReflectionFactory(GenericDeclaration d, Scope s) {
decl = d;
scope = s;
}
public GenericDeclarationWrapper(GenericDeclaration element) {
super(element);
}
public BinderArg(GenericDeclaration decl, Type[] args) {
this(decl.getTypeParameters(), args);
}
private CoreReflectionFactory(GenericDeclaration d, Scope s) {
decl = d;
scope = s;
}