List of usage examples for java.lang Class getConstructors
@CallerSensitive public Constructor<?>[] getConstructors() throws SecurityException
From source file:org.loklak.api.aaa.UserAccountPermissions.java
@Override public JSONObject serviceImpl(Query query, HttpServletResponse response, Authorization authorization, final JSONObjectWithDefault permissions) throws APIException { JSONObject result = new JSONObject(); if (query.get("getServicePermissions", null) != null) { String serviceString = query.get("getServicePermissions", null); Class<?> serviceClass; try {/*from w w w. j av a2s . c o m*/ serviceClass = Class.forName(serviceString); } catch (ClassNotFoundException e) { throw new APIException(400, "Bad service name (no class)"); } Constructor<?> constructor; try { constructor = serviceClass.getConstructors()[0]; } catch (Throwable e) { throw new APIException(400, "Bad service name (no constructor)"); } Object service; try { service = constructor.newInstance(); } catch (Throwable e) { throw new APIException(400, "Bad service name (no instance possible)"); } if (service instanceof AbstractAPIHandler) { result.put("servicePermissions", authorization.getPermissions((AbstractAPIHandler) service)); return result; } else { throw new APIException(400, "Bad service name (no instance of AbstractAPIHandler)"); } } else if (query.get("getServiceList", false)) { JSONArray serviceList = new JSONArray(); for (Class<? extends Servlet> service : SusiServer.services) { serviceList.put(service.getCanonicalName()); } result.put("serviceList", serviceList); return result; } else if (query.get("getUserRolePermission", false)) { result.put("userRolePermissions", authorization.getUserRole().getPermissionOverrides()); return result; } else { result.put("userName", authorization.getIdentity().getName()); result.put("userSpecificPermissions", authorization.getPermissionOverrides()); result.put("userRole", authorization.getUserRole().getDisplayName()); result.put("userRoleSpecificPermissions", authorization.getUserRole().getPermissionOverrides()); result.put("parentUserRole", authorization.getUserRole().getParent()); return result; } }
From source file:org.loklak.api.cms.UserAccountPermissions.java
@Override public JSONObject serviceImpl(Query query, HttpServletResponse response, Authorization authorization, final JSONObjectWithDefault permissions) throws APIException { JSONObject result = new JSONObject(); if (query.get("getServicePermissions", null) != null) { String serviceString = query.get("getServicePermissions", null); Class<?> serviceClass; try {/*from w w w.jav a 2 s . c o m*/ serviceClass = Class.forName(serviceString); } catch (ClassNotFoundException e) { throw new APIException(400, "Bad service name (no class)"); } Constructor<?> constructor; try { constructor = serviceClass.getConstructors()[0]; } catch (Throwable e) { throw new APIException(400, "Bad service name (no constructor)"); } Object service; try { service = constructor.newInstance(); } catch (Throwable e) { throw new APIException(400, "Bad service name (no instance possible)"); } if (service instanceof AbstractAPIHandler) { result.put("servicePermissions", authorization.getPermissions((AbstractAPIHandler) service)); return result; } else { throw new APIException(400, "Bad service name (no instance of AbstractAPIHandler)"); } } else if (query.get("getServiceList", false)) { JSONArray serviceList = new JSONArray(); for (Class<? extends Servlet> service : LoklakServer.services) { serviceList.put(service.getCanonicalName()); } result.put("serviceList", serviceList); return result; } else if (query.get("getUserRolePermission", false)) { result.put("userRolePermissions", authorization.getUserRole().getPermissionOverrides()); return result; } else { result.put("userName", authorization.getIdentity().getName()); result.put("userSpecificPermissions", authorization.getPermissionOverrides()); result.put("userRole", authorization.getUserRole().getDisplayName()); result.put("userRoleSpecificPermissions", authorization.getUserRole().getPermissionOverrides()); result.put("parentUserRole", authorization.getUserRole().getParent()); return result; } }
From source file:ai.susi.server.api.aaa.UserAccountPermissions.java
@Override public JSONObject serviceImpl(Query query, HttpServletResponse response, Authorization authorization, final JsonObjectWithDefault permissions) throws APIException { JSONObject result = new JSONObject(); if (query.get("getServicePermissions", null) != null) { String serviceString = query.get("getServicePermissions", null); Class<?> serviceClass; try {//from www . ja v a 2 s .c o m serviceClass = Class.forName(serviceString); } catch (ClassNotFoundException e) { throw new APIException(400, "Bad service name (no class)"); } Constructor<?> constructor; try { constructor = serviceClass.getConstructors()[0]; } catch (Throwable e) { throw new APIException(400, "Bad service name (no constructor)"); } Object service; try { service = constructor.newInstance(); } catch (Throwable e) { throw new APIException(400, "Bad service name (no instance possible)"); } if (service instanceof AbstractAPIHandler) { result.put("servicePermissions", authorization.getPermissions((AbstractAPIHandler) service)); return result; } else { throw new APIException(400, "Bad service name (no instance of AbstractAPIHandler)"); } } else if (query.get("getServiceList", false)) { JSONArray serviceList = new JSONArray(); for (Class<? extends Servlet> service : SusiServer.services) { serviceList.put(service.getCanonicalName()); } result.put("serviceList", serviceList); return result; } else if (query.get("getUserRolePermission", false)) { result.put("userRolePermissions", authorization.getUserRole().getPermissionOverrides()); return result; } else { result.put("userName", authorization.getIdentity().getName()); result.put("userSpecificPermissions", authorization.getPermissionOverrides()); result.put("userRole", authorization.getUserRole().getDisplayName()); result.put("userRoleSpecificPermissions", authorization.getUserRole().getPermissionOverrides()); result.put("parentUserRole", authorization.getUserRole().getParent()); return result; } }
From source file:com.thinkbiganalytics.policy.BasePolicyAnnotationTransformer.java
private boolean hasConstructor(Class<?> policyClass) { return policyClass.getConstructors().length > 0; }
From source file:org.pentaho.osgi.legacy.LegacyPluginExtenderFactory.java
/** * Creates the object using a child-first classloader that uses local bundle bytecode first (so that it will be the * classloader for bundle classes created using this method which is necessary for correct loading of imports), the * bundleContext's classloader second, and the Kettle plugin's classloader third. * <p/>/*from ww w. j a va 2s.co m*/ * This allows bundles to implement interfaces known only to OSGi using classes only known to Ketle plugins (or vice * versa). * * @param className the classname to instantiate * @param arguments the arguments to use to instantiate the class * @return the instantiated class * @throws ClassNotFoundException * @throws InvocationTargetException * @throws InstantiationException * @throws KettlePluginException * @throws IllegalAccessException * @throws InterruptedException */ public Object create(String className, List<Object> arguments) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException { Class<?> clazz = Class.forName(className, true, legacyBridgingClassloader); if (arguments == null || arguments.size() == 0) { return clazz.newInstance(); } for (Constructor<?> constructor : clazz.getConstructors()) { Class<?>[] parameterTypes = constructor.getParameterTypes(); if (parameterTypes.length == arguments.size()) { boolean match = true; for (int i = 0; i < parameterTypes.length; i++) { Object o = arguments.get(i); if (o != null && !parameterTypes[i].isInstance(o)) { match = false; break; } } if (match) { return constructor.newInstance(arguments.toArray()); } } } throw new InstantiationException( "Unable to find constructor for class " + className + " with arguments " + arguments); }
From source file:org.xulux.utils.ClassLoaderUtils.java
/** * Tries to find a constructor with the parameters specified in the list * If it cannot it will return the empty constructor. * * @param clazz the class/* w w w .j a va2s .co m*/ * @param parms the list of parameters as classes * @return the instantiated object */ public static Object getObjectFromClass(Class clazz, List parms) { try { if (parms != null && parms.size() > 0) { boolean cleanUp = false; if (isInner(clazz) && !Modifier.isStatic(clazz.getModifiers())) { parms.add(0, getParentObjectForInnerClass(clazz)); cleanUp = true; } Class[] clzList = new Class[parms.size()]; for (int i = 0; i < parms.size(); i++) { clzList[i] = parms.get(i).getClass(); } try { Constructor constructor = clazz.getConstructor(clzList); // clean up list.. Object retValue = constructor.newInstance(parms.toArray()); if (cleanUp) { parms.remove(0); } return retValue; } catch (NoSuchMethodException nsme) { // we should check alternative constructors // eg new ObjectImpl(Object object) is an ok constructor // when there is a String parameter. Constructor[] constructors = clazz.getConstructors(); for (int c = 0; c < constructors.length; c++) { Constructor constructor = constructors[c]; Class[] cclz = constructor.getParameterTypes(); boolean cStatus = true; if (clzList.length >= cclz.length) { for (int cc = 0; cc < cclz.length; cc++) { if (!cclz[cc].isAssignableFrom(clzList[cc])) { cStatus = false; } } } else { cStatus = false; } if (cStatus) { Object retValue = null; if (parms.size() == constructor.getParameterTypes().length) { retValue = constructor.newInstance(parms.toArray()); if (cleanUp) { parms.remove(0); } return retValue; } } } if (cleanUp) { parms.remove(0); } return null; } } } catch (Exception e) { if (log.isWarnEnabled()) { log.warn("Unknown error in getting object", e); } } return getObjectFromClass(clazz); }
From source file:com.glaf.core.util.ReflectUtils.java
public static Constructor<?> findConstructor(Class<?> clazz, Class<?> paramType) throws NoSuchMethodException { Constructor<?> targetConstructor; try {/*from w w w.ja v a 2 s . c o m*/ targetConstructor = clazz.getConstructor(new Class<?>[] { paramType }); } catch (NoSuchMethodException e) { targetConstructor = null; Constructor<?>[] constructors = clazz.getConstructors(); for (Constructor<?> constructor : constructors) { if (Modifier.isPublic(constructor.getModifiers()) && constructor.getParameterTypes().length == 1 && constructor.getParameterTypes()[0].isAssignableFrom(paramType)) { targetConstructor = constructor; break; } } if (targetConstructor == null) { throw e; } } return targetConstructor; }
From source file:com.opengamma.language.external.ExternalFunctionHandler.java
/** * Creates a handler wrapper for a given class. * /*from ww w . j a va 2s. c o m*/ * @param clazz the class containing external function methods */ public ExternalFunctionHandler(final Class<?> clazz) { final Constructor<?>[] constructors = clazz.getConstructors(); final Method[] methods = clazz.getMethods(); final ArrayList<PublishedFunction> functions = new ArrayList<PublishedFunction>( constructors.length + methods.length); // Only need an instance of the class if one or more annotated methods are not static. In this case, the same // instance will be re-used for each non-static method. If instantiation fails (e.g. no default constructor), just // skip instance methods and log warnings. Object sharedInstance = null; boolean instantiateFailed = false; for (Constructor<?> constructor : constructors) { if (!constructor.isAnnotationPresent(ExternalFunction.class)) { continue; } s_logger.debug("Found constructor {}", constructor); // If there is a constructor method, can only have static declarations instantiateFailed = true; functions.add(new ConstructorWrapper(constructor)); } for (Method method : methods) { if (!method.isAnnotationPresent(ExternalFunction.class)) { continue; } s_logger.debug("Found method {}", method); final Object instance; if (Modifier.isStatic(method.getModifiers())) { instance = null; } else { if (instantiateFailed) { s_logger.warn("Skipping method {}", method); continue; } else if (sharedInstance == null) { sharedInstance = tryGetInstance(clazz); if (sharedInstance == null) { s_logger.warn("Default instantiation failed for {}", clazz); s_logger.warn("Skipping method {}", method); instantiateFailed = true; continue; } } instance = sharedInstance; } functions.add(new MethodWrapper(method, instance)); } functions.trimToSize(); _functions = functions; }
From source file:org.opendaylight.controller.cluster.datastore.DatastoreContextIntrospector.java
/** * Finds the appropriate constructor for the specified type that we will use to construct * instances./*www . ja va 2s. c o m*/ */ private static void processPropertyType(Class<?> propertyType) throws Exception { Class<?> wrappedType = Primitives.wrap(propertyType); if (constructors.containsKey(wrappedType)) { return; } // If the type is a primitive (or String type), we look for the constructor that takes a // single String argument, which, for primitives, validates and converts from a String // representation which is the form we get on ingress. if (propertyType.isPrimitive() || Primitives.isWrapperType(propertyType) || propertyType.equals(String.class)) { constructors.put(wrappedType, propertyType.getConstructor(String.class)); } else { // This must be a yang-defined type. We need to find the constructor that takes a // primitive as the only argument. This will be used to construct instances to perform // validation (eg range checking). The yang-generated types have a couple single-argument // constructors but the one we want has the bean ConstructorProperties annotation. for (Constructor<?> ctor : propertyType.getConstructors()) { ConstructorProperties ctorPropsAnnotation = ctor.getAnnotation(ConstructorProperties.class); if (ctor.getParameterTypes().length == 1 && ctorPropsAnnotation != null) { findYangTypeGetter(propertyType, ctorPropsAnnotation.value()[0]); constructors.put(propertyType, ctor); break; } } } }
From source file:eu.sathra.io.IO.java
private SerializeInfo getSerializeInfo(Class<?> clazz) { if (!mCache.containsKey(clazz)) { SerializeInfo myInfo = new SerializeInfo(); // Find annotated constructor for (Constructor<?> myConstructor : clazz.getConstructors()) { Deserialize myDeserialize = myConstructor.getAnnotation(Deserialize.class); myInfo.types = myConstructor.getParameterTypes(); if (myDeserialize != null) { myInfo.constructor = myConstructor; Defaults myDefaults = myConstructor.getAnnotation(Defaults.class); myInfo.params = myDeserialize.value(); if (myDefaults != null) myInfo.defaults = myDefaults.value(); mCache.put(clazz, myInfo); break; }/*from w ww . ja v a 2s. c om*/ } } return mCache.get(clazz); }