List of usage examples for java.lang Class getGenericSuperclass
public Type getGenericSuperclass()
From source file:org.apache.ranger.service.RangerBaseModelService.java
@SuppressWarnings("unchecked") public RangerBaseModelService() { Class klass = getClass(); ParameterizedType genericSuperclass = (ParameterizedType) klass.getGenericSuperclass(); TypeVariable<Class<?>> var[] = klass.getTypeParameters(); if (genericSuperclass.getActualTypeArguments()[0] instanceof Class) { tEntityClass = (Class<T>) genericSuperclass.getActualTypeArguments()[0]; tViewClass = (Class<V>) genericSuperclass.getActualTypeArguments()[1]; } else if (var.length > 0) { tEntityClass = (Class<T>) var[0].getBounds()[0]; tViewClass = (Class<V>) var[1].getBounds()[0]; } else {/* w w w .ja v a 2s . com*/ LOG.fatal("Cannot find class for template", new Throwable()); } if (tEntityClass != null) { tClassName = tEntityClass.getName(); } populateExistingBaseFields = false; countQueryStr = "SELECT COUNT(obj) FROM " + tClassName + " obj "; queryStr = "SELECT obj FROM " + tClassName + " obj "; }
From source file:JDBCPool.dbcp.demo.sourcecode.PoolImplUtils.java
/** * Obtain the concrete type used by an implementation of an interface that * uses a generic type./*from ww w .ja v a 2 s . c om*/ * * @param type The interface that defines a generic type * @param clazz The class that implements the interface with a concrete type * @param <T> The interface type * * @return concrete type used by the implementation */ private static <T> Object getGenericType(Class<T> type, Class<? extends T> clazz) { // Look to see if this class implements the generic interface // Get all the interfaces Type[] interfaces = clazz.getGenericInterfaces(); for (Type iface : interfaces) { // Only need to check interfaces that use generics if (iface instanceof ParameterizedType) { ParameterizedType pi = (ParameterizedType) iface; // Look for the generic interface if (pi.getRawType() instanceof Class) { if (type.isAssignableFrom((Class<?>) pi.getRawType())) { return getTypeParameter(clazz, pi.getActualTypeArguments()[0]); } } } } // Interface not found on this class. Look at the superclass. @SuppressWarnings("unchecked") Class<? extends T> superClazz = (Class<? extends T>) clazz.getSuperclass(); Object result = getGenericType(type, superClazz); if (result instanceof Class<?>) { // Superclass implements interface and defines explicit type for // generic return result; } else if (result instanceof Integer) { // Superclass implements interface and defines unknown type for // generic // Map that unknown type to the generic types defined in this class ParameterizedType superClassType = (ParameterizedType) clazz.getGenericSuperclass(); return getTypeParameter(clazz, superClassType.getActualTypeArguments()[((Integer) result).intValue()]); } else { // Error will be logged further up the call stack return null; } }
From source file:net.firejack.platform.core.utils.Factory.java
public void addAdapter(XmlAdapter adapter) { Class<? extends XmlAdapter> adapterClass = adapter.getClass(); Type[] typeArguments = ((ParameterizedTypeImpl) adapterClass.getGenericSuperclass()) .getActualTypeArguments();/* w w w. j av a 2 s. c om*/ String marshal = ((Class) typeArguments[0]).getName() + ((Class) typeArguments[1]).getName(); String unmarshal = ((Class) typeArguments[1]).getName() + ((Class) typeArguments[0]).getName(); adapters.put(marshal, adapter); adapters.put(unmarshal, adapter); }
From source file:org.blocks4j.reconf.client.constructors.MapConstructor.java
public Object construct(MethodData data) throws Throwable { Class<?> returnClass; Type keyType = null;/*from w w w. ja v a2 s . c om*/ Type valueType = null; if (data.getReturnType() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) data.getReturnType(); returnClass = (Class<?>) parameterized.getRawType(); if (parameterized.getActualTypeArguments().length == 1) { Type first = parameterized.getActualTypeArguments()[0]; if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) { keyType = first; valueType = parameterized.getActualTypeArguments()[1]; } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) { valueType = first; keyType = parameterized.getActualTypeArguments()[0]; } else { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } } } else { keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } } else if (data.getReturnType() instanceof Class) { returnClass = (Class<?>) data.getReturnType(); if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } else { keyType = Object.class; valueType = Object.class; } } else { throw new IllegalArgumentException(msg.format("error.return", data.getMethod())); } if (returnClass.isInterface()) { returnClass = getDefaultImplementation(data, returnClass); } Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY); Map<Object, Object> mapInstance = (Map<Object, Object>) constructor .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY); if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) { return mapInstance; } if ((!(keyType instanceof Class)) || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) { throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod())); } StringParser parser = new StringParser(data); for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) { Object value = ObjectConstructorFactory.get(valueType) .construct(new MethodData(data.getMethod(), valueType, each.getValue())); mapInstance.put(ObjectConstructorFactory.get(keyType) .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value); } return mapInstance; }
From source file:reconf.client.constructors.MapConstructor.java
public Object construct(MethodData data) throws Throwable { if (data.hasAdapter()) { return data.getAdapter().adapt(data.getValue()); }/*from www .j a va2s . c o m*/ Class<?> returnClass = null; Type keyType = null; Type valueType = null; if (data.getReturnType() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) data.getReturnType(); returnClass = (Class<?>) parameterized.getRawType(); if (parameterized.getActualTypeArguments().length == 1) { Type first = parameterized.getActualTypeArguments()[0]; if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) { keyType = first; valueType = parameterized.getActualTypeArguments()[1]; } else if (parameterized.getActualTypeArguments()[1] instanceof TypeVariable) { valueType = first; keyType = parameterized.getActualTypeArguments()[0]; } else { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } } } else { keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } } else if (data.getReturnType() instanceof Class) { returnClass = (Class<?>) data.getReturnType(); if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 2) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } keyType = parameterized.getActualTypeArguments()[0]; valueType = parameterized.getActualTypeArguments()[1]; } else { keyType = Object.class; valueType = Object.class; } } else { throw new IllegalArgumentException(msg.format("error.return", data.getMethod())); } if (returnClass.isInterface()) { returnClass = getDefaultImplementation(data, returnClass); } Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY); Map<Object, Object> mapInstance = (Map<Object, Object>) constructor .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY); if (null == data.getValue() || StringUtils.isEmpty(data.getValue())) { return mapInstance; } if ((!(keyType instanceof Class)) || (!StringUtils.startsWith(data.getValue(), "[") || !StringUtils.endsWith(data.getValue(), "]"))) { throw new IllegalArgumentException(msg.format("error.build", data.getValue(), data.getMethod())); } StringParser parser = new StringParser(data); for (Entry<String, String> each : parser.getTokensAsMap().entrySet()) { Object value = ObjectConstructorFactory.get(valueType) .construct(new MethodData(data.getMethod(), valueType, each.getValue())); mapInstance.put(ObjectConstructorFactory.get(keyType) .construct(new MethodData(data.getMethod(), keyType, each.getKey())), value); } return mapInstance; }
From source file:nl.luminis.test.util.annotations.HierarchyDiscovery.java
private Type resolveTypeParameter(ParameterizedType type, Type beanType, TypeVariable<?> typeVariable) { // step1. raw type Class<?> actualType = (Class<?>) type.getRawType(); TypeVariable<?>[] typeVariables = actualType.getTypeParameters(); Type[] actualTypes = type.getActualTypeArguments(); for (int i = 0; i < typeVariables.length; i++) { if (typeVariables[i].equals(typeVariable) && !actualTypes[i].equals(typeVariable)) { return resolveType(this.type, beanType, actualTypes[i]); }/*from w ww . j a v a 2s .co m*/ } // step2. generic super class Type genericSuperType = actualType.getGenericSuperclass(); Type resolvedGenericSuperType = resolveType(genericSuperType, beanType, typeVariable); if (!(resolvedGenericSuperType instanceof TypeVariable<?>)) { return resolvedGenericSuperType; } // step3. generic interfaces if (beanType instanceof ParameterizedType) { for (Type interfaceType : ((Class<?>) ((ParameterizedType) beanType).getRawType()) .getGenericInterfaces()) { Type resolvedType = resolveType(interfaceType, interfaceType, typeVariable); if (!(resolvedType instanceof TypeVariable<?>)) { return resolvedType; } } } // don't resolve type variable return typeVariable; }
From source file:net.firejack.platform.core.utils.Factory.java
public Class getGenericParameterClass(Class actualClass, Class genericClass, int parameterIndex) { if (!genericClass.isAssignableFrom(actualClass) || genericClass.equals(actualClass)) { throw new IllegalArgumentException( "Class " + genericClass.getName() + " is not a superclass of " + actualClass.getName() + "."); }/*from w w w. j av a 2 s .c o m*/ Stack<ParameterizedType> types = new Stack<ParameterizedType>(); Class clazz = actualClass; while (true) { Type currentType = genericClass.isInterface() ? getGenericInterface(clazz, genericClass) : clazz.getGenericSuperclass(); Type rawType; if (currentType instanceof ParameterizedType) { ParameterizedType type = (ParameterizedType) currentType; types.push(type); rawType = type.getRawType(); } else { types.clear(); rawType = currentType; } if (!rawType.equals(genericClass)) { clazz = (Class) rawType; } else { break; } } if (types.isEmpty()) { return (Class) genericClass.getTypeParameters()[parameterIndex].getGenericDeclaration(); } Type result = types.pop().getActualTypeArguments()[parameterIndex]; while (result instanceof TypeVariable && !types.empty()) { int actualArgumentIndex = getParameterTypeDeclarationIndex((TypeVariable) result); ParameterizedType type = types.pop(); result = type.getActualTypeArguments()[actualArgumentIndex]; } if (result instanceof TypeVariable) { throw new IllegalStateException("Unable to resolve type variable " + result + "." + " Try to replace instances of parametrized class with its non-parameterized subtype."); } if (result instanceof ParameterizedType) { result = ((ParameterizedType) result).getRawType(); } if (result == null) { throw new IllegalStateException( "Unable to determine actual parameter type for " + actualClass.getName() + "."); } if (!(result instanceof Class)) { throw new IllegalStateException( "Actual parameter type for " + actualClass.getName() + " is not a Class."); } return (Class) result; }
From source file:org.vulpe.commons.util.VulpeReflectUtil.java
/** * Returns class of TypeVariable.// ww w . j a v a2 s .c om * * @param clazz * @param superClass * @param typeVariable * @param info * @return */ private static DeclaredType getDeclaredTypeVariableDeclared(final Class<?> clazz, final Class<?> superClass, final TypeVariable<?> typeVariable, final VariableInfo info) { if (clazz.equals(Object.class)) { return null; } if (typeVariable.getGenericDeclaration().equals(superClass) || typeVariable.getGenericDeclaration().equals(clazz)) { final int index = getIndexTypeVariable(typeVariable); if (typeVariable.getGenericDeclaration().equals(clazz)) { info.setIndex(index); info.setFirstClass((Class<?>) typeVariable.getGenericDeclaration()); info.setInSuperclass(false); return null; } else { final ParameterizedType pType = (ParameterizedType) clazz.getGenericSuperclass(); final Type type = pType.getActualTypeArguments()[index]; if (type instanceof TypeVariable) { info.setIndex(getIndexTypeVariable((TypeVariable<?>) type)); info.setFirstClass(clazz); info.setInSuperclass(true); return null; } return getDeclaredType(clazz, type); } } else { final DeclaredType declaredType = getDeclaredTypeVariableDeclared(clazz.getSuperclass(), superClass.getSuperclass(), typeVariable, info); if (declaredType == null) { ParameterizedType parameterizedType = null; if (clazz.getGenericSuperclass() instanceof ParameterizedType) { parameterizedType = (ParameterizedType) clazz.getGenericSuperclass(); info.setInSuperclass(true); info.setFirstClass(clazz); } else { parameterizedType = (ParameterizedType) superClass.getGenericSuperclass(); info.setInSuperclass(false); info.setFirstClass(superClass); } final Type type = parameterizedType.getActualTypeArguments()[info.getIndex()]; if (type instanceof TypeVariable) { info.setIndex(getIndexTypeVariable((TypeVariable<?>) type)); return null; } return getDeclaredType(clazz, type); } else { return declaredType; } } }
From source file:org.debux.webmotion.server.handler.ExecutorParametersConvertorHandler.java
protected Object convert(ParameterTree parameterTree, Class<?> type, Type genericType) throws Exception { Object result = null;/*w w w . j av a2s .co m*/ if (parameterTree == null) { return null; } if (genericType == null) { genericType = type.getGenericSuperclass(); } Map<String, List<ParameterTree>> parameterArray = parameterTree.getArray(); Map<String, ParameterTree> parameterObject = parameterTree.getObject(); Object value = parameterTree.getValue(); Converter lookup = converter.lookup(type); if (lookup != null) { // converter found, use it result = lookup.convert(type, value); return result; } // Manage enums if (type.isEnum()) { Object name = value == null ? null : ((Object[]) value)[0]; if (name != null) { result = Enum.valueOf((Class<? extends Enum>) type, name.toString()); } // Manage collection } else if (Collection.class.isAssignableFrom(type)) { Collection instance; if (type.isInterface()) { if (List.class.isAssignableFrom(type)) { instance = new ArrayList(); } else if (Set.class.isAssignableFrom(type)) { instance = new HashSet(); } else if (SortedSet.class.isAssignableFrom(type)) { instance = new TreeSet(); } else { instance = new ArrayList(); } } else { instance = (Collection) type.newInstance(); } Class convertType = String.class; if (genericType != null && genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; convertType = (Class) parameterizedType.getActualTypeArguments()[0]; } if (parameterObject != null) { for (Map.Entry<String, ParameterTree> entry : parameterObject.entrySet()) { ParameterTree object = entry.getValue(); Object converted = convert(object, convertType, null); instance.add(converted); } } else { Object[] tab = (Object[]) value; for (Object object : tab) { Object converted = converter.convert(object, convertType); instance.add(converted); } } result = instance; // Manage map } else if (Map.class.isAssignableFrom(type)) { Map instance; if (type.isInterface()) { if (SortedMap.class.isAssignableFrom(type)) { instance = new TreeMap(); } else { instance = new HashMap(); } } else { instance = (Map) type.newInstance(); } Class convertKeyType = String.class; Class convertValueType = String.class; if (genericType != null && genericType instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) genericType; convertKeyType = (Class) parameterizedType.getActualTypeArguments()[0]; convertValueType = (Class) parameterizedType.getActualTypeArguments()[1]; } for (Map.Entry<String, ParameterTree> entry : parameterObject.entrySet()) { String mapKey = entry.getKey(); ParameterTree mapValue = entry.getValue(); Object convertedKey = converter.convert(mapKey, convertKeyType); Object convertedValue = convert(mapValue, convertValueType, null); instance.put(convertedKey, convertedValue); } result = instance; // Manage simple object } else if (type.isArray()) { Class<?> componentType = type.getComponentType(); if (parameterObject != null) { Object[] tabConverted = (Object[]) Array.newInstance(componentType, parameterObject.size()); result = tabConverted; int index = 0; for (Map.Entry<String, ParameterTree> entry : parameterObject.entrySet()) { ParameterTree object = entry.getValue(); Object objectConverted = convert(object, componentType, null); tabConverted[index] = objectConverted; index++; } } else { Object[] tab = (Object[]) value; Object[] tabConverted = (Object[]) Array.newInstance(componentType, tab.length); result = tabConverted; for (int index = 0; index < tab.length; index++) { Object object = tab[index]; Object objectConverted = converter.convert(object, componentType); tabConverted[index] = objectConverted; } } } else if (value instanceof UploadFile) { if (File.class.isAssignableFrom(type)) { UploadFile uploadFile = (UploadFile) value; result = uploadFile.getFile(); } else { result = value; } // Manage simple object } else { Object instance = type.newInstance(); boolean one = false; if (parameterObject != null) { for (Map.Entry<String, ParameterTree> attribut : parameterObject.entrySet()) { String attributeName = attribut.getKey(); ParameterTree attributeValue = attribut.getValue(); boolean writeable = propertyUtils.isWriteable(instance, attributeName); if (writeable) { one = true; Field field = FieldUtils.getField(type, attributeName, true); Class<?> attributeType = field.getType(); genericType = field.getGenericType(); Object attributeConverted = convert(attributeValue, attributeType, genericType); beanUtil.setProperty(instance, attributeName, attributeConverted); } } } if (parameterArray != null) { for (Map.Entry<String, List<ParameterTree>> entry : parameterArray.entrySet()) { String attributeName = entry.getKey(); List<ParameterTree> attributeValues = entry.getValue(); boolean writeable = propertyUtils.isWriteable(instance, attributeName); if (writeable) { one = true; Field field = FieldUtils.getField(type, attributeName, true); Class<?> attributeType = field.getType(); genericType = field.getGenericType(); Object attributeConverted = convert(attributeValues, attributeType, genericType); beanUtil.setProperty(instance, attributeName, attributeConverted); } } } if (one) { result = instance; } else { result = null; } } return result; }
From source file:org.blocks4j.reconf.client.constructors.CollectionConstructor.java
public Object construct(MethodData data) throws Throwable { Class<?> returnClass; Type innerClass = null;// w w w .ja va2 s . co m if (data.getReturnType() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) data.getReturnType(); returnClass = (Class<?>) parameterized.getRawType(); if (parameterized.getActualTypeArguments()[0] instanceof ParameterizedType) { innerClass = parameterized.getActualTypeArguments()[0]; } else if (parameterized.getActualTypeArguments()[0] instanceof Class<?>) { innerClass = parameterized.getActualTypeArguments()[0]; } } else if (data.getReturnType() instanceof Class) { returnClass = (Class<?>) data.getReturnType(); if (returnClass.getGenericSuperclass() != null && returnClass.getGenericSuperclass() instanceof ParameterizedType) { ParameterizedType parameterized = (ParameterizedType) returnClass.getGenericSuperclass(); if (parameterized.getActualTypeArguments().length != 1) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } if (parameterized.getActualTypeArguments()[0] instanceof TypeVariable) { throw new IllegalArgumentException( msg.format("error.cant.build.type", data.getReturnType(), data.getMethod())); } else { innerClass = parameterized.getActualTypeArguments()[0]; } } else { innerClass = Object.class; } } else { throw new IllegalArgumentException(msg.format("error.return", data.getMethod())); } if (returnClass.isInterface()) { returnClass = getDefaultImplementation(data, returnClass); } Constructor<?> constructor = returnClass.getConstructor(ArrayUtils.EMPTY_CLASS_ARRAY); Collection<Object> collectionInstance = (Collection<Object>) constructor .newInstance(ArrayUtils.EMPTY_OBJECT_ARRAY); if (null == data.getValue()) { return collectionInstance; } for (String s : new StringParser(data).getTokens()) { Object o = ObjectConstructorFactory.get(innerClass) .construct(new MethodData(data.getMethod(), innerClass, s)); if (o != null) { collectionInstance.add(o); } } return collectionInstance; }