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);
}
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;
}
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);
}
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));
}
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);
}
Aggregations