use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class NativeCallExpression method infer.
@Override
public void infer(InferContext inferCtx) {
Type type = null;
if (insNode != null) {
insNode.ref.infer(inferCtx);
type = insNode.ref.type.copy();
} else {
Class cls = inferCtx.gt.loadClassBySimpleName(this.clsNode.cls);
if (cls == null) {
BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "该类不存在");
be.pushToken(GrammarToken.createToken(clsNode.cls, token.line));
throw be;
}
type = new Type(cls);
}
for (NativeNode node : chain) {
if (type.cls == Object.class) {
this.type = type;
// 以后生成代码有问题
return;
}
if (node instanceof NativeAtrributeNode) {
String attr = ((NativeAtrributeNode) node).attribute;
try {
Field f = type.cls.getField(attr);
Class c = f.getType();
type.cls = c;
} catch (SecurityException e) {
BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "无法访问属性", e);
be.pushToken(GrammarToken.createToken(attr, token.line));
throw be;
} catch (NoSuchFieldException e) {
BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "无此属性", e);
be.pushToken(GrammarToken.createToken(attr, token.line));
throw be;
}
} else if (node instanceof NativeArrayNode) {
if (!type.cls.isArray()) {
BeetlException be = new BeetlException(BeetlException.ARRAY_TYPE_ERROR);
// 最好是把上一个字符显示出来
be.pushToken(GrammarToken.createToken("[]", token.line));
throw be;
}
type.cls = type.cls.getComponentType();
} else if (node instanceof NativeMethodNode) {
NativeMethodNode methodNode = (NativeMethodNode) node;
String method = methodNode.method;
Expression[] expList = methodNode.paras;
Class[] argTypes = expList.length == 0 ? ObjectUtil.EMPTY_CLASS_ARRAY : new Class[expList.length];
for (int i = 0; i < expList.length; i++) {
expList[i].infer(inferCtx);
argTypes[i] = expList[i].type.cls;
}
try {
ObjectMethodMatchConf conf = ObjectUtil.findMethod(type.cls, method, argTypes);
if (conf == null) {
type.cls = Object.class;
} else {
type.cls = conf.method.getReturnType();
}
} catch (SecurityException e) {
BeetlException be = new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "不能调用方法");
be.pushToken(GrammarToken.createToken(method, token.line));
throw be;
}
}
}
this.type = type;
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class Json method call.
@Override
public Object call(Object[] paras, Context ctx) {
if (tool == null) {
return "找不到JSonTool工具,https://git.oschina.net/xiandafu/beetl-json/attach_files 下载并放到classpath下";
}
Object o = paras[0];
String policy = "";
if (paras.length == 2) {
policy = paras[1].toString();
}
Method m;
try {
m = tool.getClass().getMethod("serialize", new Class[] { Object.class, String.class });
} catch (Exception e) {
throw new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "BTJsonTool出错");
}
String json;
try {
json = (String) m.invoke(tool, new Object[] { o, policy });
} catch (Exception e) {
throw new BeetlException(BeetlException.NATIVE_CALL_EXCEPTION, "BTJsonTool 序列化对象出错", e);
}
return json;
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class SelectStatement method execute.
@Override
public void execute(Context ctx) {
Object base = null;
if (value != null) {
base = value.evaluate(ctx);
if (base == null) {
BeetlException ex = new BeetlException(BeetlException.NULL);
ex.pushToken(value.token);
throw ex;
}
}
boolean isMatch = false;
for (int i = 0; i < conditions.length; i++) {
Expression exp = conditions[i];
Object condValue = exp.evaluate(ctx);
if (value == null) {
if (condValue instanceof Boolean) {
isMatch = (Boolean) condValue;
} else {
BeetlException be = new BeetlException(BeetlException.BOOLEAN_EXPECTED_ERROR);
be.pushToken(exp.token);
throw be;
}
} else {
if (ALU.equals(base, condValue)) {
isMatch = true;
}
}
if (isMatch) {
BlockStatement block = this.blocks[i];
if (block != null) {
block.execute(ctx);
}
break;
}
}
if (!isMatch && defaultBlock != null) {
defaultBlock.execute(ctx);
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class TagStatement method execute.
@Override
public void execute(Context ctx) {
Tag tag = null;
try {
TagFactory tagFactory = ctx.gt.getTagFactory(this.tagName);
tag = tagFactory.createTag();
Object[] args = null;
if (paras.length == 0) {
args = ObjectUtil.EMPTY_OBJECT_ARRAY;
} else {
args = new Object[paras.length];
for (int i = 0; i < args.length; i++) {
args[i] = paras[i].evaluate(ctx);
}
}
tag.init(ctx, args, block);
runTag(tag, ctx);
} catch (BeetlException ex) {
ex.pushToken(this.token);
throw ex;
} catch (RuntimeException ex) {
BeetlException bex = new BeetlException(BeetlException.TAG_INSTANCE_ERROR, ex.getMessage(), ex);
bex.pushToken(token);
throw bex;
}
}
use of org.beetl.core.exception.BeetlException in project beetl2.0 by javamonkey.
the class VarRefAssignStatement method execute.
public void execute(Context ctx) {
Object value = exp.evaluate(ctx);
Object obj = varRef.evaluateUntilLast(ctx);
Object key = null;
if (lastVarAttribute instanceof VarSquareAttribute) {
key = (((VarSquareAttribute) lastVarAttribute).exp).evaluate(ctx);
} else {
key = lastVarAttribute.name;
}
try {
ObjectAA.defaultObjectAA().setValue(obj, key, value);
} catch (ClassCastException ex) {
BeetlException bx = new BeetlException(BeetlException.ATTRIBUTE_INVALID, ex);
bx.pushToken(lastVarAttribute.token);
throw bx;
} catch (BeetlException be) {
be.pushToken(lastVarAttribute.token);
throw be;
}
}
Aggregations