/** * Creates new instance of given type. In the first try, it tries to use * constructor with a {@link PetiteContainer}. If that files, uses default * constructor to builds an instance. */ private <T> T newInternalInstance(final Class<T> type, final PetiteContainer petiteContainer) throws Exception { T t = null; // first try ctor(PetiteContainer) try { Constructor<T> ctor = type.getConstructor(PetiteContainer.class); t = ctor.newInstance(petiteContainer); } catch (NoSuchMethodException nsmex) { // ignore } // if first try failed, try default ctor if (t == null) { return ClassUtil.newInstance(type); } return t; }
/** * Creates new entity instances. */ public <E> E createEntityInstance(final Class<E> type) { try { return ClassUtil.newInstance(type); } catch (Exception ex) { throw new DbOomException(ex); } }
/** * Creates new wrapper. */ protected <R extends T> R createWrapper(final Class<R> wrapperClass) { try { return ClassUtil.newInstance(wrapperClass); } catch (Exception ex) { throw new MadvocException("Invalid Madvoc wrapper: " + wrapperClass, ex); } }
/** * Creates new action object from {@link ActionRuntime} using default constructor. */ protected Object createAction(final Class actionClass) { try { return ClassUtil.newInstance(actionClass); } catch (Exception ex) { throw new MadvocException("Invalid Madvoc action", ex); } }
/** * Creates new {@link jodd.madvoc.result.ActionResult}. */ protected ActionResult createResult(final Class<? extends ActionResult> actionResultClass) { try { return ClassUtil.newInstance(actionResultClass); } catch (Exception ex) { throw new MadvocException("Invalid Madvoc result: " + actionResultClass, ex); } }
public MapperFunction lookup(final Class<? extends MapperFunction> mapperFunctionClass) { return typeCache.get(mapperFunctionClass, () -> { try { return ClassUtil.newInstance(mapperFunctionClass); } catch (Exception ex) { throw new IllegalArgumentException("Invalid mapper class " + mapperFunctionClass, ex); } }); }
/** * Creates a proxy of given target and the aspect. */ @SuppressWarnings("unchecked") public static <T> T proxyOf(final T target, final Class<? extends Aspect> aspectClass) { final Aspect aspect; try { aspect = ClassUtil.newInstance(aspectClass, target); } catch (Exception e) { throw new IllegalArgumentException("Can't create new instance of aspect class", e); } return (T) newProxyInstance(target.getClass().getClassLoader(), aspect, target.getClass().getInterfaces()); }
/** * Creates action method arguments. */ @SuppressWarnings({"unchecked", "NullArgumentToVariableArgMethod"}) protected Object createActionMethodArgument(final Class type, final Object action) { try { if (type.getEnclosingClass() == null || Modifier.isStatic(type.getModifiers())) { // regular or static class return ClassUtil.newInstance(type); } else { // member class Constructor ctor = type.getDeclaredConstructor(type.getDeclaringClass()); ctor.setAccessible(true); return ctor.newInstance(action); } } catch (Exception ex) { throw new MadvocException(ex); } }
/** * Registers pseudo class. */ public static void registerPseudoClass(final Class<? extends PseudoClass> pseudoClassType) { PseudoClass pseudoClass; try { pseudoClass = ClassUtil.newInstance(pseudoClassType); } catch (Exception ex) { throw new CSSellyException(ex); } PSEUDO_CLASS_MAP.put(pseudoClass.getPseudoClassName(), pseudoClass); }
/** * Registers pseudo function. */ public static void registerPseudoFunction(final Class<? extends PseudoFunction> pseudoFunctionType) { PseudoFunction pseudoFunction; try { pseudoFunction = ClassUtil.newInstance(pseudoFunctionType); } catch (Exception ex) { throw new CSSellyException(ex); } PSEUDO_FUNCTION_MAP.put(pseudoFunction.getPseudoFunctionName(), pseudoFunction); }
/** * Creates new instance of given class with given optional arguments. */ @SuppressWarnings("unchecked") public static <T> T newInstance(final Class<T> clazz, final Object... params) throws InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (params.length == 0) { return newInstance(clazz); } final Class<?>[] paramTypes = getClasses(params); final Constructor<?> constructor = findConstructor(clazz, paramTypes); if (constructor == null) { throw new InstantiationException("No constructor matched parameter types."); } return (T) constructor.newInstance(params); }
/** * Creates new instance of created class. * Assumes default no-arg constructor. */ public Object newInstance() { Class type = define(); try { return ClassUtil.newInstance(type); } catch (Exception ex) { throw new ProxettaException("Invalid Proxetta class", ex); } }
/** * Creates new <code>PathMacro</code> instance. */ private PathMacros createPathMacroInstance() { try { return ClassUtil.newInstance(actionsManager.getPathMacroClass()); } catch (Exception ex) { throw new MadvocException(ex); } }
/** * Returns sql type instance. Instances are stored for better performances. */ public SqlType lookupSqlType(final Class<? extends SqlType> sqlTypeClass) { SqlType sqlType = sqlTypes.get(sqlTypeClass); if (sqlType == null) { try { sqlType = ClassUtil.newInstance(sqlTypeClass); } catch (Exception ex) { throw new DbSqlException("SQL type not found: " + sqlTypeClass.getSimpleName(), ex); } sqlTypes.put(sqlTypeClass, sqlType); } return sqlType; }
/** * Alternative way for registering Joy listeners. * Sometimes servlet container does not allow adding new listener * from already added listener. This method therefore registers * the listener <i>before</i> container actually called the * callback methods. */ public static void registerInServletContext( final ServletContext servletContext, final Class<? extends JoyContextListener> joyContextListenerClass ) { try { final JoyContextListener joyContextListener = ClassUtil.newInstance(joyContextListenerClass); joyContextListener.createJoyAndInitServletContext(servletContext); } catch (Exception e) { throw new JoyException(e); } servletContext.addListener(joyContextListenerClass); }
/** * Creates {@link WebApp}. */ protected WebApp createWebApplication() { if ((webAppClassName == null) && (webAppClass == null)) { return new WebApp(); } final WebApp webApp; try { if (webAppClassName != null) { webAppClass = ClassLoaderUtil.loadClass(webAppClassName); } webApp = (WebApp) ClassUtil.newInstance(webAppClass); } catch (Exception ex) { throw new MadvocException("Unable to load Madvoc web application class: " + webAppClassName, ex); } return webApp; }
/** * Applies advice on given target class and returns proxy instance. */ public static <T> T applyAdvice(final Class<T> targetClass) { Class adviceClass = cache.get(targetClass); if (adviceClass == null) { // advice not yet created adviceClass = PROXY_PROXETTA.proxy().setTarget(targetClass).define(); cache.put(targetClass, adviceClass); } // create new advice instance and injects target instance to it try { Object advice = ClassUtil.newInstance(adviceClass); Field field = adviceClass.getField("$___target$0"); field.set(advice, targetClass); return (T) advice; } catch (Exception ex) { throw new ProxettaException(ex); } }
/** * Creates new instance for current property name through its setter. * It uses default constructor! */ protected Object createBeanProperty(final BeanProperty bp) { Setter setter = bp.getSetter(true); if (setter == null) { return null; } Class type = setter.getSetterRawType(); Object newInstance; try { newInstance = ClassUtil.newInstance(type); } catch (Exception ex) { if (isSilent) { return null; } throw new BeanException("Invalid property: " + bp.name, bp, ex); } newInstance = invokeSetter(setter, bp, newInstance); return newInstance; }
/** * Creates proxy object. */ protected C createProxyObject(Class<C> target) { target = ProxettaUtil.resolveTargetClass(target); Class proxyClass = cache.get(target); if (proxyClass == null) { proxyClass = proxetta.defineProxy(target); cache.put(target, proxyClass); } C proxy; try { proxy = (C) ClassUtil.newInstance(proxyClass); } catch (Exception ex) { throw new PathrefException(ex); } return proxy; }
/** * Parses action class and method and creates {@link ActionDefinition parsed action definition}. */ public ActionDefinition parseActionDefinition(final Class<?> actionClass, final Method actionMethod) { final ActionAnnotationValues annotationValues = detectActionAnnotationValues(actionMethod); final ActionConfig actionConfig = resolveActionConfig(annotationValues); final String[] packageActionNames = readPackageActionPath(actionClass); final String[] classActionNames = readClassActionPath(actionClass); final String[] methodActionNames = readMethodActionPath(actionMethod.getName(), annotationValues, actionConfig); final String method = readMethodHttpMethod(actionMethod); final ActionNames actionNames = new ActionNames(packageActionNames, classActionNames, methodActionNames, method); final ActionNamingStrategy namingStrategy; try { namingStrategy = ClassUtil.newInstance(actionConfig.getNamingStrategy()); contextInjectorComponent.injectContext(namingStrategy); } catch (Exception ex) { throw new MadvocException(ex); } return namingStrategy.buildActionDef(actionClass, actionMethod, actionNames); }