List of usage examples for java.lang Class asSubclass
@SuppressWarnings("unchecked") public <U> Class<? extends U> asSubclass(Class<U> clazz)
From source file:com.netflix.simianarmy.aws.SimpleDBRecorder.java
/** * Value to enum. Converts a "name|type" string back to an enum. * * @param value/*from www . j a v a 2 s . c om*/ * the value * @return the enum */ private static <T extends NamedType> T valueToEnum(Class<T> type, String value) { // parts = [enum value, enum class type] String[] parts = value.split("\\|", 2); if (parts.length < 2) { throw new RuntimeException("value " + value + " does not appear to be an internal enum format"); } Class<?> enumClass; try { enumClass = Class.forName(parts[1]); } catch (ClassNotFoundException e) { throw new RuntimeException("class for enum value " + value + " not found"); } if (!enumClass.isEnum()) { throw new RuntimeException("value " + value + " does not appear to be of an enum type"); } if (!type.isAssignableFrom(enumClass)) { throw new RuntimeException("value " + value + " cannot be assigned to a variable of this type: " + type.getCanonicalName()); } @SuppressWarnings("rawtypes") Class<? extends Enum> enumType = enumClass.asSubclass(Enum.class); @SuppressWarnings("unchecked") T enumValue = (T) Enum.valueOf(enumType, parts[0]); return enumValue; }
From source file:com.asakusafw.lang.compiler.cli.BatchCompilerCli.java
private static <T> T newInstance(ClassLoader classLoader, Class<T> type, ClassDescription aClass) { try {//from w w w. jav a 2 s. com Class<?> resolved = aClass.resolve(classLoader); if (type.isAssignableFrom(resolved) == false) { throw new DiagnosticException(Diagnostic.Level.ERROR, MessageFormat .format("{0} must be a subtype of {1}", type.getName(), aClass.getClassName())); } return resolved.asSubclass(type).newInstance(); } catch (ReflectiveOperationException e) { throw new DiagnosticException(Diagnostic.Level.ERROR, MessageFormat.format("failed to instantiate a class: {0}", aClass.getClassName()), e); } }
From source file:io.janusproject.Boot.java
private static Class<? extends Agent> loadAgentClass(String fullyQualifiedName) { Class<?> type; try {//w w w . j a v a 2s . c om type = Class.forName(fullyQualifiedName); } catch (Exception e) { showError(Locale.getString("INVALID_AGENT_QUALIFIED_NAME", //$NON-NLS-1$ fullyQualifiedName, System.getProperty("java.class.path")), //$NON-NLS-1$ null); return null; } // The following test is needed because the // cast to Class<? extends Agent> is not checking // the Agent type (it is a generic type, not // tested at runtime). if (Agent.class.isAssignableFrom(type)) { return type.asSubclass(Agent.class); } showError(Locale.getString("INVALID_AGENT_TYPE", //$NON-NLS-1$ fullyQualifiedName), null); return null; }
From source file:com.eviware.loadui.impl.statistics.db.util.TypeConverter.java
public static Object stringToObject(String value, Class<? extends Object> type) throws IOException { if (value == null) { return null; } else if (type == Long.class) { return Long.valueOf(value); } else if (type == Integer.class) { return Integer.valueOf(value); } else if (type == Double.class) { return Double.valueOf(value); } else if (type == Float.class) { return Float.valueOf(value); } else if (type == Boolean.class) { return Boolean.valueOf(value); } else if (type == String.class) { return value; } else if (type == Date.class) { Date d = new Date(); d.setTime(Long.valueOf(value)); return d; } else if (type == BufferedImage.class) { return imageFromByteArray(Base64.decodeBase64(value)); } else {//from ww w. java2 s.c om try { type.asSubclass(Serializable.class); return base64ToObject(value); } catch (Exception e) { return value; } } }
From source file:com.mongodb.hadoop.util.MapredMongoConfigUtil.java
/** * Fetch a class by its actual class name, rather than by a key name in the configuration properties. We still need to pass in a * Configuration object here, since the Configuration class maintains an internal cache of class names for performance on some hadoop * versions. It also ensures that the same classloader is used across all keys. *//*from w ww. jav a 2 s .c o m*/ public static <U> Class<? extends U> getClassByName(final Configuration conf, final String className, final Class<U> xface) { if (className == null) { return null; } try { Class<?> theClass = conf.getClassByName(className); if (theClass != null && !xface.isAssignableFrom(theClass)) { throw new RuntimeException(theClass + " not " + xface.getName()); } else if (theClass != null) { return theClass.asSubclass(xface); } else { return null; } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.apache.crunch.types.writable.Writables.java
public static final <T, W extends Writable> WritableType<T, W> records(Class<T> clazz) { if (EXTENSIONS.containsKey(clazz)) { return (WritableType<T, W>) EXTENSIONS.get(clazz); }/*from w w w. ja v a 2s . co m*/ if (Writable.class.isAssignableFrom(clazz)) { return (WritableType<T, W>) writables(clazz.asSubclass(Writable.class)); } else { throw new IllegalArgumentException( "Cannot create Writable records from non-Writable class" + clazz.getCanonicalName()); } }
From source file:net.nullschool.grains.jackson.datatype.GrainsDeserializers.java
@Override public JsonDeserializer<?> findMapDeserializer(MapType type, DeserializationConfig config, BeanDescription beanDesc, KeyDeserializer keyDeserializer, TypeDeserializer valueTypeDeserializer, JsonDeserializer<?> valueDeserializer) throws JsonMappingException { Class<?> clazz = type.getRawClass(); if (Grain.class.isAssignableFrom(clazz)) { return new GrainDeserializer(clazz.asSubclass(Grain.class)); }// ww w .j av a 2 s . c o m return null; }
From source file:com.yahoo.omid.committable.hbase.RegionSplitter.java
/** * @throws IOException//from www . j a va 2 s. c o m * if the specified SplitAlgorithm class couldn't be instantiated */ public static SplitAlgorithm newSplitAlgoInstance(Configuration conf, String splitClassName) throws IOException { Class<?> splitClass; // For split algorithms builtin to RegionSplitter, the user can specify // their simple class name instead of a fully qualified class name. if (splitClassName.equals(UniformSplit.class.getSimpleName())) { splitClass = UniformSplit.class; } else { try { splitClass = conf.getClassByName(splitClassName); } catch (ClassNotFoundException e) { throw new IOException("Couldn't load split class " + splitClassName, e); } if (splitClass == null) { throw new IOException("Failed loading split class " + splitClassName); } if (!SplitAlgorithm.class.isAssignableFrom(splitClass)) { throw new IOException("Specified split class doesn't implement SplitAlgorithm"); } } try { return splitClass.asSubclass(SplitAlgorithm.class).newInstance(); } catch (Exception e) { throw new IOException("Problem loading split algorithm: ", e); } }
From source file:controllers.modules.opinionMiners.base.OpinionMiner.java
public Set<Class<? extends OpinionMiner>> getSubMiners() { return Sets.newHashSet(Iterables.transform(getSubMiners(this.getClass()), new Function<Class<?>, Class<? extends OpinionMiner>>() { @Override//from w ww . j ava 2 s .c o m @Nullable public Class<? extends OpinionMiner> apply(@Nullable Class<?> input) { try { return input.asSubclass(OpinionMiner.class); } catch (ClassCastException e) { return null; } } })); }
From source file:com.mindquarry.persistence.config.PersistenceConfiguration.java
private Class loadEntityClass(String name) { Class<?> clazz = loadClass(name); try {/*from www. java2 s . co m*/ return clazz.asSubclass(EntityBase.class); } catch (ClassCastException e) { throw new InitializationException( "the class " + clazz + " , configured within mindquarry-persistence.xml" + " is is not a valid entity class; " + " it have to subclass " + EntityBase.class); } }