package jfun.yan.xml.nuts;
import java.lang.reflect.Constructor;
import jfun.yan.Component;
import jfun.yan.Components;
import jfun.yan.Functions;
import jfun.yan.util.ReflectionUtil;
/**
* Nut class for <ctor> tag.
* <p>
* @author Ben Yu
* Nov 9, 2005 11:41:35 PM
*/
public class ConstructorNut extends ArgumentsAndPropertiesNut
implements LifecycleDeclaration{
private Class type;
private boolean private_access = false;
public void setClass(Class type){
this.type = type;
}
/**
* Get the class that declares the constructor.
*/
public Class getDeclaringClass(){
return type;
}
public boolean isPrivate_access() {
return private_access;
}
public void setPrivate_access(boolean private_access) {
this.private_access = private_access;
}
/**
* Evaluate the Component without applying life cycle.
* Subclass can call this method and apply customized lifecycle.
*/
protected Component evaluateNoLifecycle(){
checkMandatory("class", type);
final Constructor ctor = lookupConstructor();
Component result = Components.fun(Functions.ctor(ctor));
return decorateComponent(result);
}
private Constructor lookupConstructor(){
final Class[] param_types = getParameterTypes();
if(param_types==null){
final int argcount = getMaxArgsCount();
if(argcount<0){
//no arg is specified, find any.
if(this.getParameterAutowireMode()==null){
//manual
return ReflectionUtil.getConstructor(type, null, private_access);
}
else return ReflectionUtil.getConstructor(type,
private_access);
}
else{
return ReflectionUtil.getConstructor(type, argcount,
private_access);
}
}
else{
return ReflectionUtil.getConstructor(type, param_types, private_access);
}
}
public Component eval(){
return Util.wrapLifecycle(evaluateNoLifecycle(), this);
}
private String initializer;
private String starter;
private String stopper;
private String disposer;
public String getInitializer() {
return initializer;
}
public void setInitializer(String initializer) {
this.initializer = initializer;
}
public String getStarter() {
return starter;
}
public void setStarter(String starter) {
this.starter = starter;
}
public String getStopper() {
return stopper;
}
public void setStopper(String stopper) {
this.stopper = stopper;
}
public String getDisposer() {
return disposer;
}
public void setDisposer(String disposer) {
this.disposer = disposer;
}
}