ConstructorMetadata constructorMetadata = new ConstructorMetadata(); List<Injectable> formalParameters = new ArrayList<Injectable>(); Class<?> resourceClass = metadata.getResourceClass(); constructorMetadata.setEncoded(constructor.getAnnotation(Encoded.class) != null); parameterAnnotations[pos], constructor, getMetadata().isEncoded() || constructorMetadata.isEncoded(), null); if ((constructorMetadata.getConstructor() == null) || (constructorMetadata .getFormalParameters().size() < paramTypes.length)) { constructorMetadata.setConstructor(constructor); constructorMetadata.getFormalParameters().clear(); constructorMetadata.getFormalParameters().addAll(formalParameters); } else if (constructorMetadata.getFormalParameters().size() == paramTypes.length) {
/** * creates object (StaticResource or Provider) based on its ClassMetadata * * @param metadata * @param runtimeContext * @return created object */ static Object createObject(ClassMetadata metadata, RuntimeContext runtimeContext) { try { // use constructor to create a prototype ConstructorMetadata constructorMetadata = metadata.getConstructor(); Constructor<?> constructor = constructorMetadata.getConstructor(); List<Injectable> formalParameters = constructorMetadata.getFormalParameters(); Object[] params = InjectableFactory.getInstance().instantiate(formalParameters, runtimeContext); Object object = constructor.newInstance(params); injectFields(object, metadata, runtimeContext); return object; } catch (RuntimeException e) { throw e; } catch (InvocationTargetException e) { Throwable targetException = e.getTargetException(); if (targetException instanceof RuntimeException) { throw (RuntimeException)targetException; } throw new ObjectCreationException(targetException); } catch (Exception e) { throw new ObjectCreationException(e); } }
protected static <T> ClassMetadata collectClassMetadata(final Class<T> cls, boolean validateConstructor) { ClassMetadata classMetadata = null; if (ProviderMetadataCollector.isProvider(cls)) { classMetadata = ProviderMetadataCollector.collectMetadata(cls); } else if (ResourceMetadataCollector.isResource(cls)) { classMetadata = ResourceMetadataCollector.collectMetadata(cls); } else if (ApplicationMetadataCollector.isApplication(cls)) { classMetadata = ApplicationMetadataCollector.collectMetadata(cls); } else { throw new IllegalArgumentException(Messages.getMessage("cannotCreateFactoryForClass", cls)); //$NON-NLS-1$ } // validate that there is a valid constructor if needed if (validateConstructor && classMetadata.getConstructor().getConstructor() == null) { throw new IllegalStateException(Messages.getMessage("noValidConstructorFoundFor", cls //$NON-NLS-1$ .getCanonicalName())); } return classMetadata; }