List of usage examples for java.lang.reflect ParameterizedType getRawType
Type getRawType();
From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java
private boolean containsCollection(final Type genericReturnType) { final boolean ret; if (genericReturnType instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) genericReturnType; Type rawType = t.getRawType(); Assert.state(rawType instanceof Class<?>, "raw type is not a Class: " + rawType.toString()); Class<?> cls = (Class<?>) rawType; if (HttpEntity.class.isAssignableFrom(cls)) { Type[] typeArguments = t.getActualTypeArguments(); ret = containsCollection(typeArguments[0]); } else if (Resources.class.isAssignableFrom(cls) || Collection.class.isAssignableFrom(cls)) { ret = true;/*from w w w . j a va2 s .c om*/ } else { ret = false; } } else if (genericReturnType instanceof GenericArrayType) { ret = true; } else if (genericReturnType instanceof WildcardType) { WildcardType t = (WildcardType) genericReturnType; ret = containsCollection(getBound(t.getLowerBounds())) || containsCollection(getBound(t.getUpperBounds())); } else if (genericReturnType instanceof TypeVariable) { ret = false; } else if (genericReturnType instanceof Class) { Class<?> cls = (Class<?>) genericReturnType; ret = Resources.class.isAssignableFrom(cls) || Collection.class.isAssignableFrom(cls); } else { ret = false; } return ret; }
From source file:org.topazproject.otm.metadata.AnnotationClassMetaFactory.java
private void addGenericsSyntheticProps(ClassDefinition def, Class clazz, ParameterizedType psup, Map<String, PropertyDefFactory> factories) throws OtmException { for (Method m : ((Class) psup.getRawType()).getMethods()) { if (!isAnnotated(m)) continue; Property property = Property.toProperty(m); if ((property == null) || factories.containsKey(property.getName())) continue; property = property.resolveGenericsType(clazz, psup); if (property != null) factories.put(property.getName(), new PropertyDefFactory(def, property)); }//from ww w . java2 s . c o m }
From source file:org.romaframework.core.schema.SchemaHelper.java
@SuppressWarnings({ "unchecked", "rawtypes" }) public static Class<?> resolveClassFromType(Type type, ParameterizedType params) { if (type instanceof Class<?>) return (Class<?>) type; if (type instanceof ParameterizedType) { return resolveClassFromType(((ParameterizedType) type).getRawType(), (ParameterizedType) type); }/*from w w w .ja v a 2 s . com*/ if (type instanceof GenericArrayType) { GenericArrayType gat = (GenericArrayType) type; Class<?> arrItemp = resolveClassFromType(gat.getGenericComponentType(), null); return Array.newInstance(arrItemp, 0).getClass(); } if (type instanceof TypeVariable<?>) { TypeVariable<?> t = (TypeVariable<?>) type; if (params != null) { Class<?> cl = resolveClassFromType(params.getRawType(), null); if (cl != null) { TypeVariable<Class<?>>[] var = ((Class) cl).getTypeParameters(); int i = 0; for (; i < var.length; i++) { if (var[i].getName().equals(t.getName())) { return resolveClassFromType(params.getActualTypeArguments()[i], resolveParameterizedType(params.getOwnerType())); } } } } Type[] bounds = t.getBounds(); if (bounds.length == 1) return resolveClassFromType(bounds[0], params); } if (type instanceof WildcardType) { // TODO: } return null; }
From source file:org.evosuite.testcase.ImportsTestCodeVisitor.java
private String getTypeName(ParameterizedType type) { String name = getClassName((Class<?>) type.getRawType()); Type[] types = type.getActualTypeArguments(); boolean isDefined = false; for (Type parameterType : types) { if (parameterType instanceof Class<?> || parameterType instanceof ParameterizedType || parameterType instanceof WildcardType || parameterType instanceof GenericArrayType) { isDefined = true;//from w w w . j a v a 2s .c o m break; } } if (isDefined) { if (types.length > 0) { name += "<"; for (int i = 0; i < types.length; i++) { if (i != 0) name += ", "; name += getTypeName(types[i]); } name += ">"; } } return name; }
From source file:org.evosuite.utils.TestGenericClass.java
@Test public void testSatisfiesTypeVariableInSubtype() { GenericClass iterableIntegerClass = new GenericClass( new TypeToken<com.examples.with.different.packagename.generic.GuavaExample4<java.lang.Iterable<Integer>>>() { }.getType());//from w w w .j a v a 2 s . c om ParameterizedType iterableInteger = (ParameterizedType) iterableIntegerClass.getParameterTypes().get(0); TypeVariable<?> typeVariable = ((Class<?>) iterableInteger.getRawType()).getTypeParameters()[0]; TypeVariable<?> iterableTypeVariable = iterableIntegerClass.getTypeVariables().get(0); GenericClass listOfIntegerClass = new GenericClass( com.examples.with.different.packagename.generic.GuavaExample4.class); // Object bound Assert.assertTrue(iterableIntegerClass.satisfiesBoundaries(typeVariable)); Assert.assertTrue(listOfIntegerClass.satisfiesBoundaries(typeVariable)); // Iterable bound Assert.assertTrue(iterableIntegerClass.satisfiesBoundaries(iterableTypeVariable)); Assert.assertTrue(listOfIntegerClass.satisfiesBoundaries(iterableTypeVariable)); }
From source file:org.apache.axis2.jaxws.server.dispatcher.ProviderDispatcher.java
private Class<?> getProviderType() { if (_providerType != null) { return _providerType; }//w ww . j a v a 2s . c o m Class providerType = null; Type[] giTypes = serviceImplClass.getGenericInterfaces(); for (Type giType : giTypes) { ParameterizedType paramType = null; try { paramType = (ParameterizedType) giType; } catch (ClassCastException e) { // this may not be a parameterized interface continue; } Class interfaceName = (Class) paramType.getRawType(); if (interfaceName == javax.xml.ws.Provider.class) { if (paramType.getActualTypeArguments().length > 1) { throw ExceptionFactory.makeWebServiceException(Messages.getMessage("pTypeErr")); } providerType = (Class) paramType.getActualTypeArguments()[0]; } } _providerType = providerType; return providerType; }
From source file:org.op4j.devutils.selected.ImplFile.java
public void computeMethodImplementations(final ImplType implType, final Class<?> interfaceClass) throws Exception { final List<Method> interfaceMethods = new ArrayList<Method>(); interfaceMethods.addAll(Arrays.asList(interfaceClass.getDeclaredMethods())); final Set<Type> extendedInterfaces = new HashSet<Type>( Arrays.asList(interfaceClass.getGenericInterfaces())); Type getReturnType = null;//from w ww .j a v a 2 s.c om for (final Type extendedInterface : extendedInterfaces) { if (extendedInterface instanceof ParameterizedType) { final ParameterizedType pType = (ParameterizedType) extendedInterface; if (((Class<?>) pType.getRawType()).equals(UniqOperator.class)) { getReturnType = pType.getActualTypeArguments()[0]; } } } try { interfaceMethods.add(UniqOpOperator.class.getMethod("get")); } catch (NoSuchMethodException e) { // nothing to do } if (this.className.contains("Array")) { this.element = "T[]"; } else if (this.className.contains("List")) { this.element = "List<T>"; } else if (this.className.contains("Set")) { this.element = "Set<T>"; } else if (this.className.contains("Map")) { this.element = "Map<K,V>"; } else { this.element = "T"; } for (final Method interfaceMethod : interfaceMethods) { final String methodName = interfaceMethod.getName(); this.methodNames.add(methodName); final Type[] parameterTypes = interfaceMethod.getGenericParameterTypes(); if (methodName.startsWith("exec")) { this.currentLevelType = (new TypeRep( ((WildcardType) ((ParameterizedType) parameterTypes[0]).getActualTypeArguments()[0]) .getLowerBounds()[0])).getStringRep(); if (this.currentLevelType.endsWith("[]")) { this.currentLevelElement = this.currentLevelType.substring(0, this.currentLevelType.length() - 2); } else if (this.currentLevelType.startsWith("List<") && this.currentLevelType.endsWith(">")) { this.currentLevelElement = this.currentLevelType.substring(5, this.currentLevelType.length() - 1); } else if (this.currentLevelType.startsWith("Set<") && this.currentLevelType.endsWith(">")) { this.currentLevelElement = this.currentLevelType.substring(4, this.currentLevelType.length() - 1); } else if (this.currentLevelType.startsWith("Map<") && this.currentLevelType.endsWith(">")) { this.currentLevelElement = "Map.Entry<" + this.currentLevelType.substring(4, this.currentLevelType.length() - 1) + ">"; } else { this.currentLevelElement = "%%CURRENTELEMENTSHOULDNOTBEHERE%%"; } } final String returnTypeStr = (methodName.equals("get") ? (implType == ImplType.OP ? this.element : "Function<I," + this.element + ">") : (methodName.equals("getAsArrayOf") ? (implType == ImplType.OP ? this.element + "[]" : "Function<I," + this.element + "[]>") : (methodName.equals("getAsList") ? (implType == ImplType.OP ? "List<" + this.element + ">" : "Function<I,List<" + this.element + ">>") : new TypeRep(interfaceMethod.getGenericReturnType()).getStringRep() .replaceAll("ILevel", "Level")))); final StringBuilder parameterStrBuilder = new StringBuilder(); parameterStrBuilder.append("("); List<String> normalisedParamTypes = new ArrayList<String>(); List<String> normalisedRawParamTypes = new ArrayList<String>(); for (int j = 0; j < parameterTypes.length; j++) { normalisedParamTypes.add(getNormalisedParamType(parameterTypes[j], methodName, j)); normalisedRawParamTypes.add( StringUtils.substringBefore(getNormalisedParamType(parameterTypes[j], methodName, j), "<")); } String[] paramNamesForMethod = paramNames .get(methodName + "%" + StringUtils.join(normalisedRawParamTypes, ",")); if (paramNamesForMethod == null) { paramNamesForMethod = paramNames.get(methodName + "%" + parameterTypes.length); if (paramNamesForMethod == null) { paramNamesForMethod = paramNames.get(methodName); } } for (int j = 0; j < normalisedParamTypes.size(); j++) { if (j > 0) { parameterStrBuilder.append(", "); } parameterStrBuilder.append("final " + normalisedParamTypes.get(j) + " "); if (paramNamesForMethod == null) { throw new RuntimeException("No name for parameter " + j + " of method " + methodName + " in interface " + this.interfaceTypeRep.getStringRep()); } parameterStrBuilder.append(paramNamesForMethod[j]); } parameterStrBuilder.append(")"); final StringBuilder strBuilder = new StringBuilder(); strBuilder.append( " public " + returnTypeStr + " " + methodName + parameterStrBuilder.toString() + " {\n"); strBuilder.append(" return null;\n"); strBuilder.append(" }\n"); this.methodImplementations.add(strBuilder.toString()); } }
From source file:org.evosuite.utils.generic.GenericUtils.java
public static Type replaceTypeVariable(Type targetType, TypeVariable<?> variable, Type variableType) { if (targetType instanceof Class<?>) return targetType; else if (targetType instanceof GenericArrayType) { GenericArrayType gType = (GenericArrayType) targetType; Type componentType = replaceTypeVariable(gType.getGenericComponentType(), variable, variableType); return GenericArrayTypeImpl.createArrayType(componentType); } else if (targetType instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) targetType; Type ownerType = null;/*from w ww. j a v a 2 s . c om*/ if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), variable, variableType); } Type[] originalParameterTypes = pType.getActualTypeArguments(); Type[] parameterTypes = new Type[originalParameterTypes.length]; for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], variable, variableType); } /* if (variableType instanceof ParameterizedType) { ParameterizedType parameterizedVars = (ParameterizedType) variableType; Map<TypeVariable<?>, Type> subTypes = TypeUtils.getTypeArguments(parameterizedVars); for (Entry<TypeVariable<?>, Type> subTypeEntry : subTypes.entrySet()) { if (pType.getOwnerType() != null) { ownerType = replaceTypeVariable(pType.getOwnerType(), subTypeEntry.getKey(), subTypeEntry.getValue()); } for (int i = 0; i < originalParameterTypes.length; i++) { parameterTypes[i] = replaceTypeVariable(originalParameterTypes[i], subTypeEntry.getKey(), subTypeEntry.getValue()); } } } */ return new ParameterizedTypeImpl((Class<?>) pType.getRawType(), parameterTypes, ownerType); } else if (targetType instanceof WildcardType) { WildcardType wType = (WildcardType) targetType; Type[] originalUpperBounds = wType.getUpperBounds(); Type[] originalLowerBounds = wType.getLowerBounds(); Type[] upperBounds = new Type[originalUpperBounds.length]; Type[] lowerBounds = new Type[originalLowerBounds.length]; for (int i = 0; i < originalUpperBounds.length; i++) { upperBounds[i] = replaceTypeVariable(originalUpperBounds[i], variable, variableType); } for (int i = 0; i < originalLowerBounds.length; i++) { lowerBounds[i] = replaceTypeVariable(originalLowerBounds[i], variable, variableType); } return new WildcardTypeImpl(upperBounds, lowerBounds); } else if (targetType instanceof TypeVariable<?>) { if (targetType.equals(variable)) { //logger.debug("Do equal: " + variable + "/" + targetType); return variableType; } else { //logger.debug("Do not equal: " + variable + "/" + targetType); //logger.debug("Do not equal: " + variable.getGenericDeclaration() + "/" // + ((TypeVariable<?>) targetType).getGenericDeclaration()); return targetType; } } else { //logger.debug("Unknown type of class " + targetType.getClass() + ": " // + targetType); return targetType; } }
From source file:org.broadinstitute.gatk.utils.help.GenericDocumentationHandler.java
/** * Helper routine that returns the Feature.class required by a RodBinding, * either T for RodBinding{T} or List{RodBinding{T}}. Returns null if * the Type doesn't fit either model./* w ww .jav a 2s . com*/ * * @param type * @return */ protected Class<? extends Feature> getFeatureTypeIfPossible(Type type) { if (type instanceof ParameterizedType) { ParameterizedType paramType = (ParameterizedType) type; if (RodBinding.class.isAssignableFrom((Class<?>) paramType.getRawType())) { return (Class<? extends Feature>) JVMUtils.getParameterizedTypeClass(type); } else { for (Type paramtype : paramType.getActualTypeArguments()) { Class<? extends Feature> x = getFeatureTypeIfPossible(paramtype); if (x != null) return x; } } } return null; }
From source file:net.firejack.platform.model.service.reverse.ReverseEngineeringService.java
private void analyzeField(String name, Type type, WebParam.Mode mode, boolean list, boolean holder, boolean _return, Service service) { if (type instanceof Class) { Class clazz = (Class) type; Parameter parameter = new Parameter(StringUtils.uncapitalize(name), clazz, clazz.isAnnotationPresent(XmlAccessorType.class), list, holder, mode); if (_return) { service.setReturn(parameter); } else {/* ww w .j a v a 2s .co m*/ service.addParameter(parameter); } } else if (type instanceof ParameterizedType) { ParameterizedType parameterizedType = (ParameterizedType) type; Type rawType = parameterizedType.getRawType(); Type generic = parameterizedType.getActualTypeArguments()[0]; if (rawType == Holder.class) { analyzeField(name, generic, mode, false, true, _return, service); } else if (rawType == List.class) { if (generic instanceof Class && ((Class) generic).isAnnotationPresent(XmlAccessorType.class)) { analyzeField(name, generic, mode, true, holder, _return, service); } else { analyzeField(name, rawType, mode, true, holder, _return, service); } } else { throw new IllegalStateException("Unknown type: " + type); } } }