@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; }
private static Type translateTypeVariable(Map<String, Type> lookup, TypeVariable var) { GenericDeclaration declaredBy = var.getGenericDeclaration(); if (!(declaredBy instanceof Class)) { // if the <T> is not defined by class, there is no way to get the actual type return Object.class; } Class clazz = (Class) declaredBy; Type actualType = lookup.get(var.getName() + "@" + clazz.getCanonicalName()); if (actualType == null) { // should not happen return Object.class; } if (actualType instanceof TypeVariable) { // translate to another variable, try again return translateTypeVariable(lookup, (TypeVariable) actualType); } return actualType; }
private static JavaType createByParamName(final ImplementClass implementClass, final TypeVariable<?> variable) { GenericDeclaration declaration = variable.getGenericDeclaration(); String name = variable.getName(); TypeVariable<?>[] typeParameters = declaration.getTypeParameters(); for (int index = 0; index < typeParameters.length; index++) { TypeVariable<?> type = typeParameters[index]; if (name.equals(type.getName())) { if (declaration instanceof Class) { Class<?> declarationClass = (Class<?>) declaration; Class<?> sub = implementClass.getSubclass(declarationClass); if (sub != null) { Type genericSuperclass = implementClass.getGenericParentClass(declarationClass); if (genericSuperclass instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass; return create(implementClass, parameterizedType.getActualTypeArguments()[index]); } // Generics未指定 } return new VariableJavaType(implementClass, type); } else if (declaration instanceof Executable) { return new VariableJavaType(implementClass, type); } } } throw new IllegalArgumentException(); }
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; }
protected <T extends Member & GenericDeclaration> void enhanceExecutable(JvmExecutable result, T member, String simpleName, Type[] parameterTypes, Annotation[][] annotations, int offset) { StringBuilder fqName = new StringBuilder(48); fqName.append(member.getDeclaringClass().getName()); fqName.append('.'); fqName.append(simpleName); fqName.append('('); InternalEList<JvmFormalParameter> parameters = (InternalEList<JvmFormalParameter>)result.getParameters(); for (int typeIdx = offset, annotationIdx = annotations.length - parameterTypes.length + offset; typeIdx < parameterTypes.length; typeIdx++, annotationIdx++) { if (typeIdx != offset) fqName.append(','); Type parameterType = parameterTypes[typeIdx]; uriHelper.computeTypeName(parameterType, fqName); parameters.addUnique( createFormalParameter(parameterType, "arg" + (typeIdx - offset), result, member, annotations[annotationIdx])); } fqName.append(')'); result.internalSetIdentifier(fqName.toString()); result.setSimpleName(simpleName); setVisibility(result, member.getModifiers()); }
protected JvmFormalParameter createFormalParameter(Type parameterType, String paramName, JvmMember container, GenericDeclaration member, Annotation[] annotations) { JvmFormalParameter result = TypesFactory.eINSTANCE.createJvmFormalParameter(); result.setName(paramName); if (isLocal(parameterType, member)) { result.setParameterType(createLocalTypeReference(parameterType, (JvmTypeParameterDeclarator) container, member)); } else { result.setParameterType(createTypeReference(parameterType)); } if (annotations.length != 0) { InternalEList<JvmAnnotationReference> annotationsReferences = (InternalEList<JvmAnnotationReference>)result.getAnnotations(); for (Annotation annotation : annotations) { annotationsReferences.addUnique(createAnnotationReference(annotation)); } } return result; }
Type resolve() { GenericDeclaration d = decl; while (d != null) { for (TypeVariable t : d.getTypeParameters()) { if (t.getName().equals(name)) { return t; } } d = getParent(d); } throw new MalformedParameterizedTypeException(); }
private static Type resolve(TypeVariable<? extends GenericDeclaration> value, Set<ParameterizedType> types) { GenericDeclaration genericDeclaration = value.getGenericDeclaration(); if (genericDeclaration instanceof Class<?>) { Class<?> clazz = (Class<?>) genericDeclaration; Optional<ParameterizedType> matching = types.stream() .filter(type -> type.getRawType() == clazz) .sorted(Types::byMostConcrete) .findFirst(); if (matching.isPresent()) { TypeVariable<?>[] params = genericDeclaration.getTypeParameters(); Type[] actual = matching.get().getActualTypeArguments(); for (int i = 0; i < params.length && i < actual.length; i++) { if (params[i] == value) { return actual[i]; } } } } return value; }
/** * Format a {@link TypeVariable} including its {@link GenericDeclaration}. * * @param var the type variable to create a String representation for, not {@code null} * @return String * @since 3.2 */ public static String toLongString(final TypeVariable<?> var) { Validate.notNull(var, "var is null"); final StringBuilder buf = new StringBuilder(); final GenericDeclaration d = ((TypeVariable<?>) var).getGenericDeclaration(); if (d instanceof Class<?>) { Class<?> c = (Class<?>) d; while (true) { if (c.getEnclosingClass() == null) { buf.insert(0, c.getName()); break; } buf.insert(0, c.getSimpleName()).insert(0, '.'); c = c.getEnclosingClass(); } } else if (d instanceof Type) {// not possible as of now buf.append(toString((Type) d)); } else { buf.append(d); } return buf.append(':').append(typeVariableToString(var)).toString(); }
void resolve() { if (formalVar != null) { return; } GenericDeclaration curLayer = declOfVarUser; TypeVariable var; while ((var = findFormalVar(curLayer, name)) == null) { curLayer = nextLayer(curLayer); if (curLayer == null) { throw new AssertionError("illegal type variable reference"); } } formalVar = (TypeVariableImpl<D>) var; this.genericDeclaration = formalVar.genericDeclaration; this.bounds = formalVar.bounds; }
/** * 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; } } }
/** * Parses the generic signature of a method 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 parseForMethod(GenericDeclaration genericDecl, String signature, Class<?>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { Method m = (Method) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; Class<?>[] parameterTypes = m.getParameterTypes(); if (parameterTypes.length == 0) { this.parameterTypes = ListOfTypes.EMPTY; } else { this.parameterTypes = new ListOfTypes(parameterTypes); } Class<?>[] exceptionTypes = m.getExceptionTypes(); if (exceptionTypes.length == 0) { this.exceptionTypes = ListOfTypes.EMPTY; } else { this.exceptionTypes = new ListOfTypes(exceptionTypes); } this.returnType = m.getReturnType(); } }
/** * Parses the generic signature of a constructor 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 parseForConstructor(GenericDeclaration genericDecl, String signature, Class<?>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { Constructor c = (Constructor) genericDecl; this.formalTypeParameters = EmptyArray.TYPE_VARIABLE; Class<?>[] parameterTypes = c.getParameterTypes(); if (parameterTypes.length == 0) { this.parameterTypes = ListOfTypes.EMPTY; } else { this.parameterTypes = new ListOfTypes(parameterTypes); } Class<?>[] exceptionTypes = c.getExceptionTypes(); if (exceptionTypes.length == 0) { this.exceptionTypes = ListOfTypes.EMPTY; } else { this.exceptionTypes = new ListOfTypes(exceptionTypes); } } }
TypeVariableImpl<GenericDeclaration> parseFormalTypeParameter() { // FormalTypeParameter ::= Ident ClassBound {InterfaceBound}. scanIdentifier(); String name = identifier.intern(); // FIXME: is this o.k.? ListOfTypes bounds = new ListOfTypes(8); // ClassBound ::= ":" [FieldTypeSignature]. expect(':'); if (symbol == 'L' || symbol == '[' || symbol == 'T') { bounds.add(parseFieldTypeSignature()); } while (symbol == ':') { // InterfaceBound ::= ":" FieldTypeSignature. scanSymbol(); bounds.add(parseFieldTypeSignature()); } return new TypeVariableImpl<GenericDeclaration>(genericDecl, name, bounds); }
/** * Format a {@link TypeVariable} including its {@link GenericDeclaration}. * * @param var the type variable to create a String representation for, not {@code null} * @return String * @since 3.2 */ public static String toLongString(TypeVariable<?> var) { Validate.notNull(var, "var is null"); final StringBuilder buf = new StringBuilder(); final GenericDeclaration d = ((TypeVariable<?>) var).getGenericDeclaration(); if (d instanceof Class<?>) { Class<?> c = (Class<?>) d; while (true) { if (c.getEnclosingClass() == null) { buf.insert(0, c.getName()); break; } buf.insert(0, c.getSimpleName()).insert(0, '.'); c = c.getEnclosingClass(); } } else if (d instanceof Type) {// not possible as of now buf.append(toString((Type) d)); } else { buf.append(d); } return buf.append(':').append(typeVariableToString(var)).toString(); }
/** * 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 = ListOfVariables.empty; this.superclassType = c.getSuperclass(); this.interfaceTypes = new ListOfTypes(c.getInterfaces()); } else { this.formalTypeParameters = ListOfVariables.empty; this.superclassType = Object.class; this.interfaceTypes = ListOfTypes.EMPTY; } } }
/** * Parses the generic signature of a method 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 parseForMethod(GenericDeclaration genericDecl, String signature, Class<?>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { if(genericDecl instanceof Method) { Method m = (Method) genericDecl; this.formalTypeParameters = ListOfVariables.empty; this.parameterTypes = new ListOfTypes(m.getParameterTypes()); this.exceptionTypes = new ListOfTypes(m.getExceptionTypes()); this.returnType = m.getReturnType(); } else { this.formalTypeParameters = ListOfVariables.empty; this.parameterTypes = ListOfTypes.EMPTY; this.exceptionTypes = ListOfTypes.EMPTY; this.returnType = void.class; } } }
/** * Parses the generic signature of a constructor 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 parseForConstructor(GenericDeclaration genericDecl, String signature, Class<?>[] rawExceptionTypes) { setInput(genericDecl, signature); if (!eof) { parseMethodTypeSignature(rawExceptionTypes); } else { if(genericDecl instanceof Constructor) { Constructor c = (Constructor) genericDecl; this.formalTypeParameters = ListOfVariables.empty; this.parameterTypes = new ListOfTypes(c.getParameterTypes()); this.exceptionTypes = new ListOfTypes(c.getExceptionTypes()); } else { this.formalTypeParameters = ListOfVariables.empty; this.parameterTypes = ListOfTypes.EMPTY; this.exceptionTypes = ListOfTypes.EMPTY; } } }
ImplForVariable<GenericDeclaration> parseFormalTypeParameter() { // FormalTypeParameter ::= Ident ClassBound {InterfaceBound}. scanIdentifier(); String name = identifier.intern(); // FIXME: is this o.k.? ListOfTypes bounds = new ListOfTypes(8); // ClassBound ::= ":" [FieldTypeSignature]. expect(':'); if (symbol == 'L' || symbol == '[' || symbol == 'T') { bounds.add(parseFieldTypeSignature()); } while (symbol == ':') { // InterfaceBound ::= ":" FieldTypeSignature. scanSymbol(); bounds.add(parseFieldTypeSignature()); } return new ImplForVariable<GenericDeclaration>(genericDecl, name, bounds); }
static GenericDeclaration nextLayer(GenericDeclaration decl) { if (decl instanceof Class) { // FIXME: Is the following hierarchy correct?: Class cl = (Class)decl; decl = cl.getEnclosingMethod(); if (decl != null) { return decl; } decl = cl.getEnclosingConstructor(); if (decl != null) { return decl; } return cl.getEnclosingClass(); } else if (decl instanceof Method) { return ((Method)decl).getDeclaringClass(); } else if (decl instanceof Constructor) { return ((Constructor)decl).getDeclaringClass(); } throw new RuntimeException("unknown GenericDeclaration2: " + decl.toString()); }
void resolve() { if (formalVar == null) { GenericDeclaration curLayer = declOfVarUser; TypeVariable var = null; do { var = findFormalVar(curLayer, name); if (var != null) break; else { curLayer = nextLayer(curLayer); if (curLayer == null) break; // FIXME: SHOULD NEVER HAPPEN! // throw exception: illegal // type variable reference. } } while (true); formalVar = (ImplForVariable<D>)var; this.genericDeclaration = formalVar.genericDeclaration; this.bounds = formalVar.bounds; } }
/** * ################################################################################ * for j.l.Class * ################################################################################ */ @SuppressWarnings("unchecked") public static TypeVariable[] getTypeParameters(Class c, String rawSignature) { TypeVariable[] typeParameters = null; //So, here it can be only TypeVariable elements. Object startPoint = c; String signature = AuxiliaryUtil.toUTF8(rawSignature); // getting this class signature if (signature == null) { return typeParameters = new TypeVariable[0]; } InterimClassGenericDecl decl = (InterimClassGenericDecl) Parser.parseSignature(signature, SignatureKind.CLASS_SIGNATURE, (GenericDeclaration)startPoint); // GenericSignatureFormatError can be thrown here InterimTypeParameter[] pTypeParameters = decl.typeParameters; if (pTypeParameters == null) { return typeParameters = new TypeVariable[0]; } int l = pTypeParameters.length; typeParameters = new TypeVariable[l]; for (int i = 0; i < l; i++) { String tvName = pTypeParameters[i].typeParameterName; TypeVariable variable = new TypeVariableImpl((GenericDeclaration)c, tvName, decl.typeParameters[i]); TypeVariableRepository.registerTypeVariable(variable, tvName, startPoint); typeParameters[i] = variable; } return typeParameters; }
/** * This method returns generic declaration where a type variable is defined in. * * @param typeVariableName a name of a type variable. * @param startPoint an instance of the Class, Method, Constructor or Field type to start the search * of a type variable declaration place. * @return the found generic declaration for this type variable definition or null * if a generic declaration for this type variable does not exist at all. */ public static GenericDeclaration findGenericDeclarationForTypeVariable(String typeVariableName, Object startPoint) { if (startPoint instanceof Class || startPoint instanceof Member) { Class klass; if (startPoint instanceof Class) { klass = (Class) startPoint; } else { // Member: Field, Method or Constructor if (!(startPoint instanceof Field)) { // Method or Constructor if (hasGenericDeclaration(typeVariableName, (GenericDeclaration) startPoint)) { return (GenericDeclaration) startPoint; } } klass = ((Member) startPoint).getDeclaringClass(); } while (klass != null) { if (hasGenericDeclaration(typeVariableName, klass)) { return klass; } klass = klass.getDeclaringClass(); } } return null; }
/** * This method returns TypeVariable corresponding to the name of type variable in the current scope. * * @param typeVariableName a name of a type variable. * @param startPoint an instance of the Class, Method, Constructor or Field type to start the search * of a type variable declaration place. * @return the found type variable. */ public static TypeVariable findTypeVariable(String typeVariableName, Object startPoint) { typeVariableName = transform(typeVariableName); // Is it needed at all? if (startPoint instanceof Class || startPoint instanceof Member) { Class klass; TypeVariable ret; if (startPoint instanceof Class) { klass = (Class) startPoint; } else { // Member: Field, Method or Constructor if (!(startPoint instanceof Field)) { // Method or Constructor ret = findTypeVariableInDeclaration(typeVariableName, (GenericDeclaration) startPoint); if (ret != null) return ret; } klass = ((Member) startPoint).getDeclaringClass(); } while (klass != null) { ret = findTypeVariableInDeclaration(typeVariableName, klass); if (ret != null) return ret; klass = klass.getDeclaringClass(); } } return null; }
/** * unnested generalized local class */ public <T0> void test_14() { @i class LC000<T1 extends T0> { } //@i class LC001<T1 extends T0 &java.io.Serializable &java.lang.reflect.Type &java.lang.reflect.GenericDeclaration> { @i class LC001<T1 extends LC000 &java.io.Serializable &java.lang.reflect.Type &GenericDeclaration> { java.lang.reflect.TypeVariable<?>[] getTypeParameters() { return (java.lang.reflect.TypeVariable<?>[])null; } }; new LC001().getTypeParameters(); if(!LC001.class.getEnclosingClass().equals(ClassHierarchyTest.class)) fail("test_3, case 004 FAILED: "+LC001.class.getEnclosingClass()); if(LC001.class.getEnumConstants()!=null) fail("test_3, case 009 FAILED: "+LC001.class.getEnumConstants()); if(LC001.class.isEnum()) fail("test_3, case 000 FAILED: "+LC001.class.isEnum()); try{LC001.class.asSubclass(ClassHierarchyTest.class); fail("test_3, case 011 FAILED: "+LC001.class.asSubclass(ClassHierarchyTest.class));}catch(Exception e){/*e.printStackTrace();*/} if(!LC001.class.getEnclosingMethod().getName().equals("test_14")) fail("test_3, case 013 FAILED: "+LC001.class.getEnclosingMethod().getName()); if(LC001.class.getEnclosingConstructor()!=null) fail("test_3, case 014 FAILED: "+LC001.class.getEnclosingConstructor()); if(LC001.class.isMemberClass()) fail("test_3, case 017 FAILED: "+LC001.class.isMemberClass()); if(!LC001.class.isLocalClass()) fail("test_3, case 018 FAILED: "+LC001.class.isLocalClass()); if(LC001.class.isAnonymousClass()) fail("test_3, case 019 FAILED: "+LC001.class.isAnonymousClass()); if(LC001.class.isSynthetic()) fail("test_3, case 020 FAILED: "+LC001.class.isSynthetic()); if(LC001.class.getCanonicalName()!=null) fail("test_3, case 021 FAILED: "+LC001.class.getCanonicalName()); if(!LC001.class.getSimpleName().equals("LC001")) fail("test_3, case 022 FAILED: "+LC001.class.getSimpleName()); }
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(); }