diff options
author | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-25 05:08:05 +0000 |
---|---|---|
committer | chiba <chiba@30ef5769-5b8d-40dd-aea6-55b5d6557bb3> | 2005-08-25 05:08:05 +0000 |
commit | 9366fe08625ceb75692b53cdb9be19dff2ad56c6 (patch) | |
tree | 2333f5caf415378b259986177e39078616fc12e0 /src/main/javassist/compiler/MemberCodeGen.java | |
parent | 2d60b1690e44dd33553e28618c7c0b25e4b34d9f (diff) | |
download | javassist-9366fe08625ceb75692b53cdb9be19dff2ad56c6.tar.gz javassist-9366fe08625ceb75692b53cdb9be19dff2ad56c6.zip |
Array initializer supports and better annotation supports.
git-svn-id: http://anonsvn.jboss.org/repos/javassist/trunk@196 30ef5769-5b8d-40dd-aea6-55b5d6557bb3
Diffstat (limited to 'src/main/javassist/compiler/MemberCodeGen.java')
-rw-r--r-- | src/main/javassist/compiler/MemberCodeGen.java | 70 |
1 files changed, 59 insertions, 11 deletions
diff --git a/src/main/javassist/compiler/MemberCodeGen.java b/src/main/javassist/compiler/MemberCodeGen.java index 88023202..ae586d68 100644 --- a/src/main/javassist/compiler/MemberCodeGen.java +++ b/src/main/javassist/compiler/MemberCodeGen.java @@ -18,6 +18,7 @@ package javassist.compiler; import javassist.*; import javassist.bytecode.*; import javassist.compiler.ast.*; + import java.util.ArrayList; /* Code generator methods depending on javassist.* classes. @@ -29,8 +30,7 @@ public class MemberCodeGen extends CodeGen { protected boolean resultStatic; - public MemberCodeGen(Bytecode b, CtClass cc, ClassPool cp) - { + public MemberCodeGen(Bytecode b, CtClass cc, ClassPool cp) { super(b); resolver = new MemberResolver(cp); thisClass = cc; @@ -239,26 +239,46 @@ public class MemberCodeGen extends CodeGen { } public void atNewArrayExpr(NewExpr expr) throws CompileError { - if (expr.getInitializer() != null) - throw new CompileError("array initializer is not supported"); - int type = expr.getArrayType(); ASTList size = expr.getArraySize(); ASTList classname = expr.getClassName(); + ArrayInit init = expr.getInitializer(); if (size.length() > 1) { + if (init != null) + throw new CompileError( + "sorry, multi-dimensional array initializer " + + "for new is not supported"); + atMultiNewArray(type, classname, size); return; } - size.head().accept(this); - exprType = type; - arrayDim = 1; + ASTree sizeExpr = size.head(); + atNewArrayExpr2(type, sizeExpr, Declarator.astToClassName(classname, '/'), init); + } + + private void atNewArrayExpr2(int type, ASTree sizeExpr, + String jvmClassname, ArrayInit init) throws CompileError { + if (init == null) + if (sizeExpr == null) + throw new CompileError("no array size"); + else + sizeExpr.accept(this); + else + if (sizeExpr == null) { + int s = init.length(); + bytecode.addIconst(s); + } + else + throw new CompileError("unnecessary array size specified for new"); + + String elementClass; if (type == CLASS) { - className = resolveClassName(classname); - bytecode.addAnewarray(MemberResolver.jvmToJavaName(className)); + elementClass = resolveClassName(jvmClassname); + bytecode.addAnewarray(MemberResolver.jvmToJavaName(elementClass)); } else { - className = null; + elementClass = null; int atype = 0; switch (type) { case BOOLEAN : @@ -293,12 +313,40 @@ public class MemberCodeGen extends CodeGen { bytecode.addOpcode(NEWARRAY); bytecode.add(atype); } + + if (init != null) { + int s = init.length(); + ASTList list = init; + for (int i = 0; i < s; i++) { + bytecode.addOpcode(DUP); + bytecode.addIconst(i); + list.head().accept(this); + if (!isRefType(type)) + atNumCastExpr(exprType, type); + + bytecode.addOpcode(getArrayWriteOp(type, 0)); + list = list.tail(); + } + } + + exprType = type; + arrayDim = 1; + className = elementClass; } private static void badNewExpr() throws CompileError { throw new CompileError("bad new expression"); } + protected void atArrayVariableAssign(ArrayInit init, int varType, + int varArray, String varClass) throws CompileError { + atNewArrayExpr2(varType, null, varClass, init); + } + + public void atArrayInit(ArrayInit init) throws CompileError { + throw new CompileError("array initializer is not supported"); + } + protected void atMultiNewArray(int type, ASTList classname, ASTList size) throws CompileError { |