Open dvanhorn opened 11 years ago
There is no way this can be fixed.
I examined what Java gives me with getClass() and all kinds of its variants and it is totally screwed up in the presence of generics.
This code shows you the pitfalls - all commented-out code throws null pointer exceptions.
I never tried to initialize an array this way. If you want to do it in the constructor, use the .asList method in the Arrays class:
ArrayList
-- Viera
import tester.*; import java.util.ArrayList;
class Examples {
void testGood(Tester t) {
ArrayList<Integer> is = new ArrayList<Integer>();
is.add(5);
ArrayList<Integer> js = new ArrayList<Integer>();
js.add(5);
t.checkExpect(is, js);
}
void testBad(Tester t) {
ArrayList<Integer> is = new ArrayList<Integer>() {{
add(5);
}};
ArrayList<Integer> js = new ArrayList<Integer>() {{
add(5);
}};
t.checkExpect(is, js);
}
ArrayList<Integer> is1 = new ArrayList<Integer>();
ArrayList<Integer> js1 = new ArrayList<Integer>();
ArrayList<Integer> is = new ArrayList<Integer>() {{
add(5);
}};
ArrayList<Integer> js = new ArrayList<Integer>() {{
add(5);
}};
void testBad2(Tester t) {
t.checkExpect(is, js);
System.out.println("is class name: " + is.getClass().getName());
System.out.println("js class name: " + is.getClass().getName());
System.out.println("is super class name: " +
is.getClass().getEnclosingClass().getName());
System.out.println("js super class name: " +
js.getClass().getEnclosingClass().getName());
/*
System.out.println("is declaring class name: " +
is.getClass().getDeclaringClass().getName());
System.out.println("js declaring class name: " +
js.getClass().getDeclaringClass().getName());
*/
/*
System.out.println("is super class name: " +
is.getClass().getSuperClass().getName());
System.out.println("js super class name: " +
is.getClass().getSuperClass().getName());
*/
is1.add(5);
js1.add(5);
t.checkExpect(is1, js1);
System.out.println("is1 class name: " + is.getClass().getName());
System.out.println("js1 class name: " + is.getClass().getName());
/*
System.out.println("is1 super class name: " +
is1.getClass().getEnclosingClass().getName());
System.out.println("js1 super class name: " +
js1.getClass().getEnclosingClass().getName());
*/
/*
System.out.println("is1 declaring class name: " +
is1.getClass().getDeclaringClass().getName());
System.out.println("js1 declaring class name: " +
js1.getClass().getDeclaringClass().getName());
*/
/*
System.out.println("is1 super class name: " +
is.getClass().getSuperClass().getName());
System.out.println("js1 super class name: " +
is.getClass().getSuperClass().getName());
*/
t.checkExpect((is instanceof Iterable), true);
t.checkExpect((is1 instanceof Iterable), true);
t.checkExpect((js instanceof Iterable), true);
t.checkExpect((js1 instanceof Iterable), true);
}
public static void main(String[] argv){
Examples e = new Examples();
Tester.runReport(e, true, true);
ArrayList<String> stooges =
new ArrayList<String>(Arrays.asList("Larry", "Moe", "Curly"));
System.out.println(stooges.get(0));
System.out.println(stooges.get(1));
System.out.println(stooges.get(2));
}
}
On Feb 27, 2013, at 9:36 AM, David Van Horn wrote:
This program demonstrates a possible bug in the tester library with array lists constructed using constructor blocks. The first test does not use constructor blocks and works as expected. The second gives equivalent definitions in terms of constructor blocks, but causes an error when tested.
Note that the reported actual and expected values are Examples(!) objects, not array lists.
javac -cp tester.jar TesterBug.java java -cp tester.jar: tester.Main Examples
Tests for the class: Examples
Tester Prima v.1.5.2.1
Tests defined in the class: Examples:
Examples:
new Examples:1()
Ran 2 tests. 1 test failed.
Failed test results:
Error in test number 2
tester.ErrorReport: Error trace: at Examples.testBad(TesterBug.java:32) actual: expected: new Examples$1:1(................................ new Examples$2:1( this.size = 1 this.size = 1 this.this$0 = this.this$0 = new Examples:2()) new Examples:2())
--- END OF TEST RESULTS --- import tester.*; import java.util.ArrayList;
class Examples {
void testGood(Tester t) { ArrayList<Integer> is = new ArrayList<Integer>(); is.add(5); ArrayList<Integer> js = new ArrayList<Integer>(); js.add(5); t.checkExpect(is, js); } void testBad(Tester t) { ArrayList<Integer> is = new ArrayList<Integer>() {{ add(5); }}; ArrayList<Integer> js = new ArrayList<Integer>() {{ add(5); }}; t.checkExpect(is, js); }
} — Reply to this email directly or view it on GitHub.
This program demonstrates a possible bug in the tester library with array lists constructed using constructor blocks. The first test does not use constructor blocks and works as expected. The second gives equivalent definitions in terms of constructor blocks, but causes an error when tested.
Note that the reported actual and expected values are Examples(!) objects, not array lists.
javac -cp tester.jar TesterBug.java
java -cp tester.jar: tester.Main Examples