Java Code Examples for java.lang.reflect.GenericDeclaration#getTypeParameters()

The following examples show how to use java.lang.reflect.GenericDeclaration#getTypeParameters() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeParamInfo.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 2
Source File: FieldInfo.java    From BigApp_Discuz_Android with Apache License 2.0 6 votes vote down vote up
public static Type getInheritGenericType(Class<?> clazz, TypeVariable<?> tv) {
    Type type = null;
    GenericDeclaration gd = tv.getGenericDeclaration();
    do {
        type = clazz.getGenericSuperclass();
        if (type == null) {
            return null;
        }
        if (type instanceof ParameterizedType) {
            ParameterizedType ptype = (ParameterizedType) type;
            if (ptype.getRawType() == gd) {
                TypeVariable<?>[] tvs = gd.getTypeParameters();
                Type[] types = ptype.getActualTypeArguments();
                for (int i = 0; i < tvs.length; i++) {
                    if (tvs[i] == tv)
                        return types[i];
                }
                return null;
            }
        }
        clazz = TypeUtils.getClass(type);
    } while (type != null);
    return null;
}
 
Example 3
Source File: InjectionUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
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;
    }
 
Example 4
Source File: Helper.java    From vertx-codegen with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 5
Source File: GenericsUtil.java    From doma with Apache License 2.0 5 votes vote down vote up
private int getTypeParameterIndex(
    GenericDeclaration genericDeclaration, TypeVariable<?> typeVariable) {
  Type[] types = genericDeclaration.getTypeParameters();
  for (int i = 0, len = types.length; i < len; i++) {
    if (types[i] == typeVariable) {
      return i;
    }
  }
  return 0;
}
 
Example 6
Source File: TypeVariableImpl.java    From j2objc with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 7
Source File: Types.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 8
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
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));
		}
	}
}
 
Example 9
Source File: TypeRefBase.java    From baratine with GNU General Public License v2.0 5 votes vote down vote up
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;
  }
}
 
Example 10
Source File: JavaWriter.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
private void printTypeVariable(TypeVariable<?> typeVariable)
  throws IOException
{
  print(typeVariable.getName());
  
  if (true)
    return;

  Type[] bounds = typeVariable.getBounds();

  if ((bounds != null) && (bounds.length > 0)) {
    print(" extends ");

    for (int i = 0; i < bounds.length; i++) {
      if (i != 0) {
        print(" & ");
      }

      printType(bounds[i]);
    }
  }

  GenericDeclaration genericDeclaration
  = typeVariable.getGenericDeclaration();

  Type[] typeParameters = null;

  typeParameters = genericDeclaration.getTypeParameters();

  if ((typeParameters != null) && (typeParameters.length > 0)) {
    print("<");

    for (int i = 0; i < typeParameters.length; i++) {
      if (i != 0) {
        print(", ");
      }

      printType(typeParameters[i]);
    }

    print(">");
  }
}
 
Example 11
Source File: ReflectionNavigator.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 12
Source File: ReflectionNavigator.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 13
Source File: TypeUtil.java    From blueocean-plugin with MIT License 4 votes vote down vote up
public BinderArg( GenericDeclaration decl, Type[] args ) {
    this(decl.getTypeParameters(),args);
}
 
Example 14
Source File: ReflectionNavigator.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 15
Source File: ReflectionNavigator.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 16
Source File: ReflectionNavigator.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 17
Source File: ReflectionNavigator.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 18
Source File: ReflectionNavigator.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 19
Source File: ReflectionNavigator.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
public BinderArg(GenericDeclaration decl, Type[] args) {
    this(decl.getTypeParameters(), args);
}
 
Example 20
Source File: TypeList.java    From byte-buddy with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a list of the type variables of the supplied generic declaration.
 *
 * @param genericDeclaration The generic declaration to represent.
 * @return A generic type list for the returned generic declaration.
 */
public static Generic of(GenericDeclaration genericDeclaration) {
    return new OfTypeVariables(genericDeclaration.getTypeParameters());
}