List of usage examples for java.lang.reflect Type toString
public String toString()
From source file:com.complexible.pinto.RDFMapper.java
private Class pinpointClass(final Model theGraph, final Resource theResource, final PropertyDescriptor theDescriptor) { Class aClass = theDescriptor.getPropertyType(); if (Collection.class.isAssignableFrom(aClass)) { // if the field we're assigning from is a collection, try and figure out the type of the thing // we're creating from the collection Type[] aTypes = null;// www . j a va 2 s . com if (theDescriptor.getReadMethod().getGenericParameterTypes().length > 0) { // should this be the return type? eg new Type[] { theDescriptor.getReadMethod().getGenericReturnType() }; aTypes = theDescriptor.getReadMethod().getGenericParameterTypes(); } else if (theDescriptor.getWriteMethod().getGenericParameterTypes().length > 0) { aTypes = theDescriptor.getWriteMethod().getGenericParameterTypes(); } if (aTypes != null && aTypes.length >= 1) { // first type argument to a collection is usually the one we care most about if (aTypes[0] instanceof ParameterizedType && ((ParameterizedType) aTypes[0]).getActualTypeArguments().length > 0) { Type aType = ((ParameterizedType) aTypes[0]).getActualTypeArguments()[0]; if (aType instanceof Class) { aClass = (Class) aType; } else if (aType instanceof WildcardTypeImpl) { WildcardTypeImpl aWildcard = (WildcardTypeImpl) aType; // trying to suss out super v extends w/o resorting to string munging. if (aWildcard.getLowerBounds().length == 0 && aWildcard.getUpperBounds().length > 0) { // no lower bounds afaik indicates ? extends Foo aClass = ((Class) aWildcard.getUpperBounds()[0]); } else if (aWildcard.getLowerBounds().length > 0) { // lower & upper bounds I believe indicates something of the form Foo super Bar aClass = ((Class) aWildcard.getLowerBounds()[0]); } else { // shoot, we'll try the string hack that Adrian posted on the mailing list. try { aClass = Class.forName(aType.toString().split(" ")[2].substring(0, aTypes[0].toString().split(" ")[2].length() - 1)); } catch (Exception e) { // everything has failed, let aClass be the default (theClass) and hope for the best } } } else { // punt? wtf else could it be? try { aClass = Class.forName(aType.toString()); } catch (ClassNotFoundException e) { // oh well, we did the best we can } } } else if (aTypes[0] instanceof Class) { aClass = (Class) aTypes[0]; } } else { LOGGER.info("Could not find type for collection %s", aClass); } } else if (!Classes.isInstantiable(aClass) || !Classes.hasDefaultConstructor(aClass)) { Class<?> aCurr = null; final Iterable<Resource> aRdfTypes = Models2.getTypes(theGraph, theResource); for (Resource aType : aRdfTypes) { Class<?> aMappedClass = mMappings.get(aType); if (aMappedClass != null) { if (aCurr == null) { aCurr = aMappedClass; } else if (aCurr.isAssignableFrom(aMappedClass)) { // we want the most specific class, that's likely to be what's instantiable aCurr = aMappedClass; } } } if (aCurr != null) { aClass = aCurr; } } return aClass; }
From source file:reflex.node.KernelExecutor.java
private static Object handleParameterizedType(ReflexValue v, Type type) { if (!(type instanceof ParameterizedType)) { if (type.equals(String.class)) return v.asString(); if (type.equals(Double.class)) return v.asDouble(); if (type.equals(Long.class)) return v.asLong(); if (type.equals(BigDecimal.class)) return v.asBigDecimal(); return v.asObject(); }/* www .ja va 2 s .co m*/ ParameterizedType pType = (ParameterizedType) type; if (pType.getRawType().equals(Map.class)) { if (!v.isMap()) { log.error(v.toString() + " is not a map"); return null; } Map<String, Object> convertedMap = new LinkedHashMap<>(); for (Entry<String, Object> entry : v.asMap().entrySet()) { Type[] innerType = pType.getActualTypeArguments(); String key = null; if (!innerType[0].equals(String.class)) { // This could get tricky log.warn("Keys for maps should always be Strings"); } key = entry.getKey().toString(); Object value = entry.getValue(); if (value instanceof ReflexValue) convertedMap.put(key, handleParameterizedType((ReflexValue) value, innerType[1])); else convertedMap.put(key, value); } return convertedMap; } else if (pType.getRawType().equals(List.class)) { if (!v.isList()) { log.error(v.toString() + " is not a list"); return null; } List<ReflexValue> inner = v.asList(); Type innerType = pType.getActualTypeArguments()[0]; if (innerType.equals(String.class)) { List<String> ret = new ArrayList<String>(inner.size()); for (ReflexValue vi : inner) { ret.add(vi.asString()); } return ret; } else if (innerType.equals(Double.class)) { List<Double> ret = new ArrayList<>(inner.size()); for (ReflexValue vi : inner) { ret.add(vi.asDouble()); } return ret; } else if (innerType.equals(Long.class)) { List<Long> ret = new ArrayList<>(inner.size()); for (ReflexValue vi : inner) { ret.add(vi.asLong()); } return ret; } else if (innerType.equals(BigDecimal.class)) { List<BigDecimal> ret = new ArrayList<>(inner.size()); for (ReflexValue vi : inner) { ret.add(vi.asBigDecimal()); } return ret; } else if (innerType instanceof ParameterizedType || inner instanceof List) { List<Object> ret = new ArrayList<>(); for (ReflexValue vi : inner) { ret.add(handleParameterizedType(vi, innerType)); } return ret; } else { log.warn("Cannot convert " + v.toString() + " to " + type.toString()); } } else if (v.isMap()) { // If it's a map then JacksonUtil may be able to recreate the type return handleMap(v, type); } return v.asObject(); }
From source file:au.com.addstar.cellblock.configuration.AutoConfig.java
@SuppressWarnings("unchecked") public boolean load() { FileConfiguration yml = new YamlConfiguration(); try {//from w w w . j a va 2 s . c o m if (mFile.getParentFile().exists() || mFile.getParentFile().mkdirs()) {// Make sure the file exists if (mFile.exists() || mFile.createNewFile()) { // Parse the config yml.load(mFile); for (Field field : getClass().getDeclaredFields()) { ConfigField configField = field.getAnnotation(ConfigField.class); if (configField == null) continue; String optionName = configField.name(); if (optionName.isEmpty()) optionName = field.getName(); field.setAccessible(true); String path = (configField.category().isEmpty() ? "" : configField.category() + ".") //$NON-NLS-2$ + optionName; if (!yml.contains(path)) { if (field.get(this) == null) throw new InvalidConfigurationException( path + " is required to be set! Info:\n" + configField.comment()); } else { // Parse the value if (field.getType().isArray()) { // Integer if (field.getType().getComponentType().equals(Integer.TYPE)) field.set(this, yml.getIntegerList(path).toArray(new Integer[0])); // Float else if (field.getType().getComponentType().equals(Float.TYPE)) field.set(this, yml.getFloatList(path).toArray(new Float[0])); // Double else if (field.getType().getComponentType().equals(Double.TYPE)) field.set(this, yml.getDoubleList(path).toArray(new Double[0])); // Long else if (field.getType().getComponentType().equals(Long.TYPE)) field.set(this, yml.getLongList(path).toArray(new Long[0])); // Short else if (field.getType().getComponentType().equals(Short.TYPE)) field.set(this, yml.getShortList(path).toArray(new Short[0])); // Boolean else if (field.getType().getComponentType().equals(Boolean.TYPE)) field.set(this, yml.getBooleanList(path).toArray(new Boolean[0])); // String else if (field.getType().getComponentType().equals(String.class)) { field.set(this, yml.getStringList(path).toArray(new String[0])); } else throw new IllegalArgumentException("Cannot use type " + field.getType().getSimpleName() + " for AutoConfiguration"); //$NON-NLS-1$ } else if (List.class.isAssignableFrom(field.getType())) { if (field.getGenericType() == null) throw new IllegalArgumentException( "Cannot use type List without specifying generic type for AutoConfiguration"); Type type = ((ParameterizedType) field.getGenericType()) .getActualTypeArguments()[0]; if (type.equals(Integer.class)) field.set(this, newList((Class<? extends List<Integer>>) field.getType(), yml.getIntegerList(path))); else if (type.equals(Float.class)) field.set(this, newList((Class<? extends List<Float>>) field.getType(), yml.getFloatList(path))); else if (type.equals(Double.class)) field.set(this, newList((Class<? extends List<Double>>) field.getType(), yml.getDoubleList(path))); else if (type.equals(Long.class)) field.set(this, newList((Class<? extends List<Long>>) field.getType(), yml.getLongList(path))); else if (type.equals(Short.class)) field.set(this, newList((Class<? extends List<Short>>) field.getType(), yml.getShortList(path))); else if (type.equals(Boolean.class)) field.set(this, newList((Class<? extends List<Boolean>>) field.getType(), yml.getBooleanList(path))); else if (type.equals(String.class)) field.set(this, newList((Class<? extends List<String>>) field.getType(), yml.getStringList(path))); else throw new IllegalArgumentException( "Cannot use type " + field.getType().getSimpleName() + "<" //$NON-NLS-2$ + type.toString() + "> for AutoConfiguration"); } else if (Set.class.isAssignableFrom(field.getType())) { if (field.getGenericType() == null) throw new IllegalArgumentException( "Cannot use type set without specifying generic type for AytoConfiguration"); Type type = ((ParameterizedType) field.getGenericType()) .getActualTypeArguments()[0]; if (type.equals(Integer.class)) field.set(this, newSet((Class<? extends Set<Integer>>) field.getType(), yml.getIntegerList(path))); else if (type.equals(Float.class)) field.set(this, newSet((Class<? extends Set<Float>>) field.getType(), yml.getFloatList(path))); else if (type.equals(Double.class)) field.set(this, newSet((Class<? extends Set<Double>>) field.getType(), yml.getDoubleList(path))); else if (type.equals(Long.class)) field.set(this, newSet((Class<? extends Set<Long>>) field.getType(), yml.getLongList(path))); else if (type.equals(Short.class)) field.set(this, newSet((Class<? extends Set<Short>>) field.getType(), yml.getShortList(path))); else if (type.equals(Boolean.class)) field.set(this, newSet((Class<? extends Set<Boolean>>) field.getType(), yml.getBooleanList(path))); else if (type.equals(String.class)) field.set(this, newSet((Class<? extends Set<String>>) field.getType(), yml.getStringList(path))); else throw new IllegalArgumentException( "Cannot use type " + field.getType().getSimpleName() + "<" //$NON-NLS-2$ + type.toString() + "> for AutoConfiguration"); } else { // Integer if (field.getType().equals(Integer.TYPE)) field.setInt(this, yml.getInt(path)); // Float else if (field.getType().equals(Float.TYPE)) field.setFloat(this, (float) yml.getDouble(path)); // Double else if (field.getType().equals(Double.TYPE)) field.setDouble(this, yml.getDouble(path)); // Long else if (field.getType().equals(Long.TYPE)) field.setLong(this, yml.getLong(path)); // Short else if (field.getType().equals(Short.TYPE)) field.setShort(this, (short) yml.getInt(path)); // Boolean else if (field.getType().equals(Boolean.TYPE)) field.setBoolean(this, yml.getBoolean(path)); // ItemStack else if (field.getType().equals(ItemStack.class)) field.set(this, yml.getItemStack(path)); // String else if (field.getType().equals(String.class)) field.set(this, yml.getString(path)); else throw new IllegalArgumentException("Cannot use type " + field.getType().getSimpleName() + " for AutoConfiguration"); //$NON-NLS-1$ } } } onPostLoad(); } else { Bukkit.getLogger().log(Level.INFO, "Unable to create file: " + mFile.toString()); } } else { Bukkit.getLogger().log(Level.INFO, "Unable to create file: " + mFile.getParentFile().toString()); } return true; } catch (IOException | InvalidConfigurationException | IllegalAccessException | IllegalArgumentException e) { e.printStackTrace(); return false; } }