Java Code Examples for java.lang.reflect.TypeVariable#getName()

The following examples show how to use java.lang.reflect.TypeVariable#getName() . 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: ClassTo.java    From sundrio with Apache License 2.0 6 votes vote down vote up
public TypeParamDef apply(Type item) {
    if (item instanceof TypeVariable) {
        TypeVariable typeVariable = (TypeVariable) item;
        String name = typeVariable.getName();
        List<ClassRef> bounds = new ArrayList<ClassRef>();

        for (Type b : typeVariable.getBounds()) {
            if (b instanceof Class) {
                Class c = (Class) b;
                bounds.add((ClassRef) TYPEREF.apply(c));
            }
        }
        return new TypeParamDefBuilder().withName(name).withBounds(bounds).build();
    }
    return null;
}
 
Example 2
Source File: TypeVariableImpl.java    From java-n-IDE-for-Android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean equals(Object o) {
    if (o instanceof TypeVariable) {
        TypeVariable that = (TypeVariable) o;

        GenericDeclaration thatDecl = that.getGenericDeclaration();
        String thatName = that.getName();

        return
            (genericDeclaration == null ?
             thatDecl == null :
             genericDeclaration.equals(thatDecl)) &&
            (name == null ?
             thatName == null :
             name.equals(thatName));

    } else
        return false;
}
 
Example 3
Source File: GenericTypeResolver.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
private static String getInternalGenericName(Type genericType, int i) {    
    String genericName=null;
    if (genericType instanceof ParameterizedType) { // 处理多级泛型   
        ParameterizedType subParameterizedType= (ParameterizedType) genericType;
        genericName=subParameterizedType.getTypeName();
    } else if (genericType instanceof GenericArrayType) { // 处理数组泛型  
        GenericArrayType genericArrayType= (GenericArrayType) genericType;
        genericType = genericArrayType.getGenericComponentType();
        //再递归一次
        genericName =getInternalGenericName(genericType,i); 
    } else if (genericType instanceof TypeVariable) { // 处理泛型擦拭对象     
        TypeVariable<?> typeVariable =(TypeVariable<?>) genericType;
        genericName =typeVariable.getName();
    } else if (genericType instanceof WildcardType){
        WildcardType wildcardType =((WildcardType)genericType);
        genericName=wildcardType.getTypeName();
    } 
    
    return genericName;
}
 
Example 4
Source File: CachedClassMirrors.java    From Concurnas with MIT License 6 votes vote down vote up
@Override
public String[] getGenericArguments() {
	//throw new RuntimeException("getGenericArguments not implemneted for SimpleClassDefProperties");
	//TODO: better to implement this via processing of signature?
	if(null == genArguments) {
		TypeVariable[] tvs;
		try {
			tvs = Class.forName(this.name.replace('/', '.')).getTypeParameters();
		} catch (ClassNotFoundException e) {
			genArguments = new String[0];
			return genArguments;
		}
		
		int n=0;
		genArguments = new String[tvs.length];
		for(TypeVariable tva : tvs) {
			genArguments[n++] = tva.getName();
		}
	}
	return genArguments;
}
 
Example 5
Source File: ReflectTst.java    From stategen with GNU Affero General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
    Class<?> clz = ReflectTst.class;
    Method[] declaredMethods = clz.getDeclaredMethods();
    for (Method method : declaredMethods) {
        String methodName = method.getName();
        if (methodName.startsWith("get")) {
            System.out.println("name<===========>:" + methodName);
            Type genericReturnType2 = method.getGenericReturnType();
            if (genericReturnType2 instanceof ParameterizedType) {
                ParameterizedType parameterizedType = (ParameterizedType) genericReturnType2;
                Type type = parameterizedType.getActualTypeArguments()[0];
                if (type instanceof TypeVariable) {
                    TypeVariable<?> typeVariable = (TypeVariable<?>) type;
                    String name = typeVariable.getName();
                    System.out.println("name<===========>:" + name);
                }
            }
        }
    }
}
 
Example 6
Source File: TypeVariableImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 7
Source File: TypeVariableImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 8
Source File: Class.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
static String typeVarBounds(TypeVariable<?> typeVar) {
    Type[] bounds = typeVar.getBounds();
    if (bounds.length == 1 && bounds[0].equals(Object.class)) {
        return typeVar.getName();
    } else {
        return typeVar.getName() + " extends " +
            Arrays.stream(bounds)
            .map(Type::getTypeName)
            .collect(Collectors.joining(" & "));
    }
}
 
