List of usage examples for java.lang Class getGenericSuperclass
public Type getGenericSuperclass()
From source file:reconf.client.constructors.CollectionConstructor.java
public Object construct(MethodData data) throws Throwable { if (data.hasAdapter()) { return data.getAdapter().adapt(data.getValue()); }/* w ww . ja v a 2s. com*/ Class<?> returnClass = null; Type innerClass = null; 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; }
From source file:se.vgregion.dao.domain.patterns.repository.db.jpa.AbstractJpaRepository.java
/** * Get the actual type arguments a child class has used to extend a generic base class. * //from w ww . ja v a2 s.c o m * @param childClass * the child class * @return a list of the raw classes for the actual type arguments. * * @see http://www.artima.com/weblogs/viewpost.jsp?thread=208860 */ private List<Class<? extends T>> getTypeArguments( @SuppressWarnings("rawtypes") Class<? extends AbstractJpaRepository> childClass) { Map<Type, Type> resolvedTypes = new HashMap<Type, Type>(); Type type = childClass; // start walking up the inheritance hierarchy until we hit this class while (!getClass(type).equals(AbstractJpaRepository.class)) { if (type instanceof Class) { // there is no useful information for us in raw types, so just keep going. type = ((Class<?>) type).getGenericSuperclass(); } else { ParameterizedType parameterizedType = (ParameterizedType) type; Class<?> rawType = (Class<?>) parameterizedType.getRawType(); Type[] actualTypeArguments = parameterizedType.getActualTypeArguments(); TypeVariable<?>[] typeParameters = rawType.getTypeParameters(); for (int i = 0; i < actualTypeArguments.length; i++) { resolvedTypes.put(typeParameters[i], actualTypeArguments[i]); } if (!rawType.equals(AbstractJpaRepository.class)) { type = rawType.getGenericSuperclass(); } } } // finally, for each actual type argument provided to baseClass, determine (if possible) // the raw class for that type argument. Type[] actualTypeArguments; if (type instanceof Class) { actualTypeArguments = ((Class<?>) type).getTypeParameters(); } else { actualTypeArguments = ((ParameterizedType) type).getActualTypeArguments(); } List<Class<? extends T>> typeArgumentsAsClasses = new ArrayList<Class<? extends T>>(); // resolve types by chasing down type variables. for (Type baseType : actualTypeArguments) { while (resolvedTypes.containsKey(baseType)) { baseType = resolvedTypes.get(baseType); } typeArgumentsAsClasses.add(getClass(baseType)); } return typeArgumentsAsClasses; }
From source file:com.aw.swing.mvp.Presenter.java
public Class getParameterizedType() { Class parameterizedType = getClass(); while (!(parameterizedType.getGenericSuperclass() instanceof ParameterizedType)) { parameterizedType = parameterizedType.getSuperclass(); }//ww w .j a v a 2s . c o m return parameterizedType; }
From source file:org.entando.entando.plugins.jpoauthclient.aps.system.services.client.ProviderConnectionManager.java
private Object unmarshallResponse(InputStream stream, Class expectedType, MediaType responseType) { Object bodyObject = null;//from w w w. j ava2 s . com try { if (responseType.equals(MediaType.APPLICATION_JSON_TYPE)) { JSONProvider jsonProvider = new JSONProvider(); bodyObject = jsonProvider.readFrom(expectedType, expectedType.getGenericSuperclass(), expectedType.getAnnotations(), MediaType.APPLICATION_JSON_TYPE, null, stream); } else { JAXBContext context = JAXBContext.newInstance(expectedType); Unmarshaller unmarshaller = context.createUnmarshaller(); bodyObject = (Object) unmarshaller.unmarshal(stream); } } catch (Throwable t) { _logger.error("Error unmarshalling response result", t); } return bodyObject; }
From source file:org.eclipse.wb.internal.swing.databinding.model.beans.BeanSupport.java
public List<ObserveInfo> createProperties(ObserveInfo parent, IGenericType objectType) { try {/*from w ww . jav a 2 s. c o m*/ Class<?> objectClass = objectType.getRawType(); BeanDecorationInfo decorationInfo = DecorationUtils.getDecorationInfo(objectClass); IDecorationProvider decorationProvider = decorationInfo == null ? m_decorationProviderOverType : m_decorationProviderOverInfo; List<ObserveInfo> properties = Lists.newArrayList(); // handle generic TypeVariable<?> superTypeParameter = null; Type superTypeParameterClass = null; if (objectClass.getTypeParameters().length == 1 && objectType.getSubTypes().size() == 1) { superTypeParameter = objectClass.getTypeParameters()[0]; superTypeParameterClass = objectType.getSubTypes().get(0).getRawType(); } else if (objectClass.getGenericSuperclass() instanceof ParameterizedType) { ParameterizedType superType = (ParameterizedType) objectClass.getGenericSuperclass(); if (superType.getActualTypeArguments().length == 1 && superType.getActualTypeArguments()[0] instanceof Class<?> && superType.getRawType() instanceof Class<?>) { Class<?> superClass = (Class<?>) superType.getRawType(); if (superClass.getTypeParameters().length == 1) { superTypeParameter = superClass.getTypeParameters()[0]; superTypeParameterClass = superType.getActualTypeArguments()[0]; } } } // properties for (PropertyDescriptor descriptor : getLocalPropertyDescriptors(objectClass)) { String name = descriptor.getName(); IGenericType propertyType = GenericUtils.getObjectType(superTypeParameter, superTypeParameterClass, descriptor); properties.add(new BeanPropertyObserveInfo(this, parent, name, propertyType, new StringReferenceProvider(name), decorationProvider.getDecorator(decorationInfo, propertyType, name, descriptor))); } // Swing properties if (javax.swing.text.JTextComponent.class.isAssignableFrom(objectClass)) { replaceProperty(properties, "text", new PropertiesObserveInfo(this, parent, "text", ClassGenericType.STRING_CLASS, new StringReferenceProvider("text"), IObserveDecorator.BOLD, new String[] { "text", "text_ON_ACTION_OR_FOCUS_LOST", "text_ON_FOCUS_LOST" })); } else if (javax.swing.JTable.class.isAssignableFrom(objectClass)) { addElementProperties(properties, parent); Collections.sort(properties, ObserveComparator.INSTANCE); } else if (javax.swing.JSlider.class.isAssignableFrom(objectClass)) { replaceProperty(properties, "value", new PropertiesObserveInfo(this, parent, "value", ClassGenericType.INT_CLASS, new StringReferenceProvider("value"), IObserveDecorator.BOLD, new String[] { "value", "value_IGNORE_ADJUSTING" })); } else if (javax.swing.JList.class.isAssignableFrom(objectClass)) { addElementProperties(properties, parent); Collections.sort(properties, ObserveComparator.INSTANCE); } // EL property if (m_addELProperty && !objectClass.isPrimitive()) { properties.add(0, new ElPropertyObserveInfo(parent, objectType)); } // Object property if (m_addSelfProperty && (parent == null || !(parent instanceof BeanPropertyObserveInfo))) { properties.add(0, new ObjectPropertyObserveInfo(objectType)); } // return properties; } catch (Throwable e) { DesignerPlugin.log(e); return Collections.emptyList(); } }
From source file:com.link_intersystems.lang.reflect.Class2.java
private Type findBoundTypeInGenericSuperclasses(Class<?> typeClass, TypeVariable<?> typeVariable) { Type boundType = null;//from w w w. j a v a2s . c om Type genericSuperclass = typeClass.getGenericSuperclass(); if (genericSuperclass instanceof ParameterizedType) { ParameterizedType parameterizedType = ParameterizedType.class.cast(genericSuperclass); boundType = doGetBoundType(parameterizedType, typeVariable); } return boundType; }
From source file:com.jaspersoft.jasperserver.war.helper.GenericParametersHelper.java
private static ParameterizedType findParametrizedType(Class<?> classToParse, Class<?> genericClassToFind, Map<String, Class<?>> inputParameterValues) { ParameterizedType type = null; if (genericClassToFind.isInterface()) { final Type[] genericInterfaces = classToParse.getGenericInterfaces(); if (genericInterfaces != null && genericInterfaces.length > 0) { for (Type genericInterface : genericInterfaces) { if (genericInterface == genericClassToFind) { throw new IllegalArgumentException(classToParse.getName() + " is raw implementation of " + genericClassToFind.getName()); }//from ww w. j ava2 s . c o m if (genericInterface instanceof ParameterizedType) { ParameterizedType currentParametrizedType = (ParameterizedType) genericInterface; Map<String, Class<?>> currentParameterValues = new HashMap<String, Class<?>>( inputParameterValues); if (currentParametrizedType.getRawType() == genericClassToFind) { type = (ParameterizedType) genericInterface; } else { currentParameterValues = getCurrentParameterValues( ((Class<?>) currentParametrizedType.getRawType()).getTypeParameters(), currentParametrizedType.getActualTypeArguments(), new HashMap<String, Class<?>>(inputParameterValues)); type = findParametrizedType((Class<?>) currentParametrizedType.getRawType(), genericClassToFind, currentParameterValues); } if (type != null) { inputParameterValues.clear(); inputParameterValues.putAll(currentParameterValues); break; } } } } } else { final Type genericSuperclass = classToParse.getGenericSuperclass(); if (genericSuperclass == genericClassToFind) { log.debug(classToParse.getName() + " is raw subclass of " + genericClassToFind.getName()); } else if (genericSuperclass instanceof ParameterizedType && ((ParameterizedType) genericSuperclass).getRawType() == genericClassToFind) { type = (ParameterizedType) genericSuperclass; } } return type; }
From source file:org.apache.flink.api.java.typeutils.TypeExtractor.java
private static Type getParameterType(Class<?> baseClass, ArrayList<Type> typeHierarchy, Class<?> clazz, int pos) { if (typeHierarchy != null) { typeHierarchy.add(clazz);/*w w w . j a v a 2 s . c o m*/ } Type[] interfaceTypes = clazz.getGenericInterfaces(); // search in interfaces for base class for (Type t : interfaceTypes) { Type parameter = getParameterTypeFromGenericType(baseClass, typeHierarchy, t, pos); if (parameter != null) { return parameter; } } // search in superclass for base class Type t = clazz.getGenericSuperclass(); Type parameter = getParameterTypeFromGenericType(baseClass, typeHierarchy, t, pos); if (parameter != null) { return parameter; } throw new InvalidTypesException("The types of the interface " + baseClass.getName() + " could not be inferred. " + "Support for synthetic interfaces, lambdas, and generic or raw types is limited at this point"); }
From source file:org.topazproject.otm.metadata.AnnotationClassMetaFactory.java
private void createMeta(ClassDefinition def, Class<?> clazz, String uriPrefix) throws OtmException { sf.addDefinition(def);//from w w w . j ava2 s . com ClassBinding bin = sf.getClassBinding(def.getName()); bin.bind(EntityMode.POJO, new ClassBinder(clazz)); Map<String, PropertyDefFactory> factories = new HashMap<String, PropertyDefFactory>(); for (Method method : clazz.getDeclaredMethods()) { if (!isAnnotated(method)) continue; if (method.getAnnotation(SubClassResolver.class) != null) { registerSubClassResolver(def, method); continue; } Property property = Property.toProperty(method); if (property == null) throw new OtmException("'" + method.toGenericString() + "' is not a valid getter or setter"); PropertyDefFactory pi = factories.get(property.getName()); if (pi != null) { if (method.equals(pi.property.getReadMethod()) || method.equals(pi.property.getWriteMethod())) continue; throw new OtmException("Duplicate property " + property); } validate(property, def); factories.put(property.getName(), new PropertyDefFactory(def, property)); } if (def instanceof EntityDefinition) { if (clazz.getGenericSuperclass() instanceof ParameterizedType) addGenericsSyntheticProps(def, clazz, (ParameterizedType) clazz.getGenericSuperclass(), factories); for (Type t : clazz.getGenericInterfaces()) if (t instanceof ParameterizedType) addGenericsSyntheticProps(def, clazz, (ParameterizedType) t, factories); Map<String, Map<String, String>> supersedes = new HashMap<String, Map<String, String>>(); buildSupersedes((EntityDefinition) def, supersedes); for (String name : supersedes.keySet()) { PropertyDefFactory pi = factories.get(name); if (pi != null) pi.setSupersedes(supersedes.get(name)); } } for (PropertyDefFactory fi : factories.values()) { PropertyDefinition d = fi.getDefinition(sf, uriPrefix); if (d != null) { sf.addDefinition(d); bin.addBinderFactory(new PropertyBinderFactory(fi.name, fi.property)); } SearchableDefinition sd = fi.getSearchableDefinition(sf, d instanceof BlobDefinition); if (sd != null) sf.addDefinition(sd); } }
From source file:com.clark.func.Functions.java
/** * <p>/*www .j a va 2 s .c o m*/ * Closest parent type? Closest to what? The closest parent type to the * super class specified by <code>superClass</code>. * </p> * * @param cls * @param superClass * @return */ private static Type getClosestParentType(Class<?> cls, Class<?> superClass) { // only look at the interfaces if the super class is also an interface if (superClass.isInterface()) { // get the generic interfaces of the subject class Type[] interfaceTypes = cls.getGenericInterfaces(); // will hold the best generic interface match found Type genericInterface = null; // find the interface closest to the super class for (int i = 0; i < interfaceTypes.length; i++) { Type midType = interfaceTypes[i]; Class<?> midClass = null; if (midType instanceof ParameterizedType) { midClass = getRawType((ParameterizedType) midType); } else if (midType instanceof Class<?>) { midClass = (Class<?>) midType; } else { throw new IllegalStateException("Unexpected generic" + " interface type found: " + midType); } // check if this interface is further up the inheritance chain // than the previously found match if (isAssignable(midClass, superClass) && isAssignable(genericInterface, (Type) midClass)) { genericInterface = midType; } } // found a match? if (genericInterface != null) { return genericInterface; } } // none of the interfaces were descendants of the target class, so the // super class has to be one, instead return cls.getGenericSuperclass(); }