List of usage examples for java.lang.reflect Constructor setAccessible
@Override @CallerSensitive public void setAccessible(boolean flag)
A SecurityException is also thrown if this object is a Constructor object for the class Class and flag is true.
From source file:com.adf.bean.AbsBean.java
/** * IMPORT// w w w.j av a 2 s . c om * */ public void setValueByColumn(String col, String val) throws IllegalArgumentException { try { Field f = getClass().getDeclaredField(col); Class<?> cls = f.getType(); f.setAccessible(true); if (cls.isArray()) { JSONArray arr; try { arr = new JSONArray(val); setArrayColumn(col, arr); } catch (JSONException e) { } return; } if ((int.class == cls) || (Integer.class == cls)) { int ival = Integer.parseInt(val); f.set(this, ival); } else if ((long.class == cls) || (Long.class == cls)) { long lval = Long.parseLong(val); f.set(this, lval); } else if ((float.class == cls) || (Float.class == cls)) { float fval = Float.parseFloat(val); f.set(this, fval); } else if ((short.class == cls) || (Short.class == cls)) { short sval = Short.parseShort(val); f.set(this, sval); } else if ((double.class == cls) || (Double.class == cls)) { double dval = Double.parseDouble(val); f.set(this, dval); } else if ((byte.class == cls) || (Byte.class == cls)) { byte bval = Byte.parseByte(val); f.set(this, bval); } else if ((boolean.class == cls) || (Boolean.class == cls)) { boolean bval = Boolean.parseBoolean(val); f.set(this, bval); } else if (char.class == cls) { char cval = val.charAt(0); f.set(this, cval); } else { Constructor<?> cons = cls.getDeclaredConstructor(String.class); cons.setAccessible(true); Object obj = cons.newInstance(val); f.set(this, obj); } } catch (NoSuchFieldException e) { LogUtil.err("setValueByColumn NoSuchFieldException, col " + col + " not exist!!!"); } catch (IllegalAccessException e) { LogUtil.err("setValueByColumn IllegalAccessException, col " + col + " :" + e.getMessage()); //throw e; } catch (IllegalArgumentException e) { LogUtil.err("setValueByColumn IllegalArgumentException, col " + col + " :" + e.getMessage()); //throw e; } catch (NoSuchMethodException e) { LogUtil.err("setValueByColumn NoSuchMethodException, col " + col + " :" + e.getMessage()); //throw e; } catch (InstantiationException e) { LogUtil.err("setValueByColumn InstantiationException, col " + col + " :" + e.getMessage()); //throw e; } catch (InvocationTargetException e) { LogUtil.err("setValueByColumn InvocationTargetException, col " + col + " :" + e.getMessage()); //throw e; } }
From source file:org.silverpeas.core.test.extention.SilverTestEnv.java
private void mockManagedThreadFactory() { try {/*w ww . j a va2 s . c o m*/ Constructor<ManagedThreadPool> managedThreadPoolConstructor = ManagedThreadPool.class .getDeclaredConstructor(); managedThreadPoolConstructor.setAccessible(true); ManagedThreadPool managedThreadPool = managedThreadPoolConstructor.newInstance(); ManagedThreadFactory managedThreadFactory = Thread::new; FieldUtils.writeField(managedThreadPool, "managedThreadFactory", managedThreadFactory, true); when(TestBeanContainer.getMockedBeanContainer().getBeanByType(ManagedThreadPool.class)) .thenReturn(managedThreadPool); } catch (IllegalAccessException | NoSuchMethodException | InstantiationException | InvocationTargetException e) { throw new SilverpeasRuntimeException(e); } }
From source file:com.msopentech.odatajclient.engine.communication.request.ODataRequestImpl.java
/** * Gets an empty response that can be initialized by a stream. * <p>//from w w w.jav a2 s . com * This method has to be used to build response items about a batch request. * * @param <V> ODataResppnse type. * @return empty OData response instance. */ @SuppressWarnings("unchecked") public <V extends ODataResponse> V getResponseTemplate() { for (Class<?> clazz : this.getClass().getDeclaredClasses()) { if (ODataResponse.class.isAssignableFrom(clazz)) { try { final Constructor<?> constructor = clazz.getDeclaredConstructor(this.getClass()); constructor.setAccessible(true); return (V) constructor.newInstance(this); } catch (Exception e) { LOG.error("Error retrieving response class template instance", e); } } } throw new IllegalStateException("No response class template has been found"); }
From source file:org.apache.hadoop.hbase.index.covered.CoveredColumnsIndexBuilder.java
@Override public void setup(RegionCoprocessorEnvironment env) throws IOException { this.env = env; // setup the phoenix codec. Generally, this will just be in standard one, but abstracting here // so we can use it later when generalizing covered indexes Configuration conf = env.getConfiguration(); Class<? extends IndexCodec> codecClass = conf.getClass(CODEC_CLASS_NAME_KEY, null, IndexCodec.class); try {/*from ww w.j a va2 s . c om*/ Constructor<? extends IndexCodec> meth = codecClass.getDeclaredConstructor(new Class[0]); meth.setAccessible(true); this.codec = meth.newInstance(); this.codec.initialize(env); } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e); } this.localTable = new LocalTable(env); }
From source file:org.optaplanner.core.impl.domain.solution.cloner.FieldAccessingSolutionCloner.java
protected <C> Constructor<C> retrieveCachedConstructor(Class<C> clazz) throws NoSuchMethodException { Constructor<C> constructor = constructorCache.get(clazz); if (constructor == null) { constructor = clazz.getDeclaredConstructor(); constructor.setAccessible(true); constructorCache.put(clazz, constructor); }//from w w w .jav a2 s. c o m return constructor; }
From source file:com.novartis.opensource.yada.format.AbstractResponse.java
/** * This method returns the appropriate converter {@link Converter} by looking first at the {@link YADARequest#PS_CONVERTER} * parmater, if no class is set, or the class provided cannot be instantiated, the default converter is set * using {@link #getDefaultConverter(YADAQueryResult)} * @param yqr the result container/*from ww w . j a va2 s . c o m*/ * @return the appropriate Converter object * @throws YADARequestException when the default converter cannot be instantiated * @throws YADAConverterException when result reformatting fails * @throws YADAQueryConfigurationException */ protected Converter getConverter(YADAQueryResult yqr) throws YADARequestException, YADAConverterException, YADAQueryConfigurationException { String converterClass = yqr.getYADAQueryParamValue(YADARequest.PS_CONVERTER); Converter converter; if (converterClass != null && !"".equals(converterClass)) { Constructor<?> c; Class<?> clazz; try { clazz = Class.forName(converterClass); c = clazz.getConstructor(YADAQueryResult.class); c.setAccessible(true); converter = (Converter) c.newInstance(yqr); } catch (Exception e) { l.warn("The specified class [" + converterClass + "] could not be instantiated. Trying FQCN."); try { converterClass = FORMAT_PKG + converterClass; clazz = Class.forName(converterClass); c = clazz.getConstructor(YADAQueryResult.class); c.setAccessible(true); converter = (Converter) c.newInstance(yqr); } catch (Exception e1) { l.warn("The specified class [" + converterClass + "] could not be instantiated. Trying default classes.", e1); converter = getDefaultConverter(yqr); } } } else { converter = getDefaultConverter(yqr); } return converter; }
From source file:org.jgentleframework.utils.Utils.java
/** * Creates the instance object from the given {@link Constructor}. * //from w w w . j av a2 s . c om * @param provider * the current {@link Provider} * @param constructor * the given {@link Constructor} * @return Object */ public static Object createInstanceFromInjectedConstructor(Provider provider, Constructor<?> constructor) { Object result = null; // Khi to v inject dependency cho parameter Object[] args = new Object[constructor.getParameterTypes().length]; for (int i = 0; i < constructor.getParameterAnnotations().length; i++) { Map<Class<? extends Annotation>, Annotation> annoList = new HashMap<Class<? extends Annotation>, Annotation>(); List<Class<? extends Annotation>> clazzlist = new ArrayList<Class<? extends Annotation>>(); for (Annotation anno : constructor.getParameterAnnotations()[i]) { annoList.put(anno.annotationType(), anno); clazzlist.add(anno.annotationType()); } if (!clazzlist.contains(Inject.class)) { args[i] = null; } else { args[i] = InOutExecutor.getInjectedDependency((Inject) annoList.get(Inject.class), constructor.getParameterTypes()[i], provider); } } try { constructor.setAccessible(true); result = constructor.newInstance(args); } catch (Exception e) { if (log.isFatalEnabled()) { log.fatal("Could not new instance on this constructor [" + constructor + "]", e); } } return result; }
From source file:com.adf.bean.AbsBean.java
/** * IMPORT//w w w. j av a 2s . co m * */ private void setArrayColumn(String col, JSONArray arr) throws NoSuchFieldException, IllegalAccessException, IllegalArgumentException { Field f; f = getClass().getDeclaredField(col); f.setAccessible(true); Class<?> arrCls = f.getType(); Class<?> objCls = getArrayObjectClass(arrCls); if (objCls != null) { Object array = Array.newInstance(objCls, arr.length()); for (int i = 0; i < arr.length(); i++) { Object oi = arr.opt(i); if (oi.getClass() == objCls) { Array.set(array, i, arr.opt(i)); } else { Constructor<?> cons; try { cons = objCls.getDeclaredConstructor(String.class); cons.setAccessible(true); Object obj = cons.newInstance(oi.toString()); Array.set(array, i, obj); } catch (NoSuchMethodException e) { LogUtil.err("setArrayColumn NoSuchMethodException, col " + col + " :" + e.getMessage()); } catch (InstantiationException e) { LogUtil.err("setArrayColumn InstantiationException, col " + col + " :" + e.getMessage()); } catch (InvocationTargetException e) { LogUtil.err("setArrayColumn InvocationTargetException, col " + col + " :" + e.getMessage()); } } } f.set(this, array); } else { throw new IllegalArgumentException("Can not get Array Column Object class"); } }
From source file:org.atemsource.atem.impl.common.AbstractEntityType.java
@Override public J createEntity() throws TechnicalException { try {/*from w w w .java 2 s . com*/ final Constructor constructor = entityClass.getDeclaredConstructor(new Class[0]); constructor.setAccessible(true); return (J) constructor.newInstance(new Object[0]); } catch (InstantiationException e) { throw new TechnicalException("cannot create entity", e); } catch (IllegalAccessException e) { throw new TechnicalException("cannot create entity", e); } catch (SecurityException e) { throw new TechnicalException("cannot create entity", e); } catch (NoSuchMethodException e) { throw new TechnicalException("cannot create entity", e); } catch (IllegalArgumentException e) { throw new TechnicalException("cannot create entity", e); } catch (InvocationTargetException e) { throw new TechnicalException("cannot create entity", e); } }
From source file:org.eclipse.hudson.init.InitialSetup.java
public void invokeHudson(boolean restart) { final WebAppController controller = WebAppController.get(); if (initialClassLoader == null) { initialClassLoader = getClass().getClassLoader(); }/*from w w w . j ava 2 s . co m*/ Class hudsonIsLoadingClass; try { outerClassLoader = new OuterClassLoader(initialClassLoader); hudsonIsLoadingClass = outerClassLoader.loadClass("hudson.util.HudsonIsLoading"); HudsonIsLoading hudsonIsLoading = (HudsonIsLoading) hudsonIsLoadingClass.newInstance(); Class runnableClass = outerClassLoader.loadClass("org.eclipse.hudson.init.InitialRunnable"); Constructor ctor = runnableClass.getDeclaredConstructors()[0]; ctor.setAccessible(true); InitialRunnable initialRunnable = (InitialRunnable) ctor.newInstance(controller, logger, hudsonHomeDir, servletContext, restart); controller.install(hudsonIsLoading); Thread initThread = new Thread(initialRunnable, "hudson initialization thread " + (++highInitThreadNumber)); initThread.setContextClassLoader(outerClassLoader); initThread.start(); } catch (Exception ex) { logger.error("Hudson failed to load!!!", ex); } /** Above replaces these lines controller.install(new HudsonIsLoading()); new Thread("hudson initialization thread") { }.start(); */ }