webit.script.resolvers.ResolverManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webit-script Show documentation
Show all versions of webit-script Show documentation
a template-like script and engine, all writen with Java.
The newest version!
// Copyright (c) 2013-2014, Webit Team. All Rights Reserved.
package webit.script.resolvers;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import webit.script.Engine;
import webit.script.Initable;
import webit.script.exceptions.ScriptRuntimeException;
import webit.script.loggers.Logger;
import webit.script.resolvers.impl.CommonResolver;
import webit.script.util.ClassEntry;
import webit.script.util.ClassMap;
/**
*
* @author Zqq
*/
public class ResolverManager implements Initable {
protected Logger logger;
public final ClassMap getters;
public final ClassMap setters;
public final ClassMap outters;
protected final ArrayList getResolvers;
protected final ArrayList setResolvers;
protected final ArrayList outResolvers;
protected final ArrayList getResolverTypes;
protected final ArrayList setResolverTypes;
protected final ArrayList outResolverTypes;
protected final CommonResolver commonResolver;
//settings
protected boolean ignoreNullPointer;
protected ClassEntry[] resolvers;
public ResolverManager() {
this.ignoreNullPointer = true;
getters = new ClassMap();
setters = new ClassMap();
outters = new ClassMap();
getResolvers = new ArrayList();
setResolvers = new ArrayList();
outResolvers = new ArrayList();
getResolverTypes = new ArrayList();
setResolverTypes = new ArrayList();
outResolverTypes = new ArrayList();
commonResolver = new CommonResolver();
}
@SuppressWarnings("unchecked")
protected GetResolver resolveGetResolverIfAbsent(final Class type) {
GetResolver resolver = getters.get(type);
if (resolver != null) {
return resolver;
}
final ArrayList types = getResolverTypes;
int i = types.size();
while (i > 0) {
--i;
if (types.get(i).isAssignableFrom(type)) {
resolver = getResolvers.get(i);
break;
}
}
if (resolver == null) {
resolver = resolveGetResolver(type);
}
return getters.putIfAbsent(type, resolver);
}
protected GetResolver resolveGetResolver(final Class type) {
return commonResolver;
}
@SuppressWarnings("unchecked")
protected SetResolver resolveSetResolverIfAbsent(final Class type) {
SetResolver resolver;
resolver = setters.get(type);
if (resolver != null) {
return resolver;
}
final ArrayList types = setResolverTypes;
int i = types.size();
while (i > 0) {
--i;
if (types.get(i).isAssignableFrom(type)) {
resolver = setResolvers.get(i);
break;
}
}
if (resolver == null) {
resolver = resolveSetResolver(type);
}
return setters.putIfAbsent(type, resolver);
}
protected SetResolver resolveSetResolver(final Class type) {
return commonResolver;
}
@SuppressWarnings("unchecked")
public OutResolver resolveOutResolver(final Class type) {
OutResolver resolver;
resolver = outters.get(type);
if (resolver != null) {
return resolver;
}
final ArrayList types = outResolverTypes;
int i = types.size();
while (i > 0) {
--i;
if (types.get(i).isAssignableFrom(type)) {
resolver = outResolvers.get(i);
break;
}
}
if (resolver == null) {
resolver = commonResolver;
}
return outters.putIfAbsent(type, resolver);
}
public void init(Engine engine) {
this.logger = engine.getLogger();
if (resolvers != null) {
for (ClassEntry item : resolvers) {
Resolver resolver = (Resolver) engine.getComponent(item);
registResolver(resolver.getMatchClass(), resolver);
if (resolver instanceof RegistModeResolver) {
((RegistModeResolver) resolver).regist(this);
}
}
getResolvers.trimToSize();
setResolvers.trimToSize();
outResolvers.trimToSize();
getResolverTypes.trimToSize();
setResolverTypes.trimToSize();
outResolverTypes.trimToSize();
}
}
public void registResolver(Class type, Resolver resolver) {
if (type == null) {
return;
}
int modifier = type.getModifiers();
boolean notAbstract = !Modifier.isAbstract(modifier) || type.isArray();
boolean notFinal = !Modifier.isFinal(modifier) || Object[].class.isAssignableFrom(type);
if (resolver instanceof GetResolver) {
if (notFinal) {
getResolverTypes.add(type);
getResolvers.add((GetResolver) resolver);
}
if (notAbstract) {
getters.putIfAbsent(type, (GetResolver) resolver);
}
}
if (resolver instanceof SetResolver) {
if (notFinal) {
setResolverTypes.add(type);
setResolvers.add((SetResolver) resolver);
}
if (notAbstract) {
setters.putIfAbsent(type, (SetResolver) resolver);
}
}
if (resolver instanceof OutResolver) {
if (notFinal) {
outResolverTypes.add(type);
outResolvers.add((OutResolver) resolver);
}
if (notAbstract) {
outters.putIfAbsent(type, (OutResolver) resolver);
}
}
}
public final Object get(Object bean, Object property) {
if (bean != null) {
return resolveGetResolverIfAbsent(bean.getClass()).get(bean, property);
}
return handleNullPointer();
}
public final void set(Object bean, Object property, Object value) {
if (bean != null) {
resolveSetResolverIfAbsent(bean.getClass()).set(bean, property, value);
return;
}
handleNullPointer();
}
protected final Object handleNullPointer() {
if (this.ignoreNullPointer) {
return null;
}
throw new ScriptRuntimeException("Null pointer.");
}
public void setIgnoreNullPointer(boolean ignoreNullPointer) {
this.ignoreNullPointer = ignoreNullPointer;
}
public void setResolvers(ClassEntry[] resolvers) {
this.resolvers = resolvers;
}
}