Skip to content

Commit

Permalink
fixes a bug involuved in Pull Request #294
Browse files Browse the repository at this point in the history
  • Loading branch information
chibash committed Dec 23, 2019
1 parent afe124f commit 1507091
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Readme.html
Expand Up @@ -283,7 +283,7 @@ <h2>Changes</h2>

<p>-version 3.27
<ul>
<li>GitHub Issue #271 (PR #279), #280 (PR #281), and #282.
<li>GitHub Issue #271 (PR #279), #280 (PR #281), #282, and PR #294.
</ul>

<p>-version 3.26 on October 3, 2019
Expand Down
Binary file modified javassist.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions src/main/javassist/compiler/MemberCodeGen.java
Expand Up @@ -365,7 +365,7 @@ private void atNewArrayExpr2(int type, ASTree sizeExpr,
sizeExpr.accept(this);
else
if (sizeExpr == null) {
int s = init.length();
int s = init.size();
bytecode.addIconst(s);
}
else
Expand Down Expand Up @@ -414,7 +414,7 @@ private void atNewArrayExpr2(int type, ASTree sizeExpr,
}

if (init != null) {
int s = init.length();
int s = init.size();
ASTList list = init;
for (int i = 0; i < s; i++) {
bytecode.addOpcode(DUP);
Expand Down
2 changes: 1 addition & 1 deletion src/main/javassist/compiler/Parser.java
Expand Up @@ -683,7 +683,7 @@ private ArrayInit parseArrayInitializer(SymbolTable tbl)
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);
Expand Down
17 changes: 17 additions & 0 deletions src/main/javassist/compiler/ast/ArrayInit.java
Expand Up @@ -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); }

Expand Down
16 changes: 16 additions & 0 deletions src/test/javassist/JvstTest5.java
Expand Up @@ -558,4 +558,20 @@ public void testRedundantInsertAfter() throws Exception {
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"));
}
}

0 comments on commit 1507091

Please sign in to comment.