aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorchibash <chiba@javassist.org>2019-12-24 01:02:05 +0900
committerchibash <chiba@javassist.org>2019-12-24 01:02:05 +0900
commit150709141ca5cafa0be64638bbb724aa9879a15d (patch)
tree7bbb0123348e24f1d9b7393d452e509c5fcd6559 /src
parentafe124ffa5794c881c212fb1ebec51a554d0e332 (diff)
downloadjavassist-150709141ca5cafa0be64638bbb724aa9879a15d.tar.gz
javassist-150709141ca5cafa0be64638bbb724aa9879a15d.zip
fixes a bug involuved in Pull Request #294
Diffstat (limited to 'src')
-rw-r--r--src/main/javassist/compiler/MemberCodeGen.java4
-rw-r--r--src/main/javassist/compiler/Parser.java2
-rw-r--r--src/main/javassist/compiler/ast/ArrayInit.java17
-rw-r--r--src/test/javassist/JvstTest5.java16
4 files changed, 36 insertions, 3 deletions
diff --git a/src/main/javassist/compiler/MemberCodeGen.java b/src/main/javassist/compiler/MemberCodeGen.java
index f1ec84a5..48acd137 100644
--- a/src/main/javassist/compiler/MemberCodeGen.java
+++ b/src/main/javassist/compiler/MemberCodeGen.java
@@ -365,7 +365,7 @@ public class MemberCodeGen extends CodeGen {
sizeExpr.accept(this);
else
if (sizeExpr == null) {
- int s = init.length();
+ int s = init.size();
bytecode.addIconst(s);
}
else
@@ -414,7 +414,7 @@ public class MemberCodeGen extends CodeGen {
}
if (init != null) {
- int s = init.length();
+ int s = init.size();
ASTList list = init;
for (int i = 0; i < s; i++) {
bytecode.addOpcode(DUP);
diff --git a/src/main/javassist/compiler/Parser.java b/src/main/javassist/compiler/Parser.java
index 5290450c..ae73fb29 100644
--- a/src/main/javassist/compiler/Parser.java
+++ b/src/main/javassist/compiler/Parser.java
@@ -683,7 +683,7 @@ public final class Parser implements TokenId {
lex.get(); // '{'
if(lex.lookAhead() == '}'){
lex.get();
- return new ArrayInit(new IntConst(0,TokenId.IntConstant));
+ return new ArrayInit(null);
}
ASTree expr = parseExpression(tbl);
ArrayInit init = new ArrayInit(expr);
diff --git a/src/main/javassist/compiler/ast/ArrayInit.java b/src/main/javassist/compiler/ast/ArrayInit.java
index f992f426..0b87200e 100644
--- a/src/main/javassist/compiler/ast/ArrayInit.java
+++ b/src/main/javassist/compiler/ast/ArrayInit.java
@@ -25,10 +25,27 @@ public class ArrayInit extends ASTList {
/** default serialVersionUID */
private static final long serialVersionUID = 1L;
+ /**
+ * Constructs an object.
+ * @param firstElement maybe null when the initializer is <code>{}</code> (empty).
+ */
public ArrayInit(ASTree firstElement) {
super(firstElement);
}
+ /**
+ * Gets the number of the elements. Don't call {@link #length()}.
+ *
+ * @return the number of the elements.
+ */
+ public int size() {
+ int s = length();
+ if (s == 1 && head() == null)
+ return 0;
+ else
+ return s;
+ }
+
@Override
public void accept(Visitor v) throws CompileError { v.atArrayInit(this); }
diff --git a/src/test/javassist/JvstTest5.java b/src/test/javassist/JvstTest5.java
index 554538ae..ade41e8c 100644
--- a/src/test/javassist/JvstTest5.java
+++ b/src/test/javassist/JvstTest5.java
@@ -558,4 +558,20 @@ public class JvstTest5 extends JvstTestRoot {
Object obj = make(cc.getName());
assertEquals(71 + 22, invoke(obj, "run"));
}
+
+ // PR #294
+ public void testEmptyArrayInit() throws Exception {
+ CtClass cc = sloader.makeClass("test5.EmptyArrayInit");
+ CtMethod m = CtNewMethod.make("public int[] foo(){ int[] a = {}; return a; }", cc);
+ cc.addMethod(m);
+ CtMethod m2 = CtNewMethod.make("public int[] bar(){ int[] a = new int[]{}; return a; }", cc);
+ cc.addMethod(m2);
+ CtMethod m3 = CtNewMethod.make("public String[] baz(){ String[] a = { null }; return a; }", cc);
+ cc.addMethod(m3);
+ CtMethod m0 = CtNewMethod.make("public int run() { return foo().length + bar().length + baz().length; }", cc);
+ cc.addMethod(m0);
+ cc.writeFile();
+ Object obj = make(cc.getName());
+ assertEquals(1, invoke(obj, "run"));
+ }
}