List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:ml.shifu.shifu.util.ClassUtils.java
public static <T> T newInstance(Class<T> clazz, Class<?>[] parameterClasses, Object[] parameters) { T result;// w w w . jav a2 s . c o m try { @SuppressWarnings("unchecked") Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(clazz); if (meth == null) { meth = clazz.getDeclaredConstructor(parameterClasses); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(clazz, meth); } result = meth.newInstance(parameters); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.jpmml.sparkml.ConverterUtil.java
static public <T extends Transformer> TransformerConverter<T> createConverter(T transformer) { Class<? extends Transformer> clazz = transformer.getClass(); Class<? extends TransformerConverter> converterClazz = getConverterClazz(clazz); if (converterClazz == null) { throw new IllegalArgumentException("Transformer class " + clazz.getName() + " is not supported"); }//w w w .j ava2 s . co m try { Constructor<?> constructor = converterClazz.getDeclaredConstructor(clazz); return (TransformerConverter) constructor.newInstance(transformer); } catch (Exception e) { throw new IllegalArgumentException(e); } }
From source file:nl.nn.adapterframework.util.ClassUtils.java
/** * Retrieves the constructor of a class, based on the parameters * **//*from w w w.j a va2 s . c om*/ public static Constructor getConstructorOnType(Class clas, Class[] parameterTypes) { Constructor theConstructor = null; try { theConstructor = clas.getDeclaredConstructor(parameterTypes); } catch (java.lang.NoSuchMethodException e) { log.error("cannot create constructor for Class [" + clas.getName() + "]", e); for (int i = 0; i < parameterTypes.length; i++) log.error("Parameter " + i + " type " + parameterTypes[i].getName()); } return theConstructor; }
From source file:org.opoo.util.ClassUtils.java
public static Constructor getDefaultConstructor(Class clazz) throws SecurityException, NoSuchMethodException { if (isAbstractClass(clazz)) return null; Constructor constructor = clazz.getDeclaredConstructor(NO_CLASSES); if (!isPublic(clazz, constructor)) { constructor.setAccessible(true); }/*from w ww . j av a 2 s .c o m*/ return constructor; }
From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java
private static Permission parsePermission(AuthorizationTokenStream ats) throws AuthorizationSyntaxParseException, IOException { String perm = ats.nextUsefulToken(); if (perm == null) { throw new AuthorizationSyntaxParseException("Line " + ats.getLineNum() + ", permission syntax error"); } else if (!perm.toLowerCase().equals("permission")) { String rightBracket = perm; if (rightBracket == null || !rightBracket.contains("}")) { throw new AuthorizationSyntaxParseException("Line " + ats.getLineNum() + ", no right bracket"); } else if (!rightBracket.contains(";")) { throw new AuthorizationSyntaxParseException("Line " + ats.getLineNum() + ", no \";\" sign finded"); }/*from w w w .j a v a 2 s .com*/ return null; } String className = ats.nextUsefulToken(); String isEnd = ats.nextUsefulToken(); if (className == null) { throw new AuthorizationSyntaxParseException("Line " + ats.getLineNum() + ", className is null"); } if (isEnd == null) { throw new AuthorizationSyntaxParseException("Line " + ats.getLineNum() + ", no operate object defined"); } else { try { if (!isEnd.contains(";")) { String oper = isEnd; oper = oper.replace("\"", ""); oper = oper.replace(",", ""); isEnd = ats.nextUsefulToken(); if (isEnd != null && isEnd.contains(";")) { String actions = isEnd.replace(";", ""); actions = actions.replace("\"", ""); Class<?> clazz = Class.forName(className, false, VWBPermission.class.getClassLoader()); return ((Permission) clazz .getDeclaredConstructor(new Class[] { String.class, String.class }) .newInstance(oper, actions)); } else { throw new AuthorizationSyntaxParseException( "Line " + ats.getLineNum() + ", no \";\" sign finded"); } } else { String oper = isEnd.replace(";", ""); oper = oper.replace("\"", ""); Class<?> clazz = Class.forName(className); return ((Permission) clazz.getDeclaredConstructor(String.class).newInstance(oper)); } } catch (ClassNotFoundException e) { throw new AuthorizationSyntaxParseException( "Line " + ats.getLineNum() + ", ClassNotFoundException, " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); throw new AuthorizationSyntaxParseException( "Line " + ats.getLineNum() + ", Exception happens, " + e.getMessage()); } } }
From source file:com.fer.hr.olap.util.ObjectUtil.java
private static SaikuLevel convert(Level level) { Checker c = new Checker(); try {/*ww w. java 2 s. c o m*/ try { Class.forName("mondrian.olap4j.MondrianOlap4jLevelExtend"); //Class.forName("bi.meteorite.CheckClass"); Class<LevelInterface> _tempClass = (Class<LevelInterface>) Class .forName("mondrian.olap4j.MondrianOlap4jLevelExtend"); if (c.checker(level)) { Constructor<LevelInterface> ctor = _tempClass .getDeclaredConstructor(org.olap4j.metadata.Level.class); LevelInterface test = ctor.newInstance(level); HashMap<String, String> m = null; if (test.getAnnotations() != null) { m = new HashMap<>(); for (Map.Entry<String, Annotation> entry : test.getAnnotations().entrySet()) { m.put(entry.getKey(), (String) entry.getValue().getValue()); } } return new SaikuLevel(test.getName(), test.getUniqueName(), test.getCaption(), test.getDescription(), test.getDimension().getUniqueName(), test.getHierarchy().getUniqueName(), test.isVisible(), test.getLevelType().toString(), m); } else { return new SaikuLevel(level.getName(), level.getUniqueName(), level.getCaption(), level.getDescription(), level.getDimension().getUniqueName(), level.getHierarchy().getUniqueName(), level.isVisible(), null, null); } } catch (ClassNotFoundException e) { return new SaikuLevel(level.getName(), level.getUniqueName(), level.getCaption(), level.getDescription(), level.getDimension().getUniqueName(), level.getHierarchy().getUniqueName(), level.isVisible(), null, null); } } catch (Exception e) { throw new SaikuServiceException("Cannot convert level: " + level, e); } }
From source file:org.saiku.olap.util.ObjectUtil.java
@NotNull private static SaikuLevel convert(@NotNull Level level) { Checker c = new Checker(); try {// w ww .ja v a 2s . co m try { Class.forName("mondrian.olap4j.MondrianOlap4jLevelExtend"); //Class.forName("bi.meteorite.CheckClass"); Class<LevelInterface> _tempClass = (Class<LevelInterface>) Class .forName("mondrian.olap4j.MondrianOlap4jLevelExtend"); if (c.checker(level)) { Constructor<LevelInterface> ctor = _tempClass .getDeclaredConstructor(org.olap4j.metadata.Level.class); LevelInterface test = ctor.newInstance(level); HashMap<String, String> m = null; if (test.getAnnotations() != null) { m = new HashMap<>(); for (Map.Entry<String, Annotation> entry : test.getAnnotations().entrySet()) { m.put(entry.getKey(), (String) entry.getValue().getValue()); } } return new SaikuLevel(test.getName(), test.getUniqueName(), test.getCaption(), test.getDescription(), test.getDimension().getUniqueName(), test.getHierarchy().getUniqueName(), test.isVisible(), test.getLevelType().toString(), m); } else { return new SaikuLevel(level.getName(), level.getUniqueName(), level.getCaption(), level.getDescription(), level.getDimension().getUniqueName(), level.getHierarchy().getUniqueName(), level.isVisible(), null, null); } } catch (ClassNotFoundException e) { return new SaikuLevel(level.getName(), level.getUniqueName(), level.getCaption(), level.getDescription(), level.getDimension().getUniqueName(), level.getHierarchy().getUniqueName(), level.isVisible(), null, null); } } catch (Exception e) { throw new SaikuServiceException("Cannot convert level: " + level, e); } }
From source file:org.apache.hama.util.ReflectionUtils.java
/** * Create an instance with corresponded class and object values supplied. * Constructor// w w w. j a v a 2 s . c om * * @param theClass supplies instance to be created. * @param values are parameters applied when instance is created. */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T newInstance(Class<T> theClass, Object[] values) { T result; try { Constructor<T> meth = (Constructor<T>) CONSTRUCTOR_CACHE.get(theClass); if (null == meth) { Class[] parameters = new Class[values.length]; int idx = 0; for (Object value : values) { parameters[idx++] = value.getClass(); } meth = theClass.getDeclaredConstructor(parameters); meth.setAccessible(true); CONSTRUCTOR_CACHE.put(theClass, meth); } result = meth.newInstance(values); } catch (Exception e) { throw new RuntimeException(e); } return result; }
From source file:org.cobaltians.cobalt.font.CobaltFontManager.java
/** * Initializes and returns a font drawable with a font icon identifier, color, text size and padding * @param context the activity context//from ww w .ja va 2 s. c o m * @param identifier the font icon identifier as "font-key font-icon" (i.e.: fa fa-mobile) * @param color the text color as a color-int * @return a Drawable or null */ //* @param textSize the text size in sp //* @param padding the padding in dp public static CobaltAbstractFontDrawable getCobaltFontDrawable(Context context, String identifier, int color) { mContext = context; if (identifier != null) { if (identifier.contains(" ")) { String[] splitIdentifier = identifier.split(" "); String fontName = splitIdentifier[0]; Class<? extends CobaltAbstractFontDrawable> fontClass = getFonts().get(fontName); if (fontClass != null) { try { Class[] argsClass = new Class[] { Context.class, String.class, int.class }; Object[] arrayArgs = new Object[] { context, splitIdentifier[1], color }; Constructor fontConstructor = fontClass.getDeclaredConstructor(argsClass); try { return (CobaltAbstractFontDrawable) fontConstructor.newInstance(arrayArgs); } catch (InstantiationException e) { if (Cobalt.DEBUG) Log.e(TAG, "- getCobaltFontDrawable: exception in Instantiation of fontClass"); e.printStackTrace(); } catch (IllegalAccessException e) { if (Cobalt.DEBUG) Log.e(TAG, "- getCobaltFontDrawable"); e.printStackTrace(); } catch (InvocationTargetException e) { if (Cobalt.DEBUG) Log.e(TAG, "- getCobaltFontDrawable"); e.printStackTrace(); } } catch (NoSuchMethodException e) { e.printStackTrace(); } } else if (Cobalt.DEBUG) Log.e(TAG, "- getCobaltFontDrawable: no font class found for name " + fontName + "."); } else if (Cobalt.DEBUG) Log.e(TAG, TAG + " - getCobaltFontDrawable : no space separate in identifier"); } else if (Cobalt.DEBUG) Log.e(TAG, TAG + " - getCobaltFontDrawable: identifier for icon is null"); return null; }
From source file:org.projectforge.common.BeanHelper.java
public static Object newInstance(final Class<?> clazz) { Constructor<?> constructor = null; try {//ww w. j a v a2 s. co m constructor = clazz.getDeclaredConstructor(new Class[0]); } catch (final SecurityException ex) { logInstantiationException(ex, clazz); } catch (final NoSuchMethodException ex) { logInstantiationException(ex, clazz); } if (constructor == null) { try { return clazz.newInstance(); } catch (final InstantiationException ex) { logInstantiationException(ex, clazz); } catch (final IllegalAccessException ex) { logInstantiationException(ex, clazz); } return null; } constructor.setAccessible(true); try { return constructor.newInstance(); } catch (final IllegalArgumentException ex) { logInstantiationException(ex, clazz); } catch (final InstantiationException ex) { logInstantiationException(ex, clazz); } catch (final IllegalAccessException ex) { logInstantiationException(ex, clazz); } catch (final InvocationTargetException ex) { logInstantiationException(ex, clazz); } return null; }