List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments
Type[] getActualTypeArguments();
From source file:org.sleeksnap.ScreenSnapper.java
/** * Gets a filter's parent class type//from ww w . j a v a2 s . c o m * * @param filter * @return The class representing the filter's upload type */ @SuppressWarnings("unchecked") public Class<? extends Upload> getFilterType(final UploadFilter<?> filter) { // Find the uploader type final Type[] types = filter.getClass().getGenericInterfaces(); for (final Type type : types) { if (type instanceof ParameterizedType) { final ParameterizedType parameterizedType = (ParameterizedType) type; if (parameterizedType.getRawType() == UploadFilter.class) { return (Class<? extends Upload>) parameterizedType.getActualTypeArguments()[0]; } } } throw new RuntimeException("Attempted to load invalid filter!"); }
From source file:com.datatorrent.stram.plan.logical.LogicalPlan.java
public static Type getPortType(Field f) { if (f.getGenericType() instanceof ParameterizedType) { ParameterizedType t = (ParameterizedType) f.getGenericType(); //LOG.debug("Field type is parameterized: " + Arrays.asList(t.getActualTypeArguments())); //LOG.debug("rawType: " + t.getRawType()); // the port class Type typeArgument = t.getActualTypeArguments()[0]; if (typeArgument instanceof Class) { return typeArgument; } else if (typeArgument instanceof TypeVariable) { TypeVariable<?> tv = (TypeVariable<?>) typeArgument; LOG.debug("bounds: " + Arrays.asList(tv.getBounds())); // variable may contain other variables, java.util.Map<java.lang.String, ? extends T2> return tv.getBounds()[0]; } else if (typeArgument instanceof GenericArrayType) { LOG.debug("type {} is of GenericArrayType", typeArgument); return typeArgument; } else if (typeArgument instanceof WildcardType) { LOG.debug("type {} is of WildcardType", typeArgument); return typeArgument; } else if (typeArgument instanceof ParameterizedType) { return typeArgument; } else {// ww w .ja va 2s . c o m LOG.error("Type argument is of expected type {}", typeArgument); return null; } } else { // ports are always parameterized LOG.error("No type variable: {}, typeParameters: {}", f.getType(), Arrays.asList(f.getClass().getTypeParameters())); return null; } }
From source file:com.flexoodb.engines.FlexJAXBDBDataEngine2.java
private void reviveObject(String parentid, Object o, Connection conn, String prefix, boolean revivechildren) throws Exception { Vector v = new Vector(); try {/* www . j a va 2 s . c om*/ Class c = o.getClass(); Method[] methods = c.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().startsWith("get") && method.getReturnType() != null && !method.getReturnType().getSimpleName().equals("Class")) { Class ret = method.getReturnType(); if (ret.getSimpleName().equals("List")) { Object[] args = null; List list = (ArrayList) method.invoke(o, args); ParameterizedType t = (ParameterizedType) method.getGenericReturnType(); Type type = t.getActualTypeArguments()[0]; String[] s = ("" + type).split(" "); String classname = s[1].substring(s[1].lastIndexOf(".") + 1); String tablename = prefix + classname.toLowerCase(); if (checkTable(tablename, conn, true)) { PreparedStatement ps = (PreparedStatement) conn.prepareStatement( "select id,content from " + tablename + " where parentid='" + parentid + "'"); ResultSet rec = ps.executeQuery(); // check if a record was found while (rec != null && rec.next()) { String id = rec.getString("id"); //Object o2 = _flexutils.getObject(rec.getString("content"),Class.forName(s[1])); Object o2 = _flexutils.getObject(rec.getString("content"), ClassLoader.getSystemClassLoader().loadClass(s[1])); if (id != null && o2 != null && !id.equalsIgnoreCase(parentid)) { list.add(o2); } } } } else if (!ret.getName().startsWith("java") && !ret.getSimpleName().toLowerCase().endsWith("byte[]") && !ret.getSimpleName().toLowerCase().equals("int")) // if complex { String tablename = prefix + ret.getSimpleName().toLowerCase(); if (checkTable(tablename, conn, true)) { PreparedStatement ps = (PreparedStatement) conn .prepareStatement("select distinct id,content from " + tablename + " where parentid='" + parentid + "'"); ResultSet rec = ps.executeQuery(); // check if a record was found if (rec != null && rec.next()) { String id = rec.getString("id"); Object o2 = _flexutils.getObject(rec.getString("content"), ret); if (o2 != null && !id.equalsIgnoreCase(parentid)) { String setmethod = "set" + method.getName().substring(3); Object[] args = new Object[1]; args[0] = o2; Class[] cls = new Class[1]; cls[0] = o2.getClass(); Method met = c.getMethod(setmethod, cls); met.invoke(o, args); if (revivechildren) { reviveObject(id, o2, conn, prefix, revivechildren); } //System.out.println(">>> "+o2+" added!"); } /*if (rec.isLast()) { break; }*/ } } } } } } catch (Exception f) { throw f; } }
From source file:com.flexoodb.engines.FlexJAXBDBDataEngine.java
private void reviveObject(String parentid, Object o, Connection conn, String prefix, boolean revivechildren) throws Exception { Vector v = new Vector(); try {/*from w w w.j a va 2 s .c o m*/ Class c = o.getClass(); Method[] methods = c.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().startsWith("get") && method.getReturnType() != null && !method.getReturnType().getSimpleName().equals("Class")) { Class ret = method.getReturnType(); if (ret.getSimpleName().equals("List")) { Object[] args = null; List list = (ArrayList) method.invoke(o, args); ParameterizedType t = (ParameterizedType) method.getGenericReturnType(); Type type = t.getActualTypeArguments()[0]; String[] s = ("" + type).split(" "); String classname = s[1].substring(s[1].lastIndexOf(".") + 1); String tablename = prefix + classname.toLowerCase(); if (checkTable(tablename, conn, true)) { PreparedStatement ps = (PreparedStatement) conn.prepareStatement( "select id,content from " + tablename + " where parentid='" + parentid + "'"); ResultSet rec = ps.executeQuery(); // check if a record was found while (rec != null && rec.next()) { String id = rec.getString("id"); //Object o2 = _flexutils.getObject(rec.getString("content"),Class.forName(s[1])); Object o2 = _flexutils.getObject(rec.getString("content"), ClassLoader.getSystemClassLoader().loadClass(s[1])); if (id != null && o2 != null && !id.equalsIgnoreCase(parentid)) { list.add(o2); } } } } else if (!ret.getName().startsWith("java") && !ret.getSimpleName().toLowerCase().endsWith("byte[]") && !ret.getSimpleName().toLowerCase().equals("int")) // if complex { String tablename = prefix + ret.getSimpleName().toLowerCase(); if (checkTable(tablename, conn, true)) { PreparedStatement ps = (PreparedStatement) conn .prepareStatement("select distinct id,content from " + tablename + " where parentid='" + parentid + "'"); ResultSet rec = ps.executeQuery(); // check if a record was found if (rec != null && rec.next()) { String id = rec.getString("id"); Object o2 = _flexutils.getObject(rec.getString("content"), ret); if (o2 != null && !id.equalsIgnoreCase(parentid)) { String setmethod = "set" + method.getName().substring(3); Object[] args = new Object[1]; args[0] = o2; Class[] cls = new Class[1]; cls[0] = o2.getClass(); Method met = c.getMethod(setmethod, cls); met.invoke(o, args); if (revivechildren) { reviveObject(id, o2, conn, prefix, revivechildren); } //System.out.println(">>> "+o2+" added!"); } /*if (rec.isLast()) { break; }*/ } } } } } } catch (Exception f) { throw f; } }
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 ww w . j ava2 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.LexGrid.LexBIG.caCore.web.util.LexEVSHTTPUtils.java
private String getParamatarizedListType(Field field) { String fieldTypeName = null;//from ww w. jav a2s . c o m Type type = field.getGenericType(); //if its a Parameterized List, see if we can dig out the type if (type != null && type instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) type; if (pt.getActualTypeArguments().length > 0) { Type collectionType = pt.getActualTypeArguments()[0]; if (collectionType instanceof Class) { fieldTypeName = ((Class) collectionType).getName(); } } } return fieldTypeName; }
From source file:edu.ku.brc.specify.tools.datamodelgenerator.DatamodelGenerator.java
/** * @param method/*from ww w .j a va2 s . c om*/ * @return */ protected String getReturnType(final Method method) { Class<?> classObj = method.getReturnType(); // If there is a better way, PLEASE help me! if (classObj == Set.class) { ParameterizedType type = (ParameterizedType) method.getGenericReturnType(); for (Type t : type.getActualTypeArguments()) { String cls = t.toString(); return cls.substring(6, cls.length()); } } return classObj.getName(); }
From source file:com.gatf.generator.core.GatfTestGeneratorMojo.java
@SuppressWarnings({ "unchecked", "rawtypes" }) private Object getObject(Class claz, List<Type> heirarchies) throws Exception { if (claz.isEnum()) return claz.getEnumConstants()[0]; if (isMap(claz)) { return getMapValue(claz, claz.getTypeParameters(), heirarchies); } else if (isCollection(claz)) { return getListSetValue(claz, claz.getTypeParameters(), heirarchies); } else if (claz.isInterface() || Modifier.isAbstract(claz.getModifiers())) { return null; }/* w w w . j a va 2s.c o m*/ if (heirarchies.contains(claz) || heirarchies.size() >= 2) return null; heirarchies.add(claz); Constructor cons = null; try { cons = claz.getConstructor(new Class[] {}); } catch (Exception e) { getLog().error("No public no-args constructor found for class " + claz.getName()); return null; } Object object = cons.newInstance(new Object[] {}); List<Field> allFields = getAllFields(claz); for (Field field : allFields) { if (Modifier.isStatic(field.getModifiers())) continue; if (!field.isAccessible()) { field.setAccessible(true); } List<Type> fheirlst = new ArrayList<Type>(heirarchies); if (isDebugEnabled()) getLog().info("Parsing Class " + getHeirarchyStr(fheirlst) + " field " + field.getName() + " type " + field.getType().equals(boolean.class)); if (isPrimitive(field.getType())) { field.set(object, getPrimitiveValue(field.getType())); } else if (isMap(field.getType())) { ParameterizedType type = (ParameterizedType) field.getGenericType(); field.set(object, getMapValue(field.getType(), type.getActualTypeArguments(), fheirlst)); } else if (isCollection(field.getType())) { ParameterizedType type = (ParameterizedType) field.getGenericType(); field.set(object, getListSetValue(field.getType(), type.getActualTypeArguments(), fheirlst)); } else if (!claz.equals(field.getType())) { Object fieldval = getObject(field.getType(), fheirlst); field.set(object, fieldval); } else if (claz.equals(field.getType())) { if (isDebugEnabled()) getLog().info("Ignoring recursive fields..."); } } return object; }
From source file:com.flexoodb.engines.FlexJAXBMappedDBDataEngine.java
private void reviveObject(String parentid, FlexContainer parent, Connection conn, String prefix, boolean revivechildren) throws Exception { Vector v = new Vector(); try {/* w ww. j a v a2s . c om*/ Class c = parent.getObject().getClass(); Method[] methods = c.getMethods(); for (int i = 0; i < methods.length; i++) { Method method = methods[i]; if (method.getName().startsWith("get") && method.getReturnType() != null && !method.getReturnType().getSimpleName().equals("Class")) { Class ret = method.getReturnType(); if (ret.getSimpleName().equals("List")) { Object[] args = null; //List list = (ArrayList) method.invoke(parent.getObject(), args); List list = new Vector<FlexContainer>(); ParameterizedType t = (ParameterizedType) method.getGenericReturnType(); Type type = t.getActualTypeArguments()[0]; String[] s = ("" + type).split(" "); String classname = s[1].substring(s[1].lastIndexOf(".") + 1); String tablename = classname.toLowerCase(); tablename = tablename.endsWith("type") ? tablename.substring(0, tablename.lastIndexOf("type")) : tablename; //FlexElement element = _elements.get(prefix+tablename); FlexElement element = _elements.get(tablename); String idcolumn = element.getAttribute("idcolumn").getValue(); String realtablename = element.getAttribute("realtablename").getValue(); String parentidcolumn = element.getAttribute("parentidcolumn").getValue(); boolean includeidcolumns = element.getAttribute("includeidcolumns").getValue() == null ? false : (element.getAttribute("includeidcolumns").getValue().equalsIgnoreCase("true")); //System.out.println("t:"+tablename+" "+realtablename+" >"+"select * from "+prefix+((realtablename!=null)?realtablename:tablename.toLowerCase())+" where "+parentidcolumn+"=?"+"<"); FlexElement idelement = element.getElementByName(parentidcolumn); PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from " + prefix + ((realtablename != null) ? realtablename : tablename.toLowerCase()) + " where " + parentidcolumn + "=?"); //PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from "+((realtablename!=null)?realtablename:tablename.toLowerCase())+" where "+parentidcolumn+"=?"); if (idelement.getType().equals("string")) { ps.setString(1, parentid); } else { ps.setInt(1, Integer.parseInt(parentid)); } //System.out.println(">> query:"+ps.toString()); ResultSet rec = ps.executeQuery(); RecordSet res = new RecordSet(rec); // check if a record was found while (res != null && res.next()) { //String id = res.getString("id"); String id = res.getString(idcolumn); //Object o2 = _flexutils.getObject(FlexUtils.getRDBMSRecordAsXML(tablename, res, idcolumn, parentidcolumn,includeidcolumns),Class.forName(s[1])); Object o2 = _flexutils .getObject( FlexUtils.getRDBMSRecordAsXML(tablename, res, idcolumn, parentidcolumn, includeidcolumns), ClassLoader.getSystemClassLoader().loadClass(s[1])); if (id != null && o2 != null) { //instead of adding this in the object's list we add it in the parent flex's //list.add(o2); FlexContainer fo = new FlexContainer(o2); fo.setId(id); fo.setParentId(parentid); list.add(fo); // then we check if this guy has children! enabling this will cause a loop of death //reviveObject(id,o2,conn); } } // finally we add it to the parent flex parent.addChildren(tablename, list); //System.out.println(">>>"+parent.getChildren()); } else if (!ret.getName().startsWith("java") && !ret.getSimpleName().toLowerCase().endsWith("byte[]") && !ret.getSimpleName().toLowerCase().equals("int")) // if complex { String tablename = ret.getSimpleName().toLowerCase(); tablename = tablename.endsWith("type") ? tablename.substring(0, tablename.lastIndexOf("type")) : tablename; FlexElement element = _elements.get(tablename); String idcolumn = element.getAttribute("idcolumn").getValue(); String realtablename = element.getAttribute("realtablename").getValue(); String parentidcolumn = element.getAttribute("parentidcolumn").getValue(); boolean includeidcolumns = element.getAttribute("includeidcolumns").getValue() == null ? false : (element.getAttribute("includeidcolumns").getValue().equalsIgnoreCase("true")); FlexElement idelement = element.getElementByName(parentidcolumn); PreparedStatement ps = (PreparedStatement) conn.prepareStatement("select * from " + prefix + ((realtablename != null) ? realtablename : tablename.toLowerCase()) + " where " + parentidcolumn + "=" + (idelement.getType().equals("string") ? ("'" + parentid + "'") : parentid)); ResultSet rec = ps.executeQuery(); // check if a record was found RecordSet res = new RecordSet(rec); if (res != null && res.size() > 0) { //String id = rec.getString("id"); String id = res.getString(idcolumn); Object o2 = _flexutils.getObject(FlexUtils.getRDBMSRecordAsXML(tablename, res, idcolumn, parentidcolumn, includeidcolumns), ret); if (o2 != null) { String setmethod = "set" + method.getName().substring(3); Object[] args = new Object[1]; args[0] = o2; Class[] cls = new Class[1]; cls[0] = o2.getClass(); Method met = c.getMethod(setmethod, cls); met.invoke(parent.getObject(), args); /*if (revivechildren) { reviveObject(id,o2,conn,prefix,revivechildren); }*/ } if (rec.last()) { break; } } } } } } catch (Exception f) { throw f; } }
From source file:org.broadleafcommerce.openadmin.server.service.persistence.module.BasicPersistenceModule.java
protected Class<?> getMapFieldType(Serializable instance, FieldManager fieldManager, Property property) { Class<?> returnType = null; Field field = fieldManager.getField(instance.getClass(), property.getName().substring(0, property.getName().indexOf(FieldManager.MAPFIELDSEPARATOR))); java.lang.reflect.Type type = field.getGenericType(); if (type instanceof ParameterizedType) { ParameterizedType pType = (ParameterizedType) type; Class<?> clazz = (Class<?>) pType.getActualTypeArguments()[1]; Class<?>[] entities = persistenceManager.getDynamicEntityDao() .getAllPolymorphicEntitiesFromCeiling(clazz); if (!ArrayUtils.isEmpty(entities)) { returnType = entities[entities.length - 1]; }/*w w w. ja v a2s . co m*/ } return returnType; }