List of usage examples for java.lang Class getConstructors
@CallerSensitive public Constructor<?>[] getConstructors() throws SecurityException
From source file:org.devproof.portal.core.module.common.page.TemplatePage.java
private Component createGenericBoxInstance(Class<? extends Component> boxClazz) { for (Constructor<?> constr : boxClazz.getConstructors()) { Class<?> param[] = constr.getParameterTypes(); if (param.length == 2 && param[0] == String.class && param[1] == PageParameters.class) { try { return (Component) constr.newInstance(getBoxId(), params); } catch (Exception e) { throw new UnhandledException(e); }/*w w w.ja va 2 s. co m*/ } else if (param.length == 1 && param[0] == String.class) { try { return (Component) constr.newInstance(getBoxId()); } catch (Exception e) { throw new UnhandledException(e); } } } return null; }
From source file:org.apache.hama.ml.perception.SmallMultiLayerPerceptron.java
@SuppressWarnings("rawtypes") @Override//from w ww .java 2 s .com public void readFields(DataInput input) throws IOException { this.MLPType = WritableUtils.readString(input); this.learningRate = input.readDouble(); this.regularization = input.readDouble(); this.momentum = input.readDouble(); this.numberOfLayers = input.readInt(); this.squashingFunctionName = WritableUtils.readString(input); this.costFunctionName = WritableUtils.readString(input); this.squashingFunction = FunctionFactory.createDoubleFunction(this.squashingFunctionName); this.costFunction = FunctionFactory.createDoubleDoubleFunction(this.costFunctionName); // read the number of neurons for each layer this.layerSizeArray = new int[this.numberOfLayers]; for (int i = 0; i < numberOfLayers; ++i) { this.layerSizeArray[i] = input.readInt(); } this.weightMatrice = new DenseDoubleMatrix[this.numberOfLayers - 1]; for (int i = 0; i < numberOfLayers - 1; ++i) { this.weightMatrice[i] = (DenseDoubleMatrix) MatrixWritable.read(input); } // read feature transformer int bytesLen = input.readInt(); byte[] featureTransformerBytes = new byte[bytesLen]; for (int i = 0; i < featureTransformerBytes.length; ++i) { featureTransformerBytes[i] = input.readByte(); } Class featureTransformerCls = (Class) SerializationUtils.deserialize(featureTransformerBytes); Constructor constructor = featureTransformerCls.getConstructors()[0]; try { this.featureTransformer = (FeatureTransformer) constructor.newInstance(new Object[] {}); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } }
From source file:com.thinkbiganalytics.policy.BasePolicyAnnotationTransformer.java
private P createClass(BaseUiPolicyRule rule) throws ClassNotFoundException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { P domainPolicy = null;/*w ww .j a v a 2 s . c o m*/ String classType = rule.getObjectClassType(); Class<P> domainPolicyClass = (Class<P>) Class.forName(classType); Constructor constructor = null; Object[] paramValues = null; boolean hasConstructor = false; for (Constructor con : domainPolicyClass.getConstructors()) { hasConstructor = true; int parameterSize = con.getParameterTypes().length; paramValues = new Object[parameterSize]; for (int p = 0; p < parameterSize; p++) { Annotation[] annotations = con.getParameterAnnotations()[p]; Object paramValue = null; for (Annotation a : annotations) { if (a instanceof PolicyPropertyRef) { // this is the one we want if (constructor == null) { constructor = con; } //find the value associated to this property paramValue = getPropertyValue(rule, domainPolicyClass, (PolicyPropertyRef) a); } } paramValues[p] = paramValue; } if (constructor != null) { //exit once we find a constructor with @PropertyRef break; } } if (constructor != null) { //call that constructor try { domainPolicy = ConstructorUtils.invokeConstructor(domainPolicyClass, paramValues); } catch (NoSuchMethodException e) { domainPolicy = domainPolicyClass.newInstance(); } } else { //if the class has no public constructor then attempt to call the static instance method if (!hasConstructor) { //if the class has a static "instance" method on it then call that try { domainPolicy = (P) MethodUtils.invokeStaticMethod(domainPolicyClass, "instance", null); } catch (NoSuchMethodException | SecurityException | InvocationTargetException e) { domainPolicy = domainPolicyClass.newInstance(); } } else { //attempt to create a new instance domainPolicy = domainPolicyClass.newInstance(); } } return domainPolicy; }
From source file:ClassReader.java
protected final Member resolveMethod(int index) throws IOException, ClassNotFoundException, NoSuchMethodException { int oldPos = pos; try {/*from w w w . j av a 2 s. c om*/ Member m = (Member) cpool[index]; if (m == null) { pos = cpoolIndex[index]; Class owner = resolveClass(readShort()); NameAndType nt = resolveNameAndType(readShort()); String signature = nt.name + nt.type; if ("<init>".equals(nt.name)) { Constructor[] ctors = owner.getConstructors(); for (int i = 0; i < ctors.length; i++) { String sig = getSignature(ctors[i], ctors[i].getParameterTypes()); if (sig.equals(signature)) { cpool[index] = ctors[i]; m = ctors[i]; return m; } } } else { Method[] methods = owner.getDeclaredMethods(); for (int i = 0; i < methods.length; i++) { String sig = getSignature(methods[i], methods[i].getParameterTypes()); if (sig.equals(signature)) { cpool[index] = methods[i]; m = methods[i]; return m; } } } throw new NoSuchMethodException(signature); } return m; } finally { pos = oldPos; } }
From source file:org.amanzi.neo.providers.context.ProviderContextImpl.java
@SuppressWarnings("unchecked") protected <T> T createInstance(final Class<? extends T> clazz, final Map<Class<? extends Object>, Object> parametersMap) throws InvocationTargetException, IllegalAccessException, InstantiationException, ContextException { Constructor<?> correctConstructor = null; Object[] arguments = null;//from w ww. j a v a 2 s . c o m for (Constructor<?> constructor : clazz.getConstructors()) { Class<?>[] parameterTypes = constructor.getParameterTypes(); arguments = new Object[parameterTypes.length]; int i = 0; if (parameterTypes.length == parametersMap.size()) { boolean isFound = false; for (Class<?> argumentType : parameterTypes) { for (Entry<Class<? extends Object>, Object> parameterEntry : parametersMap.entrySet()) { Class<?> parameterClass = parameterEntry.getKey(); if (argumentType.isAssignableFrom(parameterClass)) { isFound = true; arguments[i++] = parameterEntry.getValue(); break; } } if (!isFound) { break; } } if (isFound) { correctConstructor = constructor; } } } if (correctConstructor == null) { throw new ContextException("Unable to create <" + clazz.getSimpleName() + ">. Constructor for Parameters <" + parametersMap.keySet() + "> not found."); } return (T) correctConstructor.newInstance(arguments); }
From source file:org.onecmdb.core.utils.ClassInjector.java
public Object toBeanObject(ICi ci) { Class cl = getClassForCi(ci); if (cl == null) { log.info("No class with alias " + ci.getAlias() + " found!"); return (null); /*/*www. ja v a 2 s . com*/ throw new IllegalArgumentException("No class found for alias <" + alias + ">"); */ } Object instance = null; Constructor constructors[] = cl.getConstructors(); // Check if it support constructor with ICi. for (Constructor con : constructors) { Class conPar[] = con.getParameterTypes(); if (conPar.length == 1) { // Check if we can create with ICi as constructor. if (conPar[0].isAssignableFrom(ICi.class)) { try { instance = con.newInstance(new Object[] { ci }); } catch (Throwable e) { log.error(cl.getSimpleName() + ".newInstance(ICi) error with alias '" + ci.getAlias() + "' class '" + cl.getName() + "' : " + e.toString()); } break; } } } // Default constructor. if (instance == null) { try { instance = cl.newInstance(); } catch (Throwable e) { log.error("Instacation exception with alias '" + ci.getAlias() + "' class '" + cl.getName() + "' : " + e.toString()); return (null); } } // Start to inject attributes injectAttributes(instance, ci); return (instance); }
From source file:org.kuali.kpme.tklm.leave.accrual.bucket.KPMEAccrualCategoryBucket.java
public void initialize(PrincipalHRAttributesBo currentPrincipalCalendar) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { leaveBalances = new LinkedHashMap<String, List<LeaveBalance>>(); principalCalendar = currentPrincipalCalendar; asOfDate = LocalDate.now();//from ww w . j a v a 2 s.com List<AccrualCategory> accrualCategories = HrServiceLocator.getAccrualCategoryService() .getActiveAccrualCategoriesForLeavePlan(currentPrincipalCalendar.getLeavePlan(), asOfDate); for (AccrualCategory accrualCategory : accrualCategories) { List<LeaveBalance> leaveBalances = new ArrayList<LeaveBalance>(); for (Class<LeaveBalance> leaveBalanceClazz : baseBalanceList) { Constructor<?>[] constructors = leaveBalanceClazz.getConstructors(); //does this array contain default constructors as well?? Object[] args = new Object[2]; args[0] = accrualCategory; args[1] = principalCalendar; Constructor<?> constructor = constructors[0]; LeaveBalance myLeaveBalance = (LeaveBalance) constructor.newInstance(args); leaveBalances.add(myLeaveBalance); } this.leaveBalances.put(accrualCategory.getLmAccrualCategoryId(), leaveBalances); } //This list *could* contain leave blocks for accrual categories that have been deactivated, those from another leave plan, etc List<LeaveBlock> leaveBlocks = LmServiceLocator.getLeaveBlockService().getLeaveBlocksSinceCarryOver( currentPrincipalCalendar.getPrincipalId(), LmServiceLocator.getLeaveBlockService() .getLastCarryOverBlocks(currentPrincipalCalendar.getPrincipalId(), asOfDate), LocalDate.now().plusDays(365), true); //These maps could also contain such accrual categories within their keyset. Map<String, LeaveBlock> carryOverBlocks = LmServiceLocator.getLeaveBlockService() .getLastCarryOverBlocks(currentPrincipalCalendar.getPrincipalId(), asOfDate); Map<String, List<LeaveBlock>> accrualCategoryMappedLeaveBlocks = mapLeaveBlocksByAccrualCategory( leaveBlocks); //merge carryOverBlock map with accrualCategoryMappedLeaveBlocks. for (Entry<String, LeaveBlock> entry : carryOverBlocks.entrySet()) { if (accrualCategoryMappedLeaveBlocks.containsKey(entry.getKey())) accrualCategoryMappedLeaveBlocks.get(entry.getKey()).add(entry.getValue()); else { List<LeaveBlock> carryOverList = new ArrayList<LeaveBlock>(); carryOverList.add(entry.getValue()); accrualCategoryMappedLeaveBlocks.put(entry.getKey(), carryOverList); } } //add leave blocks, accrual category by accrual category, to bucket. for (Entry<String, List<LeaveBlock>> entry : accrualCategoryMappedLeaveBlocks.entrySet()) { for (LeaveBlock leaveBlock : entry.getValue()) { try { addLeaveBlock(leaveBlock); } catch (KPMEBalanceException e) { InstantiationException ie = new InstantiationException(); ie.setStackTrace(e.getStackTrace()); throw ie; } } } }
From source file:org.pentaho.platform.dataaccess.client.ConnectionServiceClient.java
/** * Returns an object for the specified class. * This is in the ObjectSupplier interface used by * the BeanUtil deserialize methods./*from ww w .j a v a 2 s .c o m*/ */ public Object getObject(Class clazz) throws AxisFault { try { System.out.println(clazz.getCanonicalName()); Constructor[] cons = clazz.getConstructors(); if (cons.length > 0) { return clazz.newInstance(); } return null; } catch (IllegalAccessException e) { throw new AxisFault(e.getLocalizedMessage(), e); } catch (InstantiationException e) { throw new AxisFault(e.getLocalizedMessage(), e); } }
From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java
/** * @return the {@link Constructor} with minimal number of parameters. *//* ww w. j a v a 2 s .com*/ public static Constructor<?> getShortestConstructor(Class<?> clazz) { Constructor<?> shortest = null; int minCount = Integer.MAX_VALUE; for (Constructor<?> constructor : clazz.getConstructors()) { int thisCount = constructor.getParameterTypes().length; if (minCount > thisCount) { shortest = constructor; minCount = thisCount; } } return shortest; }
From source file:org.evosuite.testcarver.testcase.EvoTestCaseCodeGenerator.java
private boolean hasDefaultConstructor(final Class<?> clazz) { for (final Constructor<?> c : clazz.getConstructors()) { if (c.getParameterTypes().length == 0 && Modifier.isPublic(c.getModifiers())) { return true; }//from w ww. jav a 2 s . c o m } return false; }