From 001d765704f61a813fb02952364938446b66b927 Mon Sep 17 00:00:00 2001 From: chiba Date: Wed, 8 Jun 2005 08:58:33 +0000 Subject: [PATCH] fixed a bug so that the compiler distinguishes a length field and the array length operator. git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@175 30ef5769-5b8d-40dd-aea6-55b5d6557bb3 --- src/main/javassist/compiler/CodeGen.java | 14 +-------- .../javassist/compiler/MemberCodeGen.java | 29 ++++++++++++++++--- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/main/javassist/compiler/CodeGen.java b/src/main/javassist/compiler/CodeGen.java index 5ddbc3ba..10def2e6 100644 --- a/src/main/javassist/compiler/CodeGen.java +++ b/src/main/javassist/compiler/CodeGen.java @@ -1423,9 +1423,7 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { ASTree oprand = expr.oprand1(); if (token == '.') { String member = ((Symbol)expr.oprand2()).get(); - if (member.equals("length")) - atArrayLength(expr); - else if (member.equals("class")) + if (member.equals("class")) atClassObject(expr); // .class else atFieldRead(expr); @@ -1575,16 +1573,6 @@ public abstract class CodeGen extends Visitor implements Opcode, TokenId { className = "java/lang/Class"; } - public void atArrayLength(Expr expr) throws CompileError { - expr.oprand1().accept(this); - if (arrayDim == 0) - throw new CompileError(".length applied to a non array"); - - bytecode.addOpcode(ARRAYLENGTH); - exprType = INT; - arrayDim = 0; - } - public void atArrayRead(ASTree array, ASTree index) throws CompileError { diff --git a/src/main/javassist/compiler/MemberCodeGen.java b/src/main/javassist/compiler/MemberCodeGen.java index 7ac6bba1..88023202 100644 --- a/src/main/javassist/compiler/MemberCodeGen.java +++ b/src/main/javassist/compiler/MemberCodeGen.java @@ -651,7 +651,7 @@ public class MemberCodeGen extends CodeGen { protected void atFieldAssign(Expr expr, int op, ASTree left, ASTree right, boolean doDup) throws CompileError { - CtField f = fieldAccess(left); + CtField f = fieldAccess(left, false); boolean is_static = resultStatic; if (op != '=' && !is_static) bytecode.addOpcode(DUP); @@ -728,7 +728,12 @@ public class MemberCodeGen extends CodeGen { protected void atFieldRead(ASTree expr) throws CompileError { - CtField f = fieldAccess(expr); + CtField f = fieldAccess(expr, true); + if (f == null) { + atArrayLength(expr); + return; + } + boolean is_static = resultStatic; ASTree cexpr = TypeChecker.getConstantFieldValue(f); if (cexpr == null) @@ -739,6 +744,15 @@ public class MemberCodeGen extends CodeGen { } } + private void atArrayLength(ASTree expr) throws CompileError { + if (arrayDim == 0) + throw new CompileError(".length applied to a non array"); + + bytecode.addOpcode(ARRAYLENGTH); + exprType = INT; + arrayDim = 0; + } + /** * Generates bytecode for reading a field value. * It returns a fieldref_info index or zero if the field is a private @@ -837,7 +851,7 @@ public class MemberCodeGen extends CodeGen { ASTree oprand, Expr expr, boolean doDup) throws CompileError { - CtField f = fieldAccess(oprand); + CtField f = fieldAccess(oprand, false); boolean is_static = resultStatic; if (!is_static) bytecode.addOpcode(DUP); @@ -857,8 +871,12 @@ public class MemberCodeGen extends CodeGen { } /* This method also returns a value in resultStatic. + * + * @param acceptLength true if array length is acceptable */ - protected CtField fieldAccess(ASTree expr) throws CompileError { + protected CtField fieldAccess(ASTree expr, boolean acceptLength) + throws CompileError + { if (expr instanceof Member) { String name = ((Member)expr).get(); CtField f = null; @@ -905,6 +923,9 @@ public class MemberCodeGen extends CodeGen { if (exprType == CLASS && arrayDim == 0) f = resolver.lookupFieldByJvmName(className, (Symbol)e.oprand2()); + else if (acceptLength && arrayDim > 0 + && ((Symbol)e.oprand2()).get().equals("length")) + return null; // expr is an array length. else badLvalue(); -- 2.39.5