Java Code Examples for java.lang.reflect.GenericDeclaration

The following are top voted examples for showing how to use java.lang.reflect.GenericDeclaration. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: javaide   File: TypeVariableImpl.java   View source code 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 2
Project: jsouplib   File: Resource.java   View source code 6 votes vote down vote up
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;
}
 
Example 3
Project: uroborosql   File: JavaType.java   View source code 6 votes vote down vote up
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();
}
 
Example 4
Project: uavstack   File: FieldInfo.java   View source code 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 5
Project: xtext-extras   File: ReflectionTypeFactory.java   View source code 6 votes vote down vote up
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());
}
 
Example 6
Project: xtext-extras   File: ReflectionTypeFactory.java   View source code 6 votes vote down vote up
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;
}
 
Example 7
Project: BigApp_Discuz_Android   File: FieldInfo.java   View source code 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 8
Project: javify   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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();
}
 
Example 9
Project: testrecorder   File: Types.java   View source code 6 votes vote down vote up
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;
}
 
Example 10
Project: jvm-stm   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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();
}
 
Example 11
Project: gestock   File: TypeUtils.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 12
Project: j2objc   File: TypeVariableImpl.java   View source code 6 votes vote down vote up
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;
}
 
Example 13
Project: j2objc   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 14
Project: j2objc   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 15
Project: j2objc   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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);
        }
    }
}
 
Example 16
Project: j2objc   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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);
}
 
Example 17
Project: VectorAttackScanner   File: TypeUtils.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 18
Project: In-the-Box-Fork   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 19
Project: In-the-Box-Fork   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 20
Project: In-the-Box-Fork   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 21
Project: In-the-Box-Fork   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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);
}
 
Example 22
Project: In-the-Box-Fork   File: ImplForVariable.java   View source code 6 votes vote down vote up
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());
}
 
Example 23
Project: In-the-Box-Fork   File: ImplForVariable.java   View source code 6 votes vote down vote up
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;
    }
}
 
Example 24
Project: jdk7-jdk   File: TypeVariableImpl.java   View source code 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 25
Project: openjdk-source-code-learn   File: TypeVariableImpl.java   View source code 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 26
Project: android_http_demo   File: FieldInfo.java   View source code 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 27
Project: EndHQ-Libraries   File: TypeUtils.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 28
Project: cJUnit-mc626   File: TypeUtils.java   View source code 6 votes vote down vote up
/**
 * 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();
}
 
Example 29
Project: cn1   File: Parser.java   View source code 6 votes vote down vote up
/**
 * ################################################################################
 * 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;
}
 
Example 30
Project: cn1   File: AuxiliaryFinder.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 31
Project: cn1   File: AuxiliaryFinder.java   View source code 6 votes vote down vote up
/**
 * 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;
}
 
Example 32
Project: cn1   File: ClassHierarchyTest.java   View source code 6 votes vote down vote up
/**
   * 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());
  }
 
Example 33
Project: cn1   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 34
Project: cn1   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 35
Project: cn1   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
/**
 * 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;
        }
    }
}
 
Example 36
Project: cn1   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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);
}
 
Example 37
Project: cn1   File: ImplForVariable.java   View source code 6 votes vote down vote up
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());
}
 
Example 38
Project: cn1   File: ImplForVariable.java   View source code 6 votes vote down vote up
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;
    }
}
 
Example 39
Project: JamVM-PH   File: GenericSignatureParser.java   View source code 6 votes vote down vote up
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();
}
 
Example 40
Project: vertx-codegen   File: TypeParamInfo.java   View source code 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();
}