Search in sources :

Example 81 with Type

use of java.lang.reflect.Type in project cucumber-jvm by cucumber.

the class Java8StepDefinition method getParameterInfos.

private List<ParameterInfo> getParameterInfos(Class<? extends StepdefBody> bodyClass, TypeIntrospector typeIntrospector, int parameterCount) throws Exception {
    Type genericInterface = bodyClass.getGenericInterfaces()[0];
    Type[] argumentTypes;
    if (genericInterface instanceof ParameterizedType) {
        argumentTypes = ((ParameterizedType) genericInterface).getActualTypeArguments();
    } else {
        Class<? extends StepdefBody> interfac3 = (Class<? extends StepdefBody>) bodyClass.getInterfaces()[0];
        argumentTypes = typeIntrospector.getGenericTypes(bodyClass, interfac3);
    }
    Type[] argumentTypesOfCorrectLength = new Type[parameterCount];
    System.arraycopy(argumentTypes, argumentTypes.length - parameterCount, argumentTypesOfCorrectLength, 0, parameterCount);
    verifyNotListOrMap(argumentTypesOfCorrectLength);
    return ParameterInfo.fromTypes(argumentTypesOfCorrectLength);
}
Also used : ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) StepdefBody(cucumber.api.java8.StepdefBody)

Example 82 with Type

use of java.lang.reflect.Type in project cucumber-jvm by cucumber.

the class ConstantPoolTypeIntrospector method getGenericTypes.

@Override
public Type[] getGenericTypes(Class<? extends StepdefBody> clazz, Class<? extends StepdefBody> interfac3) throws Exception {
    ConstantPool constantPool = (ConstantPool) Class_getConstantPool.invoke(clazz);
    String typeString = getLambdaTypeString(constantPool);
    int typeParameterCount = interfac3.getTypeParameters().length;
    jdk.internal.org.objectweb.asm.Type[] argumentTypes = jdk.internal.org.objectweb.asm.Type.getArgumentTypes(typeString);
    // Only look at the N last arguments to the lambda static method, since the first ones might be variables
    // who only pass in the states of closed variables
    List<jdk.internal.org.objectweb.asm.Type> interestingArgumentTypes = Arrays.asList(argumentTypes).subList(argumentTypes.length - typeParameterCount, argumentTypes.length);
    Type[] typeArguments = new Type[typeParameterCount];
    for (int i = 0; i < typeParameterCount; i++) {
        typeArguments[i] = Class.forName(interestingArgumentTypes.get(i).getClassName());
    }
    return typeArguments;
}
Also used : Type(java.lang.reflect.Type) ConstantPool(sun.reflect.ConstantPool)

Example 83 with Type

use of java.lang.reflect.Type in project neo4j by neo4j.

the class PluginPointFactoryImpl method parameterExtractor.

