List of usage examples for java.lang Class isAssignableFrom
@HotSpotIntrinsicCandidate public native boolean isAssignableFrom(Class<?> cls);
From source file:net.servicefixture.PluginManager.java
@SuppressWarnings("unchecked") private static Object newInstance(String classname, Class baseClazz) { try {//from w w w .j a v a 2 s . com Class clazz = ReflectionUtils.newClass(classname); if (baseClazz.isAssignableFrom(clazz)) { return ReflectionUtils.newInstance(clazz); } } catch (RuntimeException e) { throw new ServiceFixtureException("Unable to load plugins due to:" + e.getMessage()); } throw new ServiceFixtureException( "Unable to load plugins, class " + classname + " doesn't implement " + baseClazz.getName()); }
From source file:com.npower.dm.util.DMUtil.java
/** * Use reflection to access a URL[] getURLs or ULR[] getAllURLs method so that * non-URLClassLoader class loaders, or class loaders that override getURLs to * return null or empty, can provide the true classpath info. *//*from w w w.j av a 2 s .c o m*/ public static URL[] getClassLoaderURLs(ClassLoader cl) { URL[] urls = {}; try { Class<?> returnType = urls.getClass(); Class<?>[] parameterTypes = {}; Method getURLs = cl.getClass().getMethod("getURLs", parameterTypes); if (returnType.isAssignableFrom(getURLs.getReturnType())) { Object[] args = {}; urls = (URL[]) getURLs.invoke(cl, args); } } catch (Exception ignore) { } return urls; }
From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java
static Supplier<?> getStaticSupplier(Class<?> type, String key, String defaultValue, AbstractConfiguration configuration) { if (type.isAssignableFrom(String.class)) { return Suppliers.ofInstance(configuration.getString(key, defaultValue)); } else if (type.isAssignableFrom(Integer.class)) { return Suppliers.ofInstance( configuration.getInteger(key, defaultValue == null ? 0 : Integer.parseInt(defaultValue))); } else if (type.isAssignableFrom(Double.class)) { return Suppliers.ofInstance( configuration.getDouble(key, defaultValue == null ? 0.0 : Double.parseDouble(defaultValue))); } else if (type.isAssignableFrom(Long.class)) { return Suppliers.ofInstance( configuration.getLong(key, defaultValue == null ? 0L : Long.parseLong(defaultValue))); } else if (type.isAssignableFrom(Boolean.class)) { return Suppliers.ofInstance(configuration.getBoolean(key, defaultValue == null ? false : Boolean.parseBoolean(defaultValue))); }//from w ww.j a v a2 s . co m throw new RuntimeException("Unsupported value type " + type.getCanonicalName()); }
From source file:Main.java
public static <BoundType> boolean isJAXBElement(Class<BoundType> declaredType, QName name, Class scope, Object value) {/* w ww. ja va 2 s. c om*/ if (value == null) { return false; } else if (value instanceof JAXBElement) { final JAXBElement<?> element = (JAXBElement<?>) value; return element.getName().equals(name) && declaredType.isAssignableFrom(element.getDeclaredType()); } else { return false; } }
From source file:com.netflix.governator.configuration.ArchaiusConfigurationProvider.java
protected static Supplier<?> getDynamicSupplier(Class<?> type, String key, String defaultValue, DynamicPropertyFactory propertyFactory) { if (type.isAssignableFrom(String.class)) { return new PropertyWrapperSupplier<String>(propertyFactory.getStringProperty(key, defaultValue)); } else if (type.isAssignableFrom(Integer.class)) { return new PropertyWrapperSupplier<Integer>( propertyFactory.getIntProperty(key, defaultValue == null ? 0 : Integer.parseInt(defaultValue))); } else if (type.isAssignableFrom(Double.class)) { return new PropertyWrapperSupplier<Double>(propertyFactory.getDoubleProperty(key, defaultValue == null ? 0.0 : Double.parseDouble(defaultValue))); } else if (type.isAssignableFrom(Long.class)) { return new PropertyWrapperSupplier<Long>( propertyFactory.getLongProperty(key, defaultValue == null ? 0L : Long.parseLong(defaultValue))); } else if (type.isAssignableFrom(Boolean.class)) { return new PropertyWrapperSupplier<Boolean>(propertyFactory.getBooleanProperty(key, defaultValue == null ? false : Boolean.parseBoolean(defaultValue))); }/*from w ww. jav a2 s . c o m*/ throw new RuntimeException("Unsupported value type " + type.getCanonicalName()); }
From source file:com.netflix.paas.config.base.ConfigurationProxyUtils.java
static Supplier<?> getDynamicSupplier(Class<?> type, String key, String defaultValue, DynamicPropertyFactory propertyFactory) { if (type.isAssignableFrom(String.class)) { return new PropertyWrapperSupplier<String>(propertyFactory.getStringProperty(key, defaultValue)); } else if (type.isAssignableFrom(Integer.class)) { return new PropertyWrapperSupplier<Integer>( propertyFactory.getIntProperty(key, defaultValue == null ? 0 : Integer.parseInt(defaultValue))); } else if (type.isAssignableFrom(Double.class)) { return new PropertyWrapperSupplier<Double>(propertyFactory.getDoubleProperty(key, defaultValue == null ? 0.0 : Double.parseDouble(defaultValue))); } else if (type.isAssignableFrom(Long.class)) { return new PropertyWrapperSupplier<Long>( propertyFactory.getLongProperty(key, defaultValue == null ? 0L : Long.parseLong(defaultValue))); } else if (type.isAssignableFrom(Boolean.class)) { return new PropertyWrapperSupplier<Boolean>(propertyFactory.getBooleanProperty(key, defaultValue == null ? false : Boolean.parseBoolean(defaultValue))); }//from w w w.j a va 2s.c o m throw new RuntimeException("Unsupported value type " + type.getCanonicalName()); }
From source file:com.impetus.client.cassandra.common.CassandraUtilities.java
/** * @param value// www . ja va2 s .com * @param f * @return */ public static ByteBuffer toBytes(Object value, Class<?> clazz) { if (clazz.isAssignableFrom(String.class)) { return UTF8Type.instance.decompose((String) value); } else if (clazz.equals(int.class) || clazz.isAssignableFrom(Integer.class)) { return Int32Type.instance.decompose(Integer.parseInt(value.toString())); } else if (clazz.equals(long.class) || clazz.isAssignableFrom(Long.class)) { return LongType.instance.decompose(Long.parseLong(value.toString())); } else if (clazz.equals(boolean.class) || clazz.isAssignableFrom(Boolean.class)) { return BooleanType.instance.decompose(Boolean.valueOf(value.toString())); } else if (clazz.equals(double.class) || clazz.isAssignableFrom(Double.class)) { return DoubleType.instance.decompose(Double.valueOf(value.toString())); } else if (clazz.isAssignableFrom(java.util.UUID.class)) { return UUIDType.instance.decompose(UUID.fromString(value.toString())); } else if (clazz.equals(float.class) || clazz.isAssignableFrom(Float.class)) { return FloatType.instance.decompose(Float.valueOf(value.toString())); } else if (clazz.isAssignableFrom(Date.class)) { DateAccessor dateAccessor = new DateAccessor(); return DateType.instance.decompose((Date) value); } else { if (value.getClass().isAssignableFrom(String.class)) { value = PropertyAccessorFactory.getPropertyAccessor(clazz).fromString(clazz, value.toString()); } return BytesType.instance .decompose(ByteBuffer.wrap(PropertyAccessorFactory.getPropertyAccessor(clazz).toBytes(value))); } }
From source file:org.lambdamatic.internal.elasticsearch.codec.DocumentCodec.java
/** * Converts the given {@code value} into a value of type {@code targetType}. * <p>//from w w w .j a v a 2s. co m * <strong>Note:</strong>The conversion relies on the existence of a static * {@code valueOf(String)} method in the given {@code targetType} to convert the value. * </p> * * @param value the input value * @param targetType the target type of the value to return * @return the converted value (or the value itself if no conversion was necessary) */ protected static Object convertValue(final Object value, final Class<?> targetType) { if (targetType.isAssignableFrom(value.getClass())) { return value; } else if (targetType.isPrimitive()) { if (targetType == boolean.class) { return Boolean.parseBoolean(value.toString()); } else if (targetType == byte.class) { return Byte.parseByte(value.toString()); } else if (targetType == short.class) { return Short.parseShort(value.toString()); } else if (targetType == int.class) { return Integer.parseInt(value.toString()); } else if (targetType == long.class) { return Long.parseLong(value.toString()); } else if (targetType == double.class) { return Double.parseDouble(value.toString()); } else if (targetType == float.class) { return Float.parseFloat(value.toString()); } throw new CodecException("Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getName() + ": no object to primitive conversion available."); } try { final Method convertMethod = getConvertMethod(targetType); if (convertMethod != null) { return convertMethod.invoke(null, value.toString()); } throw new CodecException( "Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getName() + ": no conversion method available."); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) { throw new CodecException("Failed to convert value '" + value.toString() + "' (" + value.getClass().getName() + ") into a " + targetType.getClass().getName(), e); } }
From source file:baggage.BaseTestCase.java
protected static void assertFailure(Class<? extends Exception> klass, Fallible fallible) { try {/* w w w . ja va 2 s . c o m*/ fallible.execute(); } catch (Exception e) { if (!klass.isAssignableFrom(e.getClass())) { throw new RuntimeException( "Expected a " + klass.getSimpleName() + ", got a " + e.getClass().getSimpleName(), e); } return; } Assert.fail("Expected a " + klass.getSimpleName() + ", got no exceptions at all"); }
From source file:com.haulmont.cuba.core.config.type.TypeFactory.java
private static boolean isAcceptableMethod(Class<?> returnType, Method factoryMethod) { int modifiers = factoryMethod.getModifiers(); return Modifier.isStatic(modifiers) && Modifier.isPublic(modifiers) && returnType.isAssignableFrom(factoryMethod.getReturnType()); }