List of usage examples for java.lang.reflect Constructor getParameterAnnotations
@Override
public Annotation[][] getParameterAnnotations()
From source file:org.fornax.cartridges.sculptor.smartclient.server.ScServlet.java
private Object makeNewInstance(Class expectedClass, HashMap<String, Object> jsData) throws ApplicationException { Object val = null; try {//from w ww .j av a 2s .c om Constructor[] constructors = expectedClass.getConstructors(); for (int k = 0; k < constructors.length; k++) { Constructor constructor = constructors[k]; Annotation[][] constrAnnot = constructor.getParameterAnnotations(); String[] paramNames = new String[constrAnnot.length]; int annotCount = 0; for (int i = 0; i < constrAnnot.length; i++) { Annotation[] annotations = constrAnnot[i]; for (int j = 0; j < annotations.length; j++) { Annotation annotation = annotations[j]; if (annotation instanceof Name) { paramNames[i] = ((Name) annotation).value(); paramNames[i] = paramNames[i].startsWith("xxx") ? paramNames[i].substring(3) : paramNames[i]; annotCount++; break; } } } if (annotCount != constrAnnot.length) { continue; } Object[] params = prepareMethodParam(jsData, false, paramNames, constructor.getParameterTypes()); if (params != null) { val = constructor.newInstance(params); break; } } if (val == null) { throw new Exception("Can't create instance of class " + expectedClass.getName()); } } catch (Exception ex) { throw new ApplicationException(ex.getMessage(), "ERR9001", ex); } return val; }
From source file:org.jgentleframework.utils.Utils.java
/** * Creates the instance object from the given {@link Constructor}. * //from ww w .jav a 2s .c o m * @param provider * the current {@link Provider} * @param constructor * the given {@link Constructor} * @return Object */ public static Object createInstanceFromInjectedConstructor(Provider provider, Constructor<?> constructor) { Object result = null; // Khi to v inject dependency cho parameter Object[] args = new Object[constructor.getParameterTypes().length]; for (int i = 0; i < constructor.getParameterAnnotations().length; i++) { Map<Class<? extends Annotation>, Annotation> annoList = new HashMap<Class<? extends Annotation>, Annotation>(); List<Class<? extends Annotation>> clazzlist = new ArrayList<Class<? extends Annotation>>(); for (Annotation anno : constructor.getParameterAnnotations()[i]) { annoList.put(anno.annotationType(), anno); clazzlist.add(anno.annotationType()); } if (!clazzlist.contains(Inject.class)) { args[i] = null; } else { args[i] = InOutExecutor.getInjectedDependency((Inject) annoList.get(Inject.class), constructor.getParameterTypes()[i], provider); } } try { constructor.setAccessible(true); result = constructor.newInstance(args); } catch (Exception e) { if (log.isFatalEnabled()) { log.fatal("Could not new instance on this constructor [" + constructor + "]", e); } } return result; }
From source file:org.movealong.junitfu.ConstructorMockBinder.java
private static Collection<? extends MockBinder> createMockBinders(Constructor<?> constructor) { if (constructor.getAnnotation(Inject.class) != null) { HashSet<MockBinder> mockBinders = new HashSet<MockBinder>(); for (int parameterIndex = 0; parameterIndex < constructor .getParameterTypes().length; parameterIndex++) { for (Annotation annotation : constructor.getParameterAnnotations()[parameterIndex]) { if (annotation.annotationType() == Mock.class) { Class<?> mockClass = constructor.getParameterTypes()[parameterIndex]; String mockName = defaultIfEmpty(((Mock) annotation).value(), uncapitalize(mockClass.getSimpleName())); mockBinders.add(new SimpleMockBinder(mockClass, mockName)); }// w w w . ja va2 s. c om } } return mockBinders; } else { return Collections.emptyList(); } }
From source file:org.openehr.build.RMObjectBuilder.java
private Map<String, Class> attributeType(Class rmClass) { Map<String, Class> map = new HashMap<String, Class>(); Constructor constructor = fullConstructor(rmClass); if (constructor == null) { throw new IllegalArgumentException("no annotated constructor of " + rmClass + ">"); }/*from w w w . ja v a 2 s . c om*/ Annotation[][] annotations = constructor.getParameterAnnotations(); Class[] types = constructor.getParameterTypes(); if (annotations.length != types.length) { throw new IllegalArgumentException("less annotations"); } for (int i = 0; i < types.length; i++) { if (annotations[i].length == 0) { throw new IllegalArgumentException("missing annotations of attribute " + i); } Attribute attribute = (Attribute) annotations[i][0]; map.put(attribute.name(), types[i]); } return map; }
From source file:org.openehr.build.RMObjectBuilder.java
/** * Return a map with name as the key and index of position as the value for * all parameters of the full constructor in the RMObject * /*from w w w .ja v a 2s . co m*/ * @param rmClass * @return */ private Map<String, Integer> attributeIndex(Class rmClass) { Map<String, Integer> map = new HashMap<String, Integer>(); Constructor constructor = fullConstructor(rmClass); Annotation[][] annotations = constructor.getParameterAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].length == 0) { throw new IllegalArgumentException("missing annotation at position " + i); } Attribute attribute = (Attribute) annotations[i][0]; map.put(attribute.name(), i); } return map; }
From source file:org.openehr.build.RMObjectBuilder.java
/** * Return a map with name as the key and index of position as the value for * all parameters of the full constructor in the RMObject * /*w ww .jav a2 s .co m*/ * @param rmClass * @return */ private Map<String, Attribute> attributeMap(Class rmClass) { Map<String, Attribute> map = new HashMap<String, Attribute>(); Constructor constructor = fullConstructor(rmClass); Annotation[][] annotations = constructor.getParameterAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].length == 0) { throw new IllegalArgumentException("missing annotation at position " + i); } Attribute attribute = (Attribute) annotations[i][0]; map.put(attribute.name(), attribute); } return map; }
From source file:org.openehr.build.RMObjectBuilder.java
/** * Finds the matching RM class that can be used to create RM object for * given value map//from ww w. j a v a 2 s . co m * * @param valueMap * @return null if no match RM class is found */ public String findMatchingRMClass(Map<String, Object> valueMap) { List simpleTypes = Arrays.asList(SKIPPED_TYPES_IN_MATCHING); for (Class rmClass : typeMap.values()) { log.debug("matching rmClass: " + rmClass.getName()); if (simpleTypes.contains(rmClass.getSimpleName())) { continue; // skip simple value types } // replace underscore separated names with camel case Map<String, Object> filteredMap = new HashMap<String, Object>(); for (String name : valueMap.keySet()) { filteredMap.put(toCamelCase(name), valueMap.get(name)); } Constructor constructor = fullConstructor(rmClass); if (constructor == null) { throw new RuntimeException("annotated constructor missing for " + rmClass); } Annotation[][] annotations = constructor.getParameterAnnotations(); if (annotations == null || annotations.length == 0) { throw new RuntimeException("attribute annotations missing for " + rmClass); } Class[] types = constructor.getParameterTypes(); boolean matched = true; Set<String> attributes = new HashSet<String>(); for (int i = 0; i < types.length; i++) { if (annotations[i].length == 0) { throw new RuntimeException("attribute annotation missing for" + rmClass); } Attribute attribute = (Attribute) annotations[i][0]; attributes.add(attribute.name()); log.debug("checking attribute: " + attribute.name()); String attrName = attribute.name(); Object attrValue = filteredMap.get(attrName); if (attribute.required() && attrValue == null) { log.debug("missing required attribute.."); matched = false; break; } else if (attrValue != null) { if (((attrValue instanceof Boolean) && types[i] != boolean.class) || ((attrValue instanceof Integer) && types[i] != Integer.class) || ((attrValue instanceof Double) && types[i] != double.class)) { log.debug("wrong primitive value type for attribute.."); matched = false; break; } else if (!types[i].isPrimitive() && !types[i].isInstance(attrValue)) { log.debug("wrong value type for attribute.."); matched = false; break; } } } for (String attr : filteredMap.keySet()) { if (!attributes.contains(attr)) { log.debug("unknown attribute: " + attr); matched = false; } } // matching found if (matched) { String className = rmClass.getSimpleName(); log.debug(">>> MATCHING FOUND: " + className); return className; } } return null; }
From source file:org.openehr.rm.binding.DADLBinding.java
/** * Return a map with name as the key and index of position as the value for * all parameters of the full constructor in the RMObject * //from w w w .ja va 2 s. c o m * @param rmClass * @return */ private SortedMap<String, Attribute> attributeMap(Class rmClass) { SortedMap<String, Attribute> map = new TreeMap<String, Attribute>(); Constructor constructor = fullConstructor(rmClass); if (constructor == null) { throw new IllegalArgumentException("Unknown RM Class: " + rmClass.getClass().getCanonicalName()); } Annotation[][] annotations = constructor.getParameterAnnotations(); for (int i = 0; i < annotations.length; i++) { if (annotations[i].length == 0) { throw new IllegalArgumentException("missing annotation at position " + i); } Attribute attribute = (Attribute) annotations[i][0]; map.put(attribute.name(), attribute); } return map; }
From source file:org.restlet.ext.jaxrs.internal.wrappers.params.ParameterList.java
/** * @param constr//from www.j a v a2 s . c om * @param tlContext * @param leaveEncoded * @param jaxRsProviders * @param extensionBackwardMapping * @param paramsAllowed * @param logger * @param allMustBeAvailable * @throws MissingAnnotationException * @throws IllegalTypeException * if one of the parameters contains a @{@link Context} on * an type that must not be annotated with @{@link Context}. * @throws IllegalPathParamTypeException */ public ParameterList(Constructor<?> constr, ThreadLocalizedContext tlContext, boolean leaveEncoded, JaxRsProviders jaxRsProviders, ExtensionBackwardMapping extensionBackwardMapping, boolean paramsAllowed, Logger logger, boolean allMustBeAvailable) throws MissingAnnotationException, IllegalTypeException, IllegalPathParamTypeException { this(constr.getParameterTypes(), constr.getGenericParameterTypes(), constr.getParameterAnnotations(), tlContext, leaveEncoded, jaxRsProviders, extensionBackwardMapping, paramsAllowed, false, logger, allMustBeAvailable); }
From source file:org.unitils.core.context.Context.java
@SuppressWarnings("unchecked") protected <T> T createInstanceOfType(Class<T> type, String... classifiers) { Class<?> implementationType = getImplementationType(type, classifiers); Constructor<?> constructor = getConstructor(implementationType); Object[] arguments;/*from ww w .ja v a 2 s .com*/ Class<?>[] parameterTypes; if (constructor == null) { arguments = new Object[0]; parameterTypes = new Class[0]; } else { parameterTypes = constructor.getParameterTypes(); Type[] genericParameterTypes = constructor.getGenericParameterTypes(); Annotation[][] argumentAnnotations = constructor.getParameterAnnotations(); arguments = new Object[parameterTypes.length]; for (int i = 0; i < parameterTypes.length; i++) { arguments[i] = getArgumentInstance(parameterTypes[i], genericParameterTypes[i], argumentAnnotations[i]); } } Object instance = ReflectionUtils.createInstanceOfType(implementationType, true, parameterTypes, arguments); if (instance instanceof Factory) { instance = ((Factory) instance).create(); } if (!type.isAssignableFrom(instance.getClass())) { throw new UnitilsException( "Implementation type " + instance.getClass().getName() + " is not of type " + type.getName()); } return (T) instance; }