use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class StaticTypesStatementWriter method writeForInLoop.
@Override
protected void writeForInLoop(final ForStatement loop) {
controller.getAcg().onLineNumber(loop, "visitForLoop");
writeStatementLabel(loop);
CompileStack compileStack = controller.getCompileStack();
MethodVisitor mv = controller.getMethodVisitor();
OperandStack operandStack = controller.getOperandStack();
compileStack.pushLoop(loop.getVariableScope(), loop.getStatementLabels());
// Identify type of collection
TypeChooser typeChooser = controller.getTypeChooser();
Expression collectionExpression = loop.getCollectionExpression();
ClassNode collectionType = typeChooser.resolveType(collectionExpression, controller.getClassNode());
Parameter loopVariable = loop.getVariable();
int size = operandStack.getStackLength();
if (collectionType.isArray() && loopVariable.getOriginType().equals(collectionType.getComponentType())) {
writeOptimizedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
} else if (ENUMERATION_CLASSNODE.equals(collectionType)) {
writeEnumerationBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
} else {
writeIteratorBasedForEachLoop(compileStack, operandStack, mv, loop, collectionExpression, collectionType, loopVariable);
}
operandStack.popDownTo(size);
compileStack.pop();
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class StaticTypesTypeChooser method resolveType.
@Override
public ClassNode resolveType(final Expression exp, final ClassNode current) {
ASTNode target = exp instanceof VariableExpression ? getTarget((VariableExpression) exp) : exp;
ClassNode inferredType = (ClassNode) target.getNodeMetaData(StaticTypesMarker.DECLARATION_INFERRED_TYPE);
if (inferredType == null) {
inferredType = (ClassNode) target.getNodeMetaData(StaticTypesMarker.INFERRED_TYPE);
if (inferredType == null && target instanceof VariableExpression && ((VariableExpression) target).getAccessedVariable() instanceof Parameter) {
target = (Parameter) ((VariableExpression) target).getAccessedVariable();
inferredType = ((Parameter) target).getOriginType();
}
}
if (inferredType != null) {
if (ClassHelper.VOID_TYPE == inferredType) {
// we are in a case of a type inference failure, probably because code was generated
// it is better to avoid using this
inferredType = super.resolveType(exp, current);
}
return inferredType;
}
if (target instanceof VariableExpression && ((VariableExpression) target).isThisExpression()) {
// AsmClassGenerator may create "this" expressions that the type checker knows nothing about
return current;
}
return super.resolveType(exp, current);
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class AnnotationConstantsVisitor method transformConstantExpression.
private Expression transformConstantExpression(Expression val, ClassNode returnType) {
ClassNode returnWrapperType = ClassHelper.getWrapper(returnType);
if (val instanceof ConstantExpression) {
Expression result = revertType(val, returnWrapperType);
if (result != null) {
return result;
}
return val;
}
if (val instanceof CastExpression) {
CastExpression castExp = (CastExpression) val;
Expression castee = castExp.getExpression();
if (castee instanceof ConstantExpression) {
if (ClassHelper.getWrapper(castee.getType()).isDerivedFrom(returnWrapperType)) {
return castee;
}
Expression result = revertType(castee, returnWrapperType);
if (result != null) {
return result;
}
return castee;
}
}
return val;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class ClassNodeResolver method findDecompiled.
/**
* Search for classes using ASM decompiler
*/
private LookupResult findDecompiled(String name, CompilationUnit compilationUnit, GroovyClassLoader loader) {
ClassNode node = ClassHelper.make(name);
if (node.isResolved()) {
return new LookupResult(null, node);
}
DecompiledClassNode asmClass = null;
String fileName = name.replace('.', '/') + ".class";
URL resource = loader.getResource(fileName);
if (resource != null) {
try {
asmClass = new DecompiledClassNode(AsmDecompiler.parseClass(resource), new AsmReferenceResolver(this, compilationUnit));
if (!asmClass.getName().equals(name)) {
// this may happen under Windows because getResource is case insensitive under that OS!
asmClass = null;
}
} catch (IOException e) {
// fall through and attempt other search strategies
}
}
if (asmClass != null) {
if (isFromAnotherClassLoader(loader, fileName)) {
return tryAsScript(name, compilationUnit, asmClass);
}
return new LookupResult(null, asmClass);
}
return null;
}
use of org.codehaus.groovy.ast.ClassNode in project groovy-core by groovy.
the class StaticTypesStatementWriter method loadFromArray.
private void loadFromArray(MethodVisitor mv, BytecodeVariable variable, int array, int iteratorIdx) {
OperandStack os = controller.getOperandStack();
mv.visitVarInsn(ALOAD, array);
mv.visitVarInsn(ILOAD, iteratorIdx);
ClassNode varType = variable.getType();
boolean primitiveType = ClassHelper.isPrimitiveType(varType);
boolean isByte = ClassHelper.byte_TYPE.equals(varType);
boolean isShort = ClassHelper.short_TYPE.equals(varType);
boolean isInt = ClassHelper.int_TYPE.equals(varType);
boolean isLong = ClassHelper.long_TYPE.equals(varType);
boolean isFloat = ClassHelper.float_TYPE.equals(varType);
boolean isDouble = ClassHelper.double_TYPE.equals(varType);
boolean isChar = ClassHelper.char_TYPE.equals(varType);
boolean isBoolean = ClassHelper.boolean_TYPE.equals(varType);
if (primitiveType) {
if (isByte) {
mv.visitInsn(BALOAD);
}
if (isShort) {
mv.visitInsn(SALOAD);
}
if (isInt || isChar || isBoolean) {
mv.visitInsn(isChar ? CALOAD : isBoolean ? BALOAD : IALOAD);
}
if (isLong) {
mv.visitInsn(LALOAD);
}
if (isFloat) {
mv.visitInsn(FALOAD);
}
if (isDouble) {
mv.visitInsn(DALOAD);
}
} else {
mv.visitInsn(AALOAD);
}
os.push(varType);
os.storeVar(variable);
}
Aggregations