Example 9
Source File: TypeVariableImpl.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 10
Source File: TypeVariableImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: TypeVariableImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 12
Source File: TypeUtils.java    From dolphin-platform with Apache License 2.0 5 votes vote down vote up
/**
 * Format a {@link TypeVariable} as a {@link String}.
 *
 * @param v {@code TypeVariable} to format
 * @return String
 * @since 3.2
 */
private static String typeVariableToString(final TypeVariable<?> v) {
    final StringBuilder buf = new StringBuilder(v.getName());
    final Type[] bounds = v.getBounds();
    if (bounds.length > 0 && !(bounds.length == 1 && Object.class.equals(bounds[0]))) {
        buf.append(" extends ");
        appendAllTo(buf, " & ", v.getBounds());
    }
    return buf.toString();
}
 
Example 13
Source File: TypeUtil.java    From spring-auto-restdocs with Apache License 2.0 5 votes vote down vote up
public static Type findTypeFromTypeVariable(TypeVariable typeVariable, Class<?> clazz) {
    String variableName = typeVariable.getName();
    Map<TypeVariable, Type> typeMap = GenericTypeResolver.getTypeVariableMap(clazz);
    for (TypeVariable tv : typeMap.keySet()) {
        if (StringUtils.equals(tv.getName(), variableName)) {
            return typeMap.get(tv);
        }
    }
    return Object.class;
}
 
Example 14
Source File: TypeVariableImpl.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 15
Source File: TypeVariableImpl.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 16
Source File: TypeVariableImpl.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
@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;
}
 
Example 17
Source File: RemoteApiScanListener.java    From mPaaS with Apache License 2.0 5 votes vote down vote up
/** 添加泛型参数实际类型 */
private void appendVarType(Class<?> root, Class<?> iface,
                           Map<String, String> varTypes) {
    TypeVariable<?>[] params = iface.getTypeParameters();
    for (TypeVariable<?> param : params) {
        String name = param.getName();
        Class<?> clazz = PluginReflectUtil.getActualClass(root, iface,
                name);
        if (clazz != null) {
            varTypes.put(name, clazz.getName());
        }
    }
}
 
Example 18
Source File: TypeParameterMatcher.java    From live-chat-engine with Apache License 2.0 4 votes vote down vote up
private static Class<?> find0(
        final Object object, Class<?> parameterizedSuperclass, String typeParamName) {

    final Class<?> thisClass = object.getClass();
    Class<?> currentClass = thisClass;
    for (;;) {
        if (currentClass.getSuperclass() == parameterizedSuperclass) {
            int typeParamIndex = -1;
            TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
            for (int i = 0; i < typeParams.length; i ++) {
                if (typeParamName.equals(typeParams[i].getName())) {
                    typeParamIndex = i;
                    break;
                }
            }

            if (typeParamIndex < 0) {
                throw new IllegalStateException(
                        "unknown type parameter '" + typeParamName + "': " + parameterizedSuperclass);
            }

            Type genericSuperType = currentClass.getGenericSuperclass();
            if (!(genericSuperType instanceof ParameterizedType)) {
                return Object.class;
            }

            Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();

            Type actualTypeParam = actualTypeParams[typeParamIndex];
            if (actualTypeParam instanceof ParameterizedType) {
                actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
            }
            if (actualTypeParam instanceof Class) {
                return (Class<?>) actualTypeParam;
            }
            if (actualTypeParam instanceof GenericArrayType) {
                Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
                if (componentType instanceof ParameterizedType) {
                    componentType = ((ParameterizedType) componentType).getRawType();
                }
                if (componentType instanceof Class) {
                    return Array.newInstance((Class<?>) componentType, 0).getClass();
                }
            }
            if (actualTypeParam instanceof TypeVariable) {
                // Resolved type parameter points to another type parameter.
                TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
                currentClass = thisClass;
                if (!(v.getGenericDeclaration() instanceof Class)) {
                    return Object.class;
                }

                parameterizedSuperclass = (Class<?>) v.getGenericDeclaration();
                typeParamName = v.getName();
                if (parameterizedSuperclass.isAssignableFrom(thisClass)) {
                    continue;
                } else {
                    return Object.class;
                }
            }

            return fail(thisClass, typeParamName);
        }
        currentClass = currentClass.getSuperclass();
        if (currentClass == null) {
            return fail(thisClass, typeParamName);
        }
    }
}
 
