List of usage examples for java.lang.reflect Constructor getParameterTypes
@Override
public Class<?>[] getParameterTypes()
From source file:com.github.hateoas.forms.spring.SpringActionDescriptor.java
static List<ActionParameterType> findConstructorInfo(final Class<?> beanType) { List<ActionParameterType> parametersInfo = new ArrayList<ActionParameterType>(); Constructor<?>[] constructors = beanType.getConstructors(); // find default ctor Constructor<?> constructor = PropertyUtils.findDefaultCtor(constructors); // find ctor with JsonCreator ann if (constructor == null) { constructor = PropertyUtils.findJsonCreator(constructors, JsonCreator.class); }//w w w. j a v a 2 s .c o m if (constructor != null) { int parameterCount = constructor.getParameterTypes().length; if (parameterCount > 0) { Annotation[][] annotationsOnParameters = constructor.getParameterAnnotations(); Class<?>[] parameters = constructor.getParameterTypes(); int paramIndex = 0; for (Annotation[] annotationsOnParameter : annotationsOnParameters) { for (Annotation annotation : annotationsOnParameter) { if (JsonProperty.class == annotation.annotationType()) { JsonProperty jsonProperty = (JsonProperty) annotation; // TODO use required attribute of JsonProperty for required fields -> String paramName = jsonProperty.value(); MethodParameter methodParameter = new MethodParameter(constructor, paramIndex); parametersInfo.add(new MethodParameterType(paramName, methodParameter)); paramIndex++; // increase for each @JsonProperty } } } Assert.isTrue(parameters.length == paramIndex, "not all constructor arguments of @JsonCreator " + constructor.getName() + " are annotated with @JsonProperty"); } } return parametersInfo; }
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//from w ww . j a va 2 s .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:org.force66.beantester.valuegens.TemporalValueGenerator.java
public TemporalValueGenerator(Class<?> type) { this.temporalType = type; Constructor<?>[] allConstructors = type.getDeclaredConstructors(); for (Constructor<?> ctor : allConstructors) { if (ctor.getParameterTypes().length == 1 && ctor.getParameterTypes()[0].getName().equals("long")) { longConstructor = ctor;/*from w ww. j a v a2 s . c o m*/ } } Validate.notNull(longConstructor, "Not a temporal object with a long constructor. class=" + type.getName()); }
From source file:com.opengamma.masterdb.security.SecurityTestCase.java
private static <T> Constructor<T> getDefaultConstructor(final Class<T> clazz) { Constructor<T> defaultConstructor = null; Constructor<?>[] declaredConstructors = clazz.getDeclaredConstructors(); for (Constructor<?> constructor : declaredConstructors) { Class<?>[] parameterTypes = constructor.getParameterTypes(); if (parameterTypes.length == 0) { defaultConstructor = (Constructor<T>) constructor; break; }/* w ww.j ava 2 s .c o m*/ } return defaultConstructor; }
From source file:com.google.dexmaker.ProxyBuilder.java
private static <T, G extends T> void generateConstructorsAndFields(DexMaker dexMaker, TypeId<G> generatedType, TypeId<T> superType, Class<T> superClass) { TypeId<InvocationHandler> handlerType = TypeId.get(InvocationHandler.class); TypeId<Method[]> methodArrayType = TypeId.get(Method[].class); FieldId<G, InvocationHandler> handlerField = generatedType.getField(handlerType, FIELD_NAME_HANDLER); dexMaker.declare(handlerField, PRIVATE, null); FieldId<G, Method[]> allMethods = generatedType.getField(methodArrayType, FIELD_NAME_METHODS); dexMaker.declare(allMethods, PRIVATE | STATIC, null); for (Constructor<T> constructor : getConstructorsToOverwrite(superClass)) { if (constructor.getModifiers() == Modifier.FINAL) { continue; }//from ww w .j a va2s . c o m TypeId<?>[] types = classArrayToTypeArray(constructor.getParameterTypes()); MethodId<?, ?> method = generatedType.getConstructor(types); Code constructorCode = dexMaker.declare(method, PUBLIC); Local<G> thisRef = constructorCode.getThis(generatedType); Local<?>[] params = new Local[types.length]; for (int i = 0; i < params.length; ++i) { params[i] = constructorCode.getParameter(i, types[i]); } MethodId<T, ?> superConstructor = superType.getConstructor(types); constructorCode.invokeDirect(superConstructor, null, thisRef, params); constructorCode.returnVoid(); } }
From source file:org.xmlsh.util.JavaUtils.java
public static int hasBeanConstructor(Class<?> targetClass, Class<?> sourceClass) throws InvalidArgumentException { Constructor<?> c = getBeanConstructor(targetClass, sourceClass); if (c == null) return -1; if (c.getParameterTypes()[0].isAssignableFrom(sourceClass)) return 3; else/*from w ww. j a v a2 s .co m*/ return 4; }
From source file:com.swtxml.swt.metadata.WidgetRegistry.java
@SuppressWarnings("unchecked") public Constructor getWidgetConstructor(Class<? extends Widget> widgetClass) { return CollectionUtils.find(Arrays.asList(widgetClass.getConstructors()), new IFilter<Constructor>() { public boolean match(Constructor constructor) { return (constructor.getParameterTypes().length == 2 && constructor.getParameterTypes()[1] == Integer.TYPE); }// www. j a va 2s. c om }); }
From source file:org.pircbotx.hooks.events.MassEventTest.java
@Test(dependsOnMethods = "singleConstructorTest", dataProvider = "eventObjectDataProvider", dataProviderClass = TestUtils.class, description = "Verify event has a single constructor") public void firstConstructorArgBotTest(Class<?> event) { Constructor constructor = event.getDeclaredConstructors()[0]; assertEquals(constructor.getParameterTypes()[0], PircBotX.class, wrapClass(event, "First parameter of constructor isn't of PircBotX type")); }
From source file:dk.netarkivet.archive.webinterface.BatchGUI.java
/** * Finds a constructor which either does not take any arguments, or does only takes string arguments. Any * constructor which does has other arguments than string is ignored. * * @param c The class to retrieve the constructor from. * @return The string argument based constructor of the class. * @throws UnknownID If no valid constructor can be found. *//* w w w. ja v a2s . co m*/ @SuppressWarnings("rawtypes") private static Constructor findStringConstructor(Class c) throws UnknownID { for (Constructor con : c.getConstructors()) { boolean valid = true; // validate the parameter classes. Ignore if not string. for (Class cl : con.getParameterTypes()) { if (!cl.equals(java.lang.String.class)) { valid = false; break; } } if (valid) { return con; } } // throw an exception if no valid constructor can be found. throw new UnknownID("No valid constructor can be found for class '" + c.getName() + "'."); }
From source file:com.swtxml.swt.metadata.WidgetRegistry.java
public Class<?> getAllowedParentType(Class<? extends Widget> type) { Constructor<?> constructor = getWidgetConstructor(type); return constructor.getParameterTypes()[0]; }