use of lucee.transformer.TransformerException in project Lucee by lucee.
the class Types method toType.
/**
* translate sString classname to a real type
* @param type
* @return
* @throws lucee.runtime.exp.TemplateExceptionption
*/
public static Type toType(String type) throws TransformerException {
if (type == null)
return OBJECT;
type = type.trim();
String lcType = StringUtil.toLowerCase(type);
char first = lcType.charAt(0);
switch(first) {
case 'a':
if ("any".equals(lcType))
return OBJECT;
if ("array".equals(lcType))
return ARRAY;
break;
case 'b':
if ("base64".equals(lcType))
return STRING;
if ("binary".equals(lcType))
return BYTE_VALUE_ARRAY;
if ("bool".equals(lcType) || "boolean".equals(type))
return BOOLEAN_VALUE;
if ("boolean".equals(lcType))
return BOOLEAN;
if ("byte".equals(type))
return BYTE_VALUE;
if ("byte".equals(lcType))
return BYTE;
break;
case 'c':
if ("char".equals(lcType))
return CHAR;
if ("character".equals(lcType))
return CHARACTER;
if ("collection".equals(lcType))
return BYTE_VALUE_ARRAY;
if ("component".equals(lcType))
return COMPONENT;
if ("class".equals(lcType))
return COMPONENT;
break;
case 'd':
if ("date".equals(lcType) || "datetime".equals(lcType))
return DATE_TIME;
if ("decimal".equals(lcType))
return STRING;
if ("double".equals(type))
return DOUBLE_VALUE;
if ("double".equals(lcType))
return DOUBLE;
break;
case 'e':
// if("excel".equals(lcType)) return EXCEL;
break;
case 'f':
if ("file".equals(lcType))
return FILE;
if ("float".equals(type))
return FLOAT_VALUE;
if ("float".equals(lcType))
return FLOAT;
if ("function".equals(lcType))
return UDF;
break;
case 'i':
if ("int".equals(lcType))
return INT_VALUE;
else if ("integer".equals(lcType))
return INTEGER;
else if ("image".equals(lcType))
return IMAGE;
break;
case 'j':
if ("java.lang.boolean".equals(lcType))
return BOOLEAN;
if ("java.lang.byte".equals(lcType))
return BYTE;
if ("java.lang.character".equals(lcType))
return CHARACTER;
if ("java.lang.short".equals(lcType))
return SHORT;
if ("java.lang.integer".equals(lcType))
return INTEGER;
if ("java.lang.long".equals(lcType))
return LONG;
if ("java.lang.float".equals(lcType))
return FLOAT;
if ("java.lang.double".equals(lcType))
return DOUBLE;
if ("java.io.file".equals(lcType))
return FILE;
if ("java.lang.string".equals(lcType))
return STRING;
if ("java.lang.string[]".equals(lcType))
return STRING_ARRAY;
if ("java.util.date".equals(lcType))
return DATE;
if ("java.lang.object".equals(lcType))
return OBJECT;
break;
case 'l':
if ("long".equals(type))
return LONG_VALUE;
if ("long".equals(lcType))
return LONG;
if ("locale".equals(lcType))
return LOCALE;
if ("lucee.runtime.type.Collection$Key".equals(type))
return COLLECTION_KEY;
break;
case 'n':
if ("node".equals(lcType))
return NODE;
if ("number".equals(lcType))
return DOUBLE_VALUE;
if ("numeric".equals(lcType))
return DOUBLE_VALUE;
break;
case 'o':
if ("object".equals(lcType))
return OBJECT;
break;
case 'q':
if ("query".equals(lcType))
return QUERY;
if ("querycolumn".equals(lcType))
return QUERY_COLUMN;
break;
case 's':
if ("string".equals(lcType))
return STRING;
if ("struct".equals(lcType))
return STRUCT;
if ("short".equals(type))
return SHORT_VALUE;
if ("short".equals(lcType))
return SHORT;
break;
case 't':
if ("timezone".equals(lcType))
return TIMEZONE;
if ("timespan".equals(lcType))
return TIMESPAN;
break;
case 'u':
if ("udf".equals(lcType))
return UDF;
break;
case 'v':
if ("void".equals(lcType))
return VOID;
if ("variablestring".equals(lcType))
return STRING;
if ("variable_string".equals(lcType))
return STRING;
break;
case 'x':
if ("xml".equals(lcType))
return NODE;
break;
case '[':
if ("[Ljava.lang.String;".equals(lcType))
return STRING_ARRAY;
break;
}
try {
return Type.getType(ClassUtil.loadClass(type));
} catch (ClassException e) {
throw new TransformerException(e, null);
}
}
use of lucee.transformer.TransformerException in project Lucee by lucee.
the class Function method evaluate.
@Override
public void evaluate(Tag tag, TagLibTag libTag, FunctionLib[] flibs) throws EvaluatorException {
// Body p=(Body) tag.getParent();
// Statement pp = p.getParent();
boolean isCI = true;
try {
isCI = ASMUtil.getAncestorPage(tag).isComponent() || ASMUtil.getAncestorPage(tag).isInterface();
} catch (TransformerException e) {
}
Attribute attrName = tag.getAttribute("name");
if (attrName != null) {
Expression expr = attrName.getValue();
PageSource ps = null;
if (expr instanceof LitString && !isCI) {
Page p = ASMUtil.getAncestorPage(tag, null);
if (p != null) {
SourceCode sc = p.getSourceCode();
if (sc instanceof PageSourceCode) {
PageSourceCode psc = (PageSourceCode) sc;
ps = psc.getPageSource();
}
}
checkFunctionName(((LitString) expr).getString(), flibs, ps);
}
}
// attribute modifier
boolean isStatic = false;
{
Attribute attrModifier = tag.getAttribute("modifier");
if (attrModifier != null) {
ExprString expr = tag.getFactory().toExprString(attrModifier.getValue());
if (!(expr instanceof Literal))
throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
String modifier = StringUtil.emptyIfNull(((Literal) expr).getString()).trim();
if (!StringUtil.isEmpty(modifier) && !"abstract".equalsIgnoreCase(modifier) && !"final".equalsIgnoreCase(modifier) && !"static".equalsIgnoreCase(modifier))
throw new EvaluatorException("Attribute modifier of the Tag Function, must be one of the following literal string values: [abstract,final,static]");
isStatic = "static".equalsIgnoreCase(modifier);
boolean abstr = "abstract".equalsIgnoreCase(modifier);
if (abstr)
throwIfNotEmpty(tag);
}
}
// cachedWithin
{
Attribute attrCachedWithin = tag.getAttribute("cachedwithin");
if (attrCachedWithin != null) {
Expression val = attrCachedWithin.getValue();
tag.addAttribute(new Attribute(attrCachedWithin.isDynamicType(), attrCachedWithin.getName(), ASMUtil.cachedWithinValue(val), attrCachedWithin.getType()));
}
}
// Attribute localMode
{
Attribute attrLocalMode = tag.getAttribute("localmode");
if (attrLocalMode != null) {
Expression expr = attrLocalMode.getValue();
String str = ASMUtil.toString(expr, null);
if (!StringUtil.isEmpty(str) && AppListenerUtil.toLocalMode(str, -1) == -1)
throw new EvaluatorException("Attribute localMode of the Tag Function, must be a literal value (modern, classic, true or false)");
// boolean output = ((LitBoolean)expr).getBooleanValue();
// if(!output) ASMUtil.removeLiterlChildren(tag, true);
}
}
// Attribute Output
{
Attribute attrOutput = tag.getAttribute("output");
if (attrOutput != null) {
Expression expr = tag.getFactory().toExprBoolean(attrOutput.getValue());
if (!(expr instanceof LitBoolean))
throw new EvaluatorException("Attribute output of the Tag Function, must be a literal boolean value (true or false, yes or no)");
}
}
// Buffer output
{
Attribute attrBufferOutput = tag.getAttribute("bufferoutput");
if (attrBufferOutput != null) {
Expression expr = tag.getFactory().toExprBoolean(attrBufferOutput.getValue());
if (!(expr instanceof LitBoolean))
throw new EvaluatorException("Attribute bufferOutput of the Tag Function, must be a literal boolean value (true or false, yes or no)");
}
}
// check attribute values
Map<String, Attribute> attrs = tag.getAttributes();
Iterator<Attribute> it = attrs.values().iterator();
while (it.hasNext()) {
checkAttributeValue(tag, it.next());
}
// add to static scope
if (isStatic) {
// remove that tag from parent
ASMUtil.remove(tag);
Body body = (Body) tag.getParent();
StaticBody sb = Static.getStaticBody(body);
sb.addStatement(tag);
}
}
use of lucee.transformer.TransformerException in project Lucee by lucee.
the class Property method getComponentName.
public static String getComponentName(Tag tag) throws EvaluatorException {
Page page;
try {
page = ASMUtil.getAncestorPage(tag);
} catch (TransformerException e) {
throw new EvaluatorException(e.getMessage());
}
String ns = tag.getTagLibTag().getTagLib().getNameSpaceAndSeparator();
String compName = ns + (page.getSourceCode().getDialect() == CFMLEngine.DIALECT_CFML ? Constants.CFML_COMPONENT_TAG_NAME : Constants.LUCEE_COMPONENT_TAG_NAME);
return compName;
}
use of lucee.transformer.TransformerException in project Lucee by lucee.
the class TryCatchFinally method _writeOut.
/**
* @see lucee.transformer.bytecode.statement.StatementBase#_writeOut(org.objectweb.asm.commons.GeneratorAdapter)
*/
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
final GeneratorAdapter adapter = bc.getAdapter();
adapter.visitLabel(begin);
// Reference ref=null;
final int lRef = adapter.newLocal(Types.REFERENCE);
adapter.visitInsn(Opcodes.ACONST_NULL);
adapter.storeLocal(lRef);
// has no try body, if there is no try body, no catches are executed, only finally
if (!tryBody.hasStatements()) {
if (finallyBody != null)
finallyBody.writeOut(bc);
return;
}
// PageExceptionImpl old=pc.getCatch();
final int old = adapter.newLocal(Types.PAGE_EXCEPTION);
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.GET_CATCH);
adapter.storeLocal(old);
TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) throws TransformerException {
adapter.loadArg(0);
adapter.loadLocal(old);
adapter.invokeVirtual(Types.PAGE_CONTEXT, TagTry.SET_CATCH_PE);
_writeOutFinally(bc, lRef);
}
}, getFlowControlFinal());
// try
tcfv.visitTryBegin(bc);
tryBody.writeOut(bc);
int lThrow = tcfv.visitTryEndCatchBeging(bc);
_writeOutCatch(bc, lRef, lThrow, old);
tcfv.visitCatchEnd(bc);
}
use of lucee.transformer.TransformerException in project Lucee by lucee.
the class TagHelper method writeOut.
/**
* writes out the tag
* @param tag
* @param bc
* @param doReuse
* @throws TransformerException
* @throws BundleException
* @throws ClassException
*/
public static void writeOut(Tag tag, BytecodeContext bc, boolean doReuse, final FlowControlFinal fcf) throws TransformerException {
final GeneratorAdapter adapter = bc.getAdapter();
final TagLibTag tlt = tag.getTagLibTag();
final ClassDefinition cd = tlt.getTagClassDefinition();
final boolean fromBundle = cd.getName() != null;
final Type currType;
if (fromBundle) {
try {
if (Reflector.isInstaneOf(cd.getClazz(), BodyTag.class))
currType = BODY_TAG;
else
currType = TAG;
} catch (Exception e) {
if (e instanceof TransformerException)
throw (TransformerException) e;
throw new TransformerException(e, tag.getStart());
}
} else
currType = getTagType(tag);
final int currLocal = adapter.newLocal(currType);
Label tagBegin = new Label();
Label tagEnd = new Label();
ExpressionUtil.visitLine(bc, tag.getStart());
// TODO adapter.visitLocalVariable("tag", "L"+currType.getInternalName()+";", null, tagBegin, tagEnd, currLocal);
adapter.visitLabel(tagBegin);
// tag=pc.use(String tagClassName,String tagBundleName, String tagBundleVersion, String fullname,int attrType) throws PageException {
adapter.loadArg(0);
adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
adapter.push(cd.getClassName());
// has bundle info/version
if (fromBundle) {
// name
adapter.push(cd.getName());
// version
if (cd.getVersion() != null)
adapter.push(cd.getVersionAsString());
else
ASMConstants.NULL(adapter);
}
adapter.push(tlt.getFullName());
adapter.push(tlt.getAttributeType());
adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, fromBundle ? USE5 : USE3);
if (currType != TAG)
adapter.checkCast(currType);
adapter.storeLocal(currLocal);
TryFinallyVisitor outerTcfv = new TryFinallyVisitor(new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) {
adapter.loadArg(0);
adapter.checkCast(Types.PAGE_CONTEXT_IMPL);
adapter.loadLocal(currLocal);
if (cd.getName() != null) {
adapter.push(cd.getName());
if (cd.getVersion() != null)
adapter.push(cd.getVersionAsString());
else
ASMConstants.NULL(adapter);
}
adapter.invokeVirtual(Types.PAGE_CONTEXT_IMPL, fromBundle ? RE_USE3 : RE_USE1);
}
}, null);
if (doReuse)
outerTcfv.visitTryBegin(bc);
// appendix
if (tlt.hasAppendix()) {
adapter.loadLocal(currLocal);
adapter.push(tag.getAppendix());
if (// PageContextUtil.setAppendix(tag,appendix)
fromBundle)
ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, SET_APPENDIX2);
else
// tag.setAppendix(appendix)
ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, SET_APPENDIX1);
}
// hasBody
boolean hasBody = tag.getBody() != null;
if (tlt.isBodyFree() && tlt.hasBodyMethodExists()) {
adapter.loadLocal(currLocal);
adapter.push(hasBody);
if (// PageContextUtil.setAppendix(tag,appendix)
fromBundle)
ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, HAS_BODY2);
else
// tag.setAppendix(appendix)
ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, HAS_BODY1);
}
// default attributes (get overwritten by attributeCollection because of that set before)
setAttributes(bc, tag, currLocal, currType, true, fromBundle);
// attributeCollection
Attribute attrColl = tag.getAttribute("attributecollection");
if (attrColl != null) {
int attrType = tag.getTagLibTag().getAttributeType();
if (TagLibTag.ATTRIBUTE_TYPE_NONAME != attrType) {
tag.removeAttribute("attributecollection");
// TagUtil.setAttributeCollection(Tag, Struct)
adapter.loadArg(0);
adapter.loadLocal(currLocal);
if (currType != TAG)
adapter.cast(currType, TAG);
// /
TagLibTagAttr[] missings = tag.getMissingAttributes();
if (!ArrayUtil.isEmpty(missings)) {
ArrayVisitor av = new ArrayVisitor();
av.visitBegin(adapter, MISSING_ATTRIBUTE, missings.length);
int count = 0;
TagLibTagAttr miss;
for (int i = 0; i < missings.length; i++) {
miss = missings[i];
av.visitBeginItem(adapter, count++);
bc.getFactory().registerKey(bc, bc.getFactory().createLitString(miss.getName()), false);
adapter.push(miss.getType());
if (ArrayUtil.isEmpty(miss.getAlias()))
adapter.invokeStatic(MISSING_ATTRIBUTE, NEW_INSTANCE_MAX2);
else {
new LiteralStringArray(bc.getFactory(), miss.getAlias()).writeOut(bc, Expression.MODE_REF);
adapter.invokeStatic(MISSING_ATTRIBUTE, NEW_INSTANCE_MAX3);
}
av.visitEndItem(bc.getAdapter());
}
av.visitEnd();
} else {
ASMConstants.NULL(adapter);
}
// /
attrColl.getValue().writeOut(bc, Expression.MODE_REF);
adapter.push(attrType);
adapter.invokeStatic(TAG_UTIL, SET_ATTRIBUTE_COLLECTION);
}
}
// metadata
Attribute attr;
Map<String, Attribute> metadata = tag.getMetaData();
if (metadata != null) {
Iterator<Attribute> it = metadata.values().iterator();
while (it.hasNext()) {
attr = it.next();
adapter.loadLocal(currLocal);
adapter.push(attr.getName());
attr.getValue().writeOut(bc, Expression.MODE_REF);
if (fromBundle)
ASMUtil.invoke(ASMUtil.STATIC, adapter, Types.TAG_UTIL, SET_META_DATA3);
else
ASMUtil.invoke(ASMUtil.VIRTUAL, adapter, currType, SET_META_DATA2);
}
}
// set attributes
setAttributes(bc, tag, currLocal, currType, false, fromBundle);
// Body
if (hasBody) {
final int state = adapter.newLocal(Types.INT_VALUE);
// int state=tag.doStartTag();
adapter.loadLocal(currLocal);
ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_START_TAG);
// adapter.invokeVirtual(currType, DO_START_TAG);
adapter.storeLocal(state);
// if (state!=Tag.SKIP_BODY)
Label endBody = new Label();
adapter.loadLocal(state);
adapter.push(javax.servlet.jsp.tagext.Tag.SKIP_BODY);
adapter.visitJumpInsn(Opcodes.IF_ICMPEQ, endBody);
// pc.initBody(tag, state);
adapter.loadArg(0);
adapter.loadLocal(currLocal);
adapter.loadLocal(state);
adapter.invokeVirtual(Types.PAGE_CONTEXT, INIT_BODY);
OnFinally onFinally = new OnFinally() {
@Override
public void _writeOut(BytecodeContext bc) {
Label endIf = new Label();
/*if(tlt.handleException() && fcf!=null && fcf.getAfterFinalGOTOLabel()!=null){
ASMUtil.visitLabel(adapter, fcf.getFinalEntryLabel());
}*/
adapter.loadLocal(state);
adapter.push(javax.servlet.jsp.tagext.Tag.EVAL_BODY_INCLUDE);
adapter.visitJumpInsn(Opcodes.IF_ICMPEQ, endIf);
// ... pc.popBody();
adapter.loadArg(0);
adapter.invokeVirtual(Types.PAGE_CONTEXT, POP_BODY);
adapter.pop();
adapter.visitLabel(endIf);
// tag.doFinally();
if (tlt.handleException()) {
adapter.loadLocal(currLocal);
ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_FINALLY);
// adapter.invokeVirtual(currType, DO_FINALLY);
}
// GOTO after execution body, used when a continue/break was called before
/*if(fcf!=null) {
Label l = fcf.getAfterFinalGOTOLabel();
if(l!=null)adapter.visitJumpInsn(Opcodes.GOTO, l);
}*/
}
};
if (tlt.handleException()) {
TryCatchFinallyVisitor tcfv = new TryCatchFinallyVisitor(onFinally, fcf);
tcfv.visitTryBegin(bc);
doTry(bc, adapter, tag, currLocal, currType, fromBundle);
int t = tcfv.visitTryEndCatchBeging(bc);
// tag.doCatch(t);
adapter.loadLocal(currLocal);
adapter.loadLocal(t);
// adapter.visitVarInsn(Opcodes.ALOAD,t);
ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_CATCH);
// adapter.invokeVirtual(currType, DO_CATCH);
tcfv.visitCatchEnd(bc);
} else {
TryFinallyVisitor tfv = new TryFinallyVisitor(onFinally, fcf);
tfv.visitTryBegin(bc);
doTry(bc, adapter, tag, currLocal, currType, fromBundle);
tfv.visitTryEnd(bc);
}
adapter.visitLabel(endBody);
} else {
// tag.doStartTag();
adapter.loadLocal(currLocal);
ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_START_TAG);
// adapter.invokeVirtual(currType, DO_START_TAG);
adapter.pop();
}
// if (tag.doEndTag()==Tag.SKIP_PAGE) throw new Abort(0<!-- SCOPE_PAGE -->);
Label endDoEndTag = new Label();
adapter.loadLocal(currLocal);
ASMUtil.invoke(fromBundle ? ASMUtil.INTERFACE : ASMUtil.VIRTUAL, adapter, currType, DO_END_TAG);
// adapter.invokeVirtual(currType, DO_END_TAG);
adapter.push(javax.servlet.jsp.tagext.Tag.SKIP_PAGE);
adapter.visitJumpInsn(Opcodes.IF_ICMPNE, endDoEndTag);
adapter.push(Abort.SCOPE_PAGE);
adapter.invokeStatic(ABORT, NEW_INSTANCE);
adapter.throwException();
adapter.visitLabel(endDoEndTag);
if (doReuse) {
// } finally{pc.reuse(tag);}
outerTcfv.visitTryEnd(bc);
}
adapter.visitLabel(tagEnd);
ExpressionUtil.visitLine(bc, tag.getEnd());
}
Aggregations