List of usage examples for java.lang.reflect Constructor getAnnotation
public <T extends Annotation> T getAnnotation(Class<T> annotationClass)
From source file:io.milton.config.HttpManagerBuilder.java
private Object createObject(Class c) throws CreationException { log.info("createObject: {}", c.getCanonicalName()); // Look for an @Inject or default constructor Constructor found = null;/*from w w w . j a va2 s . c o m*/ for (Constructor con : c.getConstructors()) { Annotation[][] paramTypes = con.getParameterAnnotations(); if (paramTypes != null && paramTypes.length > 0) { Annotation inject = con.getAnnotation(Inject.class); if (inject != null) { found = con; } } else { found = con; } } if (found == null) { throw new RuntimeException( "Could not find a default or @Inject constructor for class: " + c.getCanonicalName()); } Object args[] = new Object[found.getParameterTypes().length]; int i = 0; for (Class paramType : found.getParameterTypes()) { try { args[i++] = findOrCreateObject(paramType); } catch (CreationException ex) { throw new CreationException(c, ex); } } Object created; try { log.info("Creating: {}", c.getCanonicalName()); created = found.newInstance(args); rootContext.put(created); } catch (InstantiationException ex) { throw new CreationException(c, ex); } catch (IllegalAccessException ex) { throw new CreationException(c, ex); } catch (IllegalArgumentException ex) { throw new CreationException(c, ex); } catch (InvocationTargetException ex) { throw new CreationException(c, ex); } // Now look for @Inject fields for (Field field : c.getDeclaredFields()) { Inject anno = field.getAnnotation(Inject.class); if (anno != null) { boolean acc = field.isAccessible(); try { field.setAccessible(true); field.set(created, findOrCreateObject(field.getType())); } catch (IllegalArgumentException ex) { throw new CreationException(field, c, ex); } catch (IllegalAccessException ex) { throw new CreationException(field, c, ex); } finally { field.setAccessible(acc); // put back the way it was } } } // Finally set any @Inject methods for (Method m : c.getMethods()) { Inject anno = m.getAnnotation(Inject.class); if (anno != null) { Object[] methodArgs = new Object[m.getParameterTypes().length]; int ii = 0; try { for (Class<?> paramType : m.getParameterTypes()) { methodArgs[ii++] = findOrCreateObject(paramType); } m.invoke(created, methodArgs); } catch (CreationException creationException) { throw new CreationException(m, c, creationException); } catch (IllegalAccessException ex) { throw new CreationException(m, c, ex); } catch (IllegalArgumentException ex) { throw new CreationException(m, c, ex); } catch (InvocationTargetException ex) { throw new CreationException(m, c, ex); } } } if (created instanceof InitListener) { if (listeners == null) { listeners = new ArrayList<InitListener>(); } InitListener l = (InitListener) created; l.beforeInit(this); // better late then never!! listeners.add(l); } return created; }
From source file:org.jvnet.hudson.test.JenkinsRule.java
public Constructor<?> findDataBoundConstructor(Class<?> c) { for (Constructor<?> m : c.getConstructors()) { if (m.getAnnotation(DataBoundConstructor.class) != null) return m; }//from w w w . ja v a2 s .c o m return null; }