List of usage examples for java.lang Class asSubclass
@SuppressWarnings("unchecked") public <U> Class<? extends U> asSubclass(Class<U> clazz)
From source file:org.apache.nifi.controller.ExtensionBuilder.java
private ControllerServiceNode createControllerServiceNode() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InitializationException { final ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader(); try {/* www . ja v a2s . c om*/ final Bundle bundle = extensionManager.getBundle(bundleCoordinate); if (bundle == null) { throw new IllegalStateException( "Unable to find bundle for coordinate " + bundleCoordinate.getCoordinate()); } final ClassLoader detectedClassLoader = extensionManager.createInstanceClassLoader(type, identifier, bundle, classpathUrls == null ? Collections.emptySet() : classpathUrls); final Class<?> rawClass = Class.forName(type, true, detectedClassLoader); Thread.currentThread().setContextClassLoader(detectedClassLoader); final Class<? extends ControllerService> controllerServiceClass = rawClass .asSubclass(ControllerService.class); final ControllerService serviceImpl = controllerServiceClass.newInstance(); final StandardControllerServiceInvocationHandler invocationHandler = new StandardControllerServiceInvocationHandler( extensionManager, serviceImpl); // extract all interfaces... controllerServiceClass is non null so getAllInterfaces is non null final List<Class<?>> interfaceList = ClassUtils.getAllInterfaces(controllerServiceClass); final Class<?>[] interfaces = interfaceList.toArray(new Class<?>[0]); final ControllerService proxiedService; if (detectedClassLoader == null) { proxiedService = (ControllerService) Proxy.newProxyInstance(getClass().getClassLoader(), interfaces, invocationHandler); } else { proxiedService = (ControllerService) Proxy.newProxyInstance(detectedClassLoader, interfaces, invocationHandler); } logger.info("Created Controller Service of type {} with identifier {}", type, identifier); final ComponentLog serviceLogger = new SimpleProcessLogger(identifier, serviceImpl); final TerminationAwareLogger terminationAwareLogger = new TerminationAwareLogger(serviceLogger); final StateManager stateManager = stateManagerProvider.getStateManager(identifier); final ControllerServiceInitializationContext initContext = new StandardControllerServiceInitializationContext( identifier, terminationAwareLogger, serviceProvider, stateManager, kerberosConfig); serviceImpl.initialize(initContext); final LoggableComponent<ControllerService> originalLoggableComponent = new LoggableComponent<>( serviceImpl, bundleCoordinate, terminationAwareLogger); final LoggableComponent<ControllerService> proxiedLoggableComponent = new LoggableComponent<>( proxiedService, bundleCoordinate, terminationAwareLogger); final ComponentVariableRegistry componentVarRegistry = new StandardComponentVariableRegistry( this.variableRegistry); final ValidationContextFactory validationContextFactory = new StandardValidationContextFactory( serviceProvider, componentVarRegistry); final ControllerServiceNode serviceNode = new StandardControllerServiceNode(originalLoggableComponent, proxiedLoggableComponent, invocationHandler, identifier, validationContextFactory, serviceProvider, componentVarRegistry, reloadComponent, extensionManager, validationTrigger); serviceNode.setName(rawClass.getSimpleName()); invocationHandler.setServiceNode(serviceNode); return serviceNode; } finally { if (ctxClassLoader != null) { Thread.currentThread().setContextClassLoader(ctxClassLoader); } } }
From source file:jef.database.DbUtils.java
/** * class// w w w . j a v a 2 s.c om * * @param field * @return */ public static AbstractMetadata getTableMeta(Field field) { Assert.notNull(field); if (field instanceof MetadataContainer) { return (AbstractMetadata) ((MetadataContainer) field).getMeta(); } if (field instanceof Enum) { // FIXME ?? Class<?> c = field.getClass().getDeclaringClass(); Assert.isTrue(IQueryableEntity.class.isAssignableFrom(c), field + " is not a defined in a IQueryableEntity's meta-model."); return MetaHolder.getMeta(c.asSubclass(IQueryableEntity.class)); } else { throw new IllegalArgumentException( "method 'getTableMeta' doesn't support field type of " + field.getClass()); } }
From source file:org.apache.hadoop.yarn.server.nodemanager.containermanager.logaggregation.AppLogAggregatorImpl.java
private ContainerLogAggregationPolicy getLogAggPolicyInstance(Configuration conf) { Class<? extends ContainerLogAggregationPolicy> policyClass = null; if (this.logAggregationContext != null) { String className = this.logAggregationContext.getLogAggregationPolicyClassName(); if (className != null) { try { Class<?> policyFromContext = conf.getClassByName(className); if (ContainerLogAggregationPolicy.class.isAssignableFrom(policyFromContext)) { policyClass = policyFromContext.asSubclass(ContainerLogAggregationPolicy.class); } else { LOG.warn(this.appId + " specified invalid log aggregation policy " + className); }/*from w w w.ja v a 2 s . c o m*/ } catch (ClassNotFoundException cnfe) { // We don't fail the app if the policy class isn't valid. LOG.warn(this.appId + " specified invalid log aggregation policy " + className); } } } if (policyClass == null) { policyClass = conf.getClass(YarnConfiguration.NM_LOG_AGG_POLICY_CLASS, AllContainerLogAggregationPolicy.class, ContainerLogAggregationPolicy.class); } else { LOG.info(this.appId + " specifies ContainerLogAggregationPolicy of " + policyClass); } return ReflectionUtils.newInstance(policyClass, conf); }
From source file:com.asakusafw.runtime.stage.launcher.LauncherOptionsParser.java
private Class<? extends Tool> buildApplicationClass(String applicationClassName) { Class<? extends Tool> applicationClass; try {/* w w w .j av a 2s .c om*/ Class<?> aClass = configuration.getClassByName(applicationClassName); if (Tool.class.isAssignableFrom(aClass) == false) { throw new IllegalArgumentException( MessageFormat.format("Application \"{0}\" must be a subclass of \"{1}\"", aClass.getName(), Tool.class.getName())); } applicationClass = aClass.asSubclass(Tool.class); } catch (ClassNotFoundException e) { throw new IllegalArgumentException( MessageFormat.format("Application \"{0}\" is not found", applicationClassName)); } return applicationClass; }
From source file:com.asakusafw.runtime.stage.input.BridgeInputFormat.java
@SuppressWarnings("unchecked") private Class<? extends DataFormat<?>> extractFormatClass(JobContext context, StageInput input) throws IOException { assert context != null; assert input != null; String value = extract(input, DirectDataSourceConstants.KEY_FORMAT_CLASS); try {/*from w ww . j a va2 s. com*/ Class<?> aClass = Class.forName(value, false, context.getConfiguration().getClassLoader()); return (Class<? extends DataFormat<?>>) aClass.asSubclass(DataFormat.class); } catch (Exception e) { throw new IOException( MessageFormat.format("Invalid format class: \"{1}\" (path={0})", extractBasePath(input), value), e); } }
From source file:com.asakusafw.runtime.stage.input.BridgeInputFormat.java
@SuppressWarnings("unchecked") private Class<? extends DataFilter<?>> extractFilterClass(JobContext context, StageInput input) throws IOException { assert context != null; assert input != null; String value = input.getAttributes().get(DirectDataSourceConstants.KEY_FILTER_CLASS); if (value == null) { return null; }/*from w w w. j a v a2 s . co m*/ try { Class<?> aClass = Class.forName(value, false, context.getConfiguration().getClassLoader()); return (Class<? extends DataFilter<?>>) aClass.asSubclass(DataFilter.class); } catch (Exception e) { throw new IOException( MessageFormat.format("Invalid format class: \"{1}\" (path={0})", extractBasePath(input), value), e); } }
From source file:org.pentaho.metadata.SQLModelGeneratorTest.java
private Connection getDataSourceConnection(String driverClass, String name, String username, String password, String url) throws Exception { Connection conn = null;/*from ww w . j ava 2s . c om*/ if (StringUtils.isEmpty(driverClass)) { throw new Exception("Connection attempt failed"); //$NON-NLS-1$ } Class<?> driverC = null; try { driverC = Class.forName(driverClass); } catch (ClassNotFoundException e) { throw new Exception("Driver not found in the class path. Driver was " + driverClass, e); //$NON-NLS-1$ } if (!Driver.class.isAssignableFrom(driverC)) { throw new Exception("Driver not found in the class path. Driver was " + driverClass); //$NON-NLS-1$ } } Driver driver = null; try { driver = driverC.asSubclass(Driver.class).newInstance(); } catch (InstantiationException e) { throw new Exception("Unable to instance the driver", e); //$NON-NLS-1$ } catch (IllegalAccessException e) { throw new Exception("Unable to instance the driver", e); //$NON-NLS-1$ } } try { DriverManager.registerDriver(driver); conn = DriverManager.getConnection(url, username, password); return conn; } catch (SQLException e) { throw new Exception("Unable to connect", e); //$NON-NLS-1$ } }
From source file:org.opensingular.form.STypeSimple.java
@SuppressWarnings("unchecked") @Override/* ww w. j av a 2s . c o m*/ public <T> T convert(Object value, Class<T> resultClass) { if (value == null) { return null; } else if (resultClass.isAssignableFrom(valueClass)) { return resultClass.cast(convert(value)); } else if (resultClass.isInstance(value)) { return resultClass.cast(value); } else if (resultClass.isAssignableFrom(String.class)) { if (valueClass.isInstance(value)) { return resultClass.cast(toStringPersistence(valueClass.cast(value))); } return resultClass.cast(value.toString()); } else if (Enum.class.isAssignableFrom(resultClass)) { Class<? extends Enum> enumClass = resultClass.asSubclass(Enum.class); return (T) Enum.valueOf(enumClass, value.toString()); } else { Converter apacheConverter = ConvertUtils.lookup(value.getClass(), resultClass); if (apacheConverter != null) { return resultClass.cast(apacheConverter.convert(resultClass, value)); } } throw createConversionError(value, resultClass); }
From source file:nl.knaw.dans.common.ldap.repo.LdapMapper.java
@SuppressWarnings("unchecked") private Object getSingleValue(Class<?> type, Object o) throws NamingException { Object value = null;//from ww w . j a va2s .co m if (o != null) { if (type.isPrimitive()) { value = getPrimitive(type, (String) o); } else if (type.isEnum()) { value = Enum.valueOf(type.asSubclass(Enum.class), (String) o); } else { value = o; } } return value; }
From source file:org.cloudata.core.common.conf.CloudataConf.java
/** Returns the value of the <code>name</code> property as a Class. If no * such property is specified, then <code>defaultValue</code> is returned. * An error is thrown if the returned class does not implement the named * interface. // w w w.j ava 2 s . co m */ public <U> Class<? extends U> getClass(String propertyName, Class<? extends U> defaultValue, Class<U> xface) { try { Class<?> theClass = getClass(propertyName, defaultValue); 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); } }