private static ParameterExtractor parameterExtractor(Type type, Parameter parameter, Description description) {
    if (type instanceof ParameterizedType) {
        ParameterizedType paramType = (ParameterizedType) type;
        Class<?> raw = (Class<?>) paramType.getRawType();
        Type componentType = paramType.getActualTypeArguments()[0];
        Class<?> component = null;
        if (componentType instanceof Class<?>) {
            component = (Class<?>) componentType;
        }
        if (Set.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return new HashSet<Object>(Arrays.asList(result));
                    }
                };
            }
        } else if (List.class == raw || Collection.class == raw || Iterable.class == raw) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return Arrays.asList(result);
                    }
                };
            }
        }
    } else if (type instanceof Class<?>) {
        Class<?> raw = (Class<?>) type;
        if (raw.isArray()) {
            TypeCaster caster = TYPES.get(raw.getComponentType());
            if (caster != null) {
                return new ListParameterExtractor(caster, raw.getComponentType(), parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        } else {
            TypeCaster caster = TYPES.get(raw);
            if (caster != null) {
                return new ParameterExtractor(caster, raw, parameter, description);
            }
        }
    } else if (type instanceof GenericArrayType) {
        GenericArrayType array = (GenericArrayType) type;
        Type component = array.getGenericComponentType();
        if (component instanceof Class<?>) {
            TypeCaster caster = TYPES.get(component);
            if (caster != null) {
                return new ListParameterExtractor(caster, (Class<?>) component, parameter, description) {

                    @Override
                    Object convert(Object[] result) {
                        return result;
                    }
                };
            }
        }
    }
    throw new IllegalStateException("Unsupported parameter type: " + type);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RelationshipType(org.neo4j.graphdb.RelationshipType) Collection(java.util.Collection) List(java.util.List) HashSet(java.util.HashSet)

Example 84 with Type

use of java.lang.reflect.Type in project neo4j by neo4j.

the class PluginPointFactoryImpl method createFrom.

public PluginPoint createFrom(ServerPlugin plugin, Method method, Class<?> discovery) {
    ResultConverter result = ResultConverter.get(method.getGenericReturnType());
    Type[] types = method.getGenericParameterTypes();
    Annotation[][] annotations = method.getParameterAnnotations();
    SourceExtractor sourceExtractor = null;
    DataExtractor[] extractors = new DataExtractor[types.length];
    for (int i = 0; i < types.length; i++) {
        Description description = null;
        Parameter param = null;
        Source source = null;
        for (Annotation annotation : annotations[i]) {
            if (annotation instanceof Description) {
                description = (Description) annotation;
            } else if (annotation instanceof Parameter) {
                param = (Parameter) annotation;
            } else if (annotation instanceof Source) {
                source = (Source) annotation;
            }
        }
        if (param != null && source != null) {
            throw new IllegalStateException(String.format("Method parameter %d of %s cannot be retrieved as both Parameter and Source", Integer.valueOf(i), method));
        } else if (source != null) {
            if (types[i] != discovery) {
                throw new IllegalStateException("Source parameter type (" + types[i] + ") must equal the discovery type (" + discovery.getName() + ").");
            }
            if (sourceExtractor != null) {
                throw new IllegalStateException("Server Extension methods may have at most one Source parameter.");
            }
            extractors[i] = sourceExtractor = new SourceExtractor(source, description);
        } else if (param != null) {
            extractors[i] = parameterExtractor(types[i], param, description);
        } else {
            throw new IllegalStateException("Parameters of Server Extension methods must be annotated as either Source or Parameter.");
        }
    }
    return new PluginMethod(nameOf(method), discovery, plugin, result, method, extractors, method.getAnnotation(Description.class));
}
Also used : Annotation(java.lang.annotation.Annotation) GenericArrayType(java.lang.reflect.GenericArrayType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) RelationshipType(org.neo4j.graphdb.RelationshipType)

Example 85 with Type

use of java.lang.reflect.Type in project jodd by oblac.

the class ReflectUtilTest method testMethodTypeToString.

@Test
public void testMethodTypeToString() {
    Method[] methods = MethodReturnType.class.getDeclaredMethods();
    Arrays.sort(methods, new Comparator<Method>() {

        public int compare(Method o1, Method o2) {
            return o1.getName().compareTo(o2.getName());
        }
    });
    String result = "";
    for (Method method : methods) {
        Type type = method.getGenericReturnType();
        result += method.getName() + " - " + ReflectUtil.typeToString(type) + '\n';
    }
    assertEquals("mBoundedWildcard - java.util.List<? extends java.lang.Number>\n" + "mRaw - java.util.List\n" + "mTypeLiteral - java.util.List<T extends java.util.List<java.lang.String>>\n" + "mTypeString - java.util.List<java.lang.String>\n" + "mWildcard - java.util.List<? extends java.lang.Object>\n", result);
}
Also used : Type(java.lang.reflect.Type) Method(java.lang.reflect.Method) Test(org.junit.Test)

Aggregations

Type (java.lang.reflect.Type)6423 ParameterizedType (java.lang.reflect.ParameterizedType)1761 ProgressRequestBody (io.kubernetes.client.ProgressRequestBody)722 ProgressResponseBody (io.kubernetes.client.ProgressResponseBody)722 GenericArrayType (java.lang.reflect.GenericArrayType)690 WildcardType (java.lang.reflect.WildcardType)571 Test (org.junit.Test)512 ArrayList (java.util.ArrayList)427 Method (java.lang.reflect.Method)416 TypeVariable (java.lang.reflect.TypeVariable)337 List (java.util.List)335 Map (java.util.Map)289 Gson (com.google.gson.Gson)231 V1Status (io.kubernetes.client.models.V1Status)228 V1Status (io.kubernetes.client.openapi.models.V1Status)224 HashMap (java.util.HashMap)204 Field (java.lang.reflect.Field)163 Annotation (java.lang.annotation.Annotation)160 IOException (java.io.IOException)140 Collection (java.util.Collection)111