List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:org.apache.hadoop.mapred.lib.CombineFileRecordReader.java
/** * A generic RecordReader that can hand out different recordReaders * for each chunk in the CombineFileSplit. */// w w w. ja va 2 s . c o m public CombineFileRecordReader(JobConf job, CombineFileSplit split, Reporter reporter, Class<RecordReader<K, V>> rrClass) throws IOException { this.split = split; this.jc = job; this.rrClass = rrClass; this.reporter = reporter; this.idx = 0; this.curReader = null; this.progress = 0; try { rrConstructor = rrClass.getDeclaredConstructor(constructorSignature); rrConstructor.setAccessible(true); } catch (Exception e) { throw new RuntimeException(rrClass.getName() + " does not have valid constructor", e); } initNextRecordReader(); }
From source file:org.jtester.utility.ReflectionUtils.java
/** * Creates an instance of the given type * // ww w .j av a2 s . c o m * @param <T> * The type of the instance * @param type * The type of the instance * @param bypassAccessibility * If true, no exception is thrown if the parameterless * constructor is not public * @param argumentTypes * The constructor arg types, not null * @param arguments * The constructor args, not null * @return An instance of this type * @throws JTesterException * If an instance could not be created */ @SuppressWarnings("rawtypes") public static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments) { if (type.isMemberClass() && !isStatic(type.getModifiers())) { throw new JTesterException( "Creation of an instance of a non-static innerclass is not possible using reflection. The type " + type.getSimpleName() + " is only known in the context of an instance of the enclosing class " + type.getEnclosingClass().getSimpleName() + ". Declare the innerclass as static to make construction possible."); } try { Constructor<T> constructor = type.getDeclaredConstructor(argumentTypes); if (bypassAccessibility) { constructor.setAccessible(true); } return constructor.newInstance(arguments); } catch (InvocationTargetException e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e.getCause()); } catch (Exception e) { throw new JTesterException("Error while trying to create object of class " + type.getName(), e); } }
From source file:org.polymap.core.runtime.DefaultSessionContext.java
public final <T> T sessionSingleton(final Class<T> type) { assert type != null; checkDestroyed();//from ww w. j av a2 s. c om try { T result = (T) attributes.get(type.getName()); if (result == null) { // create an instance (without write lock) Constructor constructor = type.getDeclaredConstructor(new Class[] {}); if (constructor.isAccessible()) { result = type.newInstance(); } else { constructor.setAccessible(true); result = (T) constructor.newInstance(new Object[] {}); } Object old = attributes.putIfAbsent(type.getName(), result); // as there is no lock we have to check after put what object was added actually result = old != null ? (T) old : result; } return result; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.javascool.compiler.ProgletCodeCompiler.java
/** * Cherche si la proglet dfinit un Translator. Permet d'acceder aux translators des package du Classpath. * * @param progletName L'identificateur de la proglet * @return Le Translator utiliser pour cette proglet *//*from w w w . j ava2s. c o m*/ protected JVSTranslator getTranslatorForProglet(String progletName) { if (!isDefaultProglet(progletName)) { try { Class<?> translatorClass = ProgletCodeCompiler.class.getClassLoader() .loadClass("org.javascool.proglets." + this.proglet + ".Translator"); translator = (JVSTranslator) translatorClass.getDeclaredConstructor(File.class) .newInstance(jvsFile); } catch (ClassNotFoundException e) { Logger.getAnonymousLogger().log(Level.INFO, "Aucun Translator pour " + progletName); } // Dans ce cas on ne fais rien car il n'y a pas de Functions.java catch (Exception e) { throw new IllegalStateException("Impossible de crer un translator."); } } try { return new DefaultJVSTranslator(jvsFile); } catch (IOException e) { throw new IllegalStateException("Impossible de crer un translator sur le fichier ouvrir"); } }
From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFServiceTest.java
private Object buildRowIndexedLiteral(String language) { try {/*from w w w. jav a2 s .c o m*/ Class<?> clazz = Class.forName(RIL_CLASSNAME); Class<?>[] argTypes = { LanguageFilteringRDFService.class, Literal.class, Integer.TYPE }; Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes); constructor.setAccessible(true); Literal l = new LiteralStub(language); int i = literalIndex++; return constructor.newInstance(filteringRDFService, l, i); } catch (Exception e) { throw new RuntimeException("Could not create a row-indexed literal", e); } }
From source file:com.facebook.stetho.json.ObjectMapper.java
private <T> T _convertFromJSONObject(JSONObject jsonObject, Class<T> type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, JSONException { Constructor<T> constructor = type.getDeclaredConstructor((Class[]) null); constructor.setAccessible(true);//from w w w.j a v a2s . c o m T instance = constructor.newInstance(); Field[] fields = type.getFields(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; Object value = jsonObject.opt(field.getName()); Object setValue = getValueForField(field, value); try { field.set(instance, getValueForField(field, value)); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Class: " + type.getSimpleName() + " " + "Field: " + field.getName() + " type " + setValue.getClass().getName(), e); } } return instance; }
From source file:com.taobao.weex.devtools.json.ObjectMapper.java
private <T> T _convertFromJSONObject(JSONObject jsonObject, Class<T> type) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, JSONException { Constructor<T> constructor = type.getDeclaredConstructor((Class[]) null); constructor.setAccessible(true);//from w ww . j a va 2s . co m T instance = constructor.newInstance(); Field[] fields = type.getFields(); for (int i = 0; i < fields.length; ++i) { Field field = fields[i]; Object value = jsonObject.opt(field.getName()); Object setValue = getValueForField(field, value); try { field.set(instance, setValue); } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Class: " + type.getSimpleName() + " " + "Field: " + field.getName() + " type " + setValue.getClass().getName(), e); } } return instance; }
From source file:com.likya.myra.jef.utils.JobQueueOperations.java
public static JobImpl transformJobTypeToImpl(AbstractJobType abstractJobType) { String handlerUri = abstractJobType.getHandlerURI(); Class<?> abstractClass; try {// w ww.j a v a2 s . co m // abstractClass = Class.forName("com.likya.myra.jef.jobs.ExecuteInShell"); abstractClass = Class.forName(handlerUri); } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } // levelize the initial state according to trigger value Management management = abstractJobType.getManagement(); JobHelper.evaluateTriggerType(abstractJobType, false); // remove the difference between borned and planned time management.getTimeManagement().getJsPlannedTime() .setStartTime(management.getTimeManagement().getBornedPlannedTime().getStartTime()); management.getTimeManagement().getJsPlannedTime() .setStopTime(management.getTimeManagement().getBornedPlannedTime().getStopTime()); JobRuntimeInterface jobRuntimeInterface = new JobRuntimeProperties(); JobImpl jobImpl = null; try { jobImpl = (JobImpl) abstractClass .getDeclaredConstructor(new Class[] { AbstractJobType.class, JobRuntimeInterface.class }) .newInstance(abstractJobType, jobRuntimeInterface); jobImpl.getJobInfo(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } // switch (jobType) { // case JobCommandType.INT_BATCH_PROCESS: // jobImpl = new ExecuteInShell(simpleProperties, jobRuntimeInterface); // ((ExecuteInShell) jobImpl).setShell(true); // break; // case JobCommandType.INT_SYSTEM_COMMAND: // jobImpl = new ExecuteInShell(simpleProperties, jobRuntimeInterface); // ((ExecuteInShell) jobImpl).setShell(false); // break; // case JobCommandType.INT_REMOTE_SHELL: // jobImpl = new ExecuteInShell(simpleProperties, jobRuntimeInterface); // break; // // default: // break; // } return jobImpl; }
From source file:edu.isi.wings.catalog.component.api.impl.kb.ComponentCreationKB.java
public ComponentCreationKB(Properties props, boolean load_concrete) { super(props, load_concrete, true, false, true); String extern = props.getProperty("extern_component_catalog"); if (extern != null) { try {/* w ww. j ava 2 s . c o m*/ Class<?> classz = Class.forName(extern); ComponentCreationAPI externalCC = (ComponentCreationAPI) classz .getDeclaredConstructor(Properties.class).newInstance(props); this.setExternalCatalog(externalCC); } catch (Exception e) { e.printStackTrace(); } } }
From source file:org.apache.hadoop.mapreduce.approx.lib.input.SampleRecordReader.java
/** * A generic RecordReader that can hand out different recordReaders * for each chunk in the SampleFileSplit. *//*from www . java 2 s .co m*/ public SampleRecordReader(SampleFileSplit split, TaskAttemptContext context, Class<? extends RecordReader<K, V>> rrClass) throws IOException { this.split = split; this.context = context; this.rrClass = rrClass; this.idx = 0; this.curReader = null; this.progress = 0; try { rrConstructor = rrClass.getDeclaredConstructor(constructorSignature); rrConstructor.setAccessible(true); } catch (Exception e) { throw new RuntimeException(rrClass.getName() + " does not have valid constructor", e); } initNextRecordReader(); }