use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class ClosureWriter method writeClosure.
public void writeClosure(ClosureExpression expression) {
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
ClassNode classNode = controller.getClassNode();
AsmClassGenerator acg = controller.getAcg();
// generate closure as public class to make sure it can be properly invoked by classes of the
// Groovy runtime without circumventing JVM access checks (see CachedMethod for example).
int mods = ACC_PUBLIC;
if (classNode.isInterface()) {
mods |= ACC_STATIC;
}
ClassNode closureClass = getOrAddClosureClass(expression, mods);
String closureClassinternalName = BytecodeHelper.getClassInternalName(closureClass);
List constructors = closureClass.getDeclaredConstructors();
ConstructorNode node = (ConstructorNode) constructors.get(0);
Parameter[] localVariableParams = node.getParameters();
mv.visitTypeInsn(NEW, closureClassinternalName);
mv.visitInsn(DUP);
if (controller.isStaticMethod() || compileStack.isInSpecialConstructorCall()) {
(new ClassExpression(classNode)).visit(acg);
(new ClassExpression(controller.getOutermostClass())).visit(acg);
} else {
mv.visitVarInsn(ALOAD, 0);
controller.getOperandStack().push(ClassHelper.OBJECT_TYPE);
loadThis();
}
// on the stack
for (int i = 2; i < localVariableParams.length; i++) {
Parameter param = localVariableParams[i];
String name = param.getName();
loadReference(name, controller);
if (param.getNodeMetaData(ClosureWriter.UseExistingReference.class) == null) {
param.setNodeMetaData(ClosureWriter.UseExistingReference.class, Boolean.TRUE);
}
}
// we may need to pass in some other constructors
//cv.visitMethodInsn(INVOKESPECIAL, innerClassinternalName, "<init>", prototype + ")V");
mv.visitMethodInsn(INVOKESPECIAL, closureClassinternalName, "<init>", BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, localVariableParams), false);
controller.getOperandStack().replace(ClassHelper.CLOSURE_TYPE, localVariableParams.length);
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticImportVisitor method findStaticPropertyAccessor.
private Expression findStaticPropertyAccessor(ClassNode staticImportType, String propName) {
String accessorName = getAccessorName(propName);
Expression accessor = findStaticPropertyAccessorByFullName(staticImportType, accessorName);
if (accessor == null && accessorName.startsWith("get")) {
accessor = findStaticPropertyAccessorByFullName(staticImportType, "is" + accessorName.substring(3));
}
if (accessor == null && hasStaticProperty(staticImportType, propName)) {
// args will be replaced
if (inLeftExpression)
accessor = new StaticMethodCallExpression(staticImportType, accessorName, ArgumentListExpression.EMPTY_ARGUMENTS);
else
accessor = new PropertyExpression(new ClassExpression(staticImportType), propName);
}
return accessor;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticImportVisitor method transformPropertyExpression.
protected Expression transformPropertyExpression(PropertyExpression pe) {
if (currentMethod != null && currentMethod.isStatic() && pe.getObjectExpression() instanceof VariableExpression && ((VariableExpression) pe.getObjectExpression()).isSuperExpression()) {
PropertyExpression pexp = new PropertyExpression(new ClassExpression(currentClass.getSuperClass()), transform(pe.getProperty()));
pexp.setSourcePosition(pe);
return pexp;
}
boolean oldInPropertyExpression = inPropertyExpression;
Expression oldFoundArgs = foundArgs;
Expression oldFoundConstant = foundConstant;
inPropertyExpression = true;
foundArgs = null;
foundConstant = null;
Expression objectExpression = transform(pe.getObjectExpression());
boolean candidate = false;
if (objectExpression instanceof MethodCallExpression) {
candidate = ((MethodCallExpression) objectExpression).isImplicitThis();
}
if (foundArgs != null && foundConstant != null && candidate) {
Expression result = findStaticMethodImportFromModule(foundConstant, foundArgs);
if (result != null) {
objectExpression = result;
objectExpression.setSourcePosition(pe);
}
}
inPropertyExpression = oldInPropertyExpression;
foundArgs = oldFoundArgs;
foundConstant = oldFoundConstant;
pe.setObjectExpression(objectExpression);
return pe;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class StaticImportVisitor method transformInlineConstants.
// resolve constant-looking expressions statically (do here as gets transformed away later)
private Expression transformInlineConstants(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
if (pe.getObjectExpression() instanceof ClassExpression) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum())
return exp;
Expression constant = findConstant(getField(type, pe.getPropertyAsString()));
if (constant != null)
return constant;
}
} else if (exp instanceof ListExpression) {
ListExpression le = (ListExpression) exp;
ListExpression result = new ListExpression();
for (Expression e : le.getExpressions()) {
result.addExpression(transformInlineConstants(e));
}
return result;
}
return exp;
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class Java5 method annotationValueToExpression.
private Expression annotationValueToExpression(Object value) {
if (value == null || value instanceof String || value instanceof Number || value instanceof Character || value instanceof Boolean)
return new ConstantExpression(value);
if (value instanceof Class)
return new ClassExpression(ClassHelper.makeWithoutCaching((Class) value));
if (value.getClass().isArray()) {
ListExpression elementExprs = new ListExpression();
int len = Array.getLength(value);
for (int i = 0; i != len; ++i) elementExprs.addExpression(annotationValueToExpression(Array.get(value, i)));
return elementExprs;
}
return null;
}
Aggregations