use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.
the class CallSiteWriter method generateCreateCallSiteArray.
private void generateCreateCallSiteArray() {
List<String> callSiteInitMethods = new LinkedList<String>();
int index = 0;
int methodIndex = 0;
final int size = callSites.size();
final int maxArrayInit = 5000;
// create array initialization methods
while (index < size) {
methodIndex++;
String methodName = "$createCallSiteArray_" + methodIndex;
callSiteInitMethods.add(methodName);
MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, methodName, "([Ljava/lang/String;)V", null, null);
controller.setMethodVisitor(mv);
mv.visitCode();
int methodLimit = size;
// check if the next block is over the max allowed
if ((methodLimit - index) > maxArrayInit) {
methodLimit = index + maxArrayInit;
}
for (; index < methodLimit; index++) {
mv.visitVarInsn(ALOAD, 0);
mv.visitLdcInsn(index);
mv.visitLdcInsn(callSites.get(index));
mv.visitInsn(AASTORE);
}
mv.visitInsn(RETURN);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
// create base createCallSiteArray method
MethodVisitor mv = controller.getClassVisitor().visitMethod(MOD_PRIVSS, CREATE_CSA_METHOD, GET_CALLSITEARRAY_DESC, null, null);
controller.setMethodVisitor(mv);
mv.visitCode();
mv.visitLdcInsn(size);
mv.visitTypeInsn(ANEWARRAY, "java/lang/String");
mv.visitVarInsn(ASTORE, 0);
for (String methodName : callSiteInitMethods) {
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESTATIC, controller.getInternalClassName(), methodName, "([Ljava/lang/String;)V", false);
}
mv.visitTypeInsn(NEW, CALLSITE_ARRAY_CLASS);
mv.visitInsn(DUP);
controller.getAcg().visitClassExpression(new ClassExpression(controller.getClassNode()));
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, CALLSITE_ARRAY_CLASS, "<init>", "(Ljava/lang/Class;[Ljava/lang/String;)V", false);
mv.visitInsn(ARETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy-core by groovy.
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();
ClassNode closureClass = getOrAddClosureClass(expression, 0);
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-core by groovy.
the class StatementMetaTypeChooser method resolveType.
public ClassNode resolveType(final Expression exp, final ClassNode current) {
if (exp instanceof ClassExpression)
return ClassHelper.CLASS_Type;
OptimizingStatementWriter.StatementMeta meta = (OptimizingStatementWriter.StatementMeta) exp.getNodeMetaData(OptimizingStatementWriter.StatementMeta.class);
ClassNode type = null;
if (meta != null)
type = meta.type;
if (type != null)
return type;
if (exp instanceof VariableExpression) {
VariableExpression ve = (VariableExpression) exp;
if (ve.isClosureSharedVariable())
return ve.getType();
type = ve.getOriginType();
if (ve.getAccessedVariable() instanceof FieldNode) {
FieldNode fn = (FieldNode) ve.getAccessedVariable();
if (!fn.getDeclaringClass().equals(current))
return fn.getOriginType();
}
} else if (exp instanceof Variable) {
Variable v = (Variable) exp;
type = v.getOriginType();
} else {
type = exp.getType();
}
return type.redirect();
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.
the class AbstractGrailsArtefactTransformer method addApiLookupFieldAndSetter.
protected void addApiLookupFieldAndSetter(ClassNode classNode, ClassNode implementationNode, String apiProperty, Expression initialValueExpression) {
FieldNode fieldNode = classNode.getField(apiProperty);
if (fieldNode == null || !fieldNode.getDeclaringClass().equals(classNode)) {
fieldNode = new FieldNode(apiProperty, Modifier.PRIVATE | Modifier.STATIC, implementationNode, classNode, initialValueExpression);
classNode.addField(fieldNode);
String setterName = "set" + MetaClassHelper.capitalize(apiProperty);
Parameter setterParameter = new Parameter(implementationNode, apiProperty);
BlockStatement setterBody = new BlockStatement();
setterBody.addStatement(new ExpressionStatement(new BinaryExpression(new AttributeExpression(new ClassExpression(classNode), new ConstantExpression(apiProperty)), Token.newSymbol(Types.EQUAL, 0, 0), new VariableExpression(setterParameter))));
GrailsASTUtils.addCompileStaticAnnotation(classNode.addMethod(setterName, Modifier.PUBLIC | Modifier.STATIC, ClassHelper.VOID_TYPE, new Parameter[] { setterParameter }, null, setterBody));
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project grails-core by grails.
the class ApiDelegateTransformation method visit.
public void visit(ASTNode[] nodes, SourceUnit source) {
if (nodes.length != 2 || !(nodes[0] instanceof AnnotationNode) || !(nodes[1] instanceof AnnotatedNode)) {
throw new GroovyBugError("Internal error: expecting [AnnotationNode, AnnotatedNode] but got: " + Arrays.asList(nodes));
}
AnnotatedNode parent = (AnnotatedNode) nodes[1];
AnnotationNode annotationNode = (AnnotationNode) nodes[0];
if (parent instanceof FieldNode) {
Expression value = annotationNode.getMember("value");
FieldNode fieldNode = (FieldNode) parent;
final ClassNode type = fieldNode.getType();
final ClassNode owner = fieldNode.getOwner();
ClassNode supportedType = owner;
if (value instanceof ClassExpression) {
supportedType = value.getType();
}
GrailsASTUtils.addDelegateInstanceMethods(supportedType, owner, type, new VariableExpression(fieldNode.getName()), resolveGenericsPlaceHolders(supportedType), isNoNullCheck(), isUseCompileStatic());
}
}
Aggregations