Example 19
Source File: TypeParameterMatcher.java    From netty-4.1.22 with Apache License 2.0 4 votes vote down vote up
private static Class<?> find0(
        final Object object, Class<?> parametrizedSuperclass, String typeParamName) {

    final Class<?> thisClass = object.getClass();
    Class<?> currentClass = thisClass;
    for (;;) {
        if (currentClass.getSuperclass() == parametrizedSuperclass) {
            int typeParamIndex = -1;
            TypeVariable<?>[] typeParams = currentClass.getSuperclass().getTypeParameters();
            for (int i = 0; i < typeParams.length; i ++) {
                if (typeParamName.equals(typeParams[i].getName())) {
                    typeParamIndex = i;
                    break;
                }
            }

            if (typeParamIndex < 0) {
                throw new IllegalStateException(
                        "unknown type parameter '" + typeParamName + "': " + parametrizedSuperclass);
            }

            Type genericSuperType = currentClass.getGenericSuperclass();
            if (!(genericSuperType instanceof ParameterizedType)) {
                return Object.class;
            }

            Type[] actualTypeParams = ((ParameterizedType) genericSuperType).getActualTypeArguments();

            Type actualTypeParam = actualTypeParams[typeParamIndex];
            if (actualTypeParam instanceof ParameterizedType) {
                actualTypeParam = ((ParameterizedType) actualTypeParam).getRawType();
            }
            if (actualTypeParam instanceof Class) {
                return (Class<?>) actualTypeParam;
            }
            if (actualTypeParam instanceof GenericArrayType) {
                Type componentType = ((GenericArrayType) actualTypeParam).getGenericComponentType();
                if (componentType instanceof ParameterizedType) {
                    componentType = ((ParameterizedType) componentType).getRawType();
                }
                if (componentType instanceof Class) {
                    return Array.newInstance((Class<?>) componentType, 0).getClass();
                }
            }
            if (actualTypeParam instanceof TypeVariable) {
                // Resolved type parameter points to another type parameter.
                TypeVariable<?> v = (TypeVariable<?>) actualTypeParam;
                currentClass = thisClass;
                if (!(v.getGenericDeclaration() instanceof Class)) {
                    return Object.class;
                }

                parametrizedSuperclass = (Class<?>) v.getGenericDeclaration();
                typeParamName = v.getName();
                if (parametrizedSuperclass.isAssignableFrom(thisClass)) {
                    continue;
                } else {
                    return Object.class;
                }
            }

            return fail(thisClass, typeParamName);
        }
        currentClass = currentClass.getSuperclass();
        if (currentClass == null) {
            return fail(thisClass, typeParamName);
        }
    }
}
 
Example 20
Source File: BaseType.java    From baratine with GNU General Public License v2.0 4 votes vote down vote up
private static BaseType createVar(Type type,
                                  HashMap<String,BaseType> paramMap,
                                  String paramDeclName,
                                  Type parentType,
                                  ClassFill classFill)
{
  TypeVariable aType = (TypeVariable) type;

  BaseType actualType = null;
  
  String aTypeName = aType.getName();

  if (paramMap != null) {
    actualType = (BaseType) paramMap.get(aTypeName);

    if (actualType != null)
      return actualType;
    
    if (paramDeclName != null) {
      aTypeName = paramDeclName + "_" + aType.getName();

      actualType = (BaseType) paramMap.get(aTypeName);
    }

    if (actualType != null)
      return actualType;
  }
  
  String varName;
  
  if (paramMap != null)
    varName = createVarName(paramMap, paramDeclName);
  else
    varName = aType.getName();
  
  BaseType []baseBounds;

  if (aType.getBounds() != null) {
    Type []bounds = aType.getBounds();

    baseBounds = new BaseType[bounds.length];

    for (int i = 0; i < bounds.length; i++) {
      // ejb/1243 - Enum
      if (bounds[i] != parentType)
        baseBounds[i] = create(bounds[i], 
                               paramMap, paramDeclName, 
                               type, ClassFill.TARGET);
      else
        baseBounds[i] = ObjectType.OBJECT_TYPE;
    }
  }
  else
    baseBounds = new BaseType[0];
  
  VarType<?> varType = new VarType(varName, baseBounds);
  
  if (paramMap != null)
    paramMap.put(aTypeName, varType);
  
  return varType;
}