use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class Java5 method configureAnnotation.
private void configureAnnotation(AnnotationNode node, Annotation annotation) {
Class type = annotation.annotationType();
if (type == Retention.class) {
Retention r = (Retention) annotation;
RetentionPolicy value = r.value();
setRetentionPolicy(value, node);
node.setMember("value", new PropertyExpression(new ClassExpression(ClassHelper.makeWithoutCaching(RetentionPolicy.class, false)), value.toString()));
} else if (type == Target.class) {
Target t = (Target) annotation;
ElementType[] elements = t.value();
ListExpression elementExprs = new ListExpression();
for (ElementType element : elements) {
elementExprs.addExpression(new PropertyExpression(new ClassExpression(ClassHelper.ELEMENT_TYPE_TYPE), element.name()));
}
node.setMember("value", elementExprs);
} else {
Method[] declaredMethods;
try {
declaredMethods = type.getDeclaredMethods();
} catch (SecurityException se) {
declaredMethods = new Method[0];
}
for (Method declaredMethod : declaredMethods) {
try {
Object value = declaredMethod.invoke(annotation);
Expression valueExpression = annotationValueToExpression(value);
if (valueExpression == null)
continue;
node.setMember(declaredMethod.getName(), valueExpression);
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
}
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
the class InvokeDynamicWriter method coerce.
@Override
public void coerce(ClassNode from, ClassNode target) {
ClassNode wrapper = ClassHelper.getWrapper(target);
makeIndyCall(invokeMethod, EmptyExpression.INSTANCE, false, false, "asType", new ClassExpression(wrapper));
if (ClassHelper.boolean_TYPE.equals(target) || ClassHelper.Boolean_TYPE.equals(target)) {
writeIndyCast(ClassHelper.OBJECT_TYPE, target);
} else {
BytecodeHelper.doCast(controller.getMethodVisitor(), wrapper);
controller.getOperandStack().replace(wrapper);
controller.getOperandStack().doGroovyCast(target);
}
}
use of org.codehaus.groovy.ast.expr.ClassExpression in project groovy by apache.
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 by apache.
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 groovy by apache.
the class AbstractASTTransformation method getClassList.
@Deprecated
public List<ClassNode> getClassList(AnnotationNode anno, String name) {
List<ClassNode> list = new ArrayList<ClassNode>();
Expression expr = anno.getMember(name);
if (expr != null && expr instanceof ListExpression) {
final ListExpression listExpression = (ListExpression) expr;
for (Expression itemExpr : listExpression.getExpressions()) {
if (itemExpr != null && itemExpr instanceof ClassExpression) {
ClassNode cn = itemExpr.getType();
if (cn != null)
list.add(cn);
}
}
} else if (expr != null && expr instanceof ClassExpression) {
ClassNode cn = expr.getType();
if (cn != null)
list.add(cn);
}
return list;
}
Aggregations