Some more tests for method scoped and anonymous classes

git-svn-id: https://svn.code.sf.net/p/jode/code/trunk@1188 379699f6-c40d-0410-875b-85095c16579e
branch_1_1
jochen 25 years ago
parent b06f5a9c12
commit f7fced3615
  1. 34
      jode/test/AnonymousClass.java
  2. 19
      jode/test/AnonymousJavac.java
  3. 124
      jode/test/MethodScopedClass.java
  4. 89
      jode/test/PrivateHideTest.java

@ -42,17 +42,15 @@ public class AnonymousClass {
}
///#ifndef JAVAC11
///#ifndef JAVAC12
///#ifndef JIKES
/// Hello(int i) {
/// this("If you find a compiler that can compile this,"
/// +" please comment this out and tell me if "
/// +"decompilation works.\n"
/// +"jikes 0.47, javac 1.2 are both broken!");
/// }
///#ifndef JIKES0
Hello(int i) {
this("This can only be compiled correctly"
+" by a recent jikes");
}
///#endif
///#endif
///#endif
public void hello() {
private void hello() {
this.hashCode();
Inner.this.hashCode();
AnonymousClass.this.hashCode();
@ -64,12 +62,12 @@ public class AnonymousClass {
final Object o = new Object() {
int blah = 5;
Hello hii = hi;
{
System.err.println("Anonymous Constructor speaking");
}
Hello hii = hi;
public String toString() {
this.hii.hello();
hi.hello();
@ -90,7 +88,7 @@ public class AnonymousClass {
///#ifndef JAVAC12
Hello blah = new Hello("Hello World") {
public void hello() {
System.err.println("overwritten");
System.err.println("overwritten" + dblVar + hi);
}
};
///#endif
@ -107,6 +105,19 @@ public class AnonymousClass {
}
}
class Huhu extends Hello {
public Huhu(String str) {
super(str);
}
public Huhu(int i) {
}
public Huhu() {
super("What's up");
}
}
Vector v = new Vector(hi.var, new Inner("blah").var) {
public String newMethod() {
return super.toString();
@ -114,6 +125,7 @@ public class AnonymousClass {
};
Hi hu = new Hi();
new Huhu(1);
}
Inner (String str) {

@ -41,14 +41,7 @@ public class AnonymousJavac {
System.err.println("construct: "+info);
}
// Hello(int i) {
// this("If you find a compiler that can compile this,"
// +" please comment this out and tell me if "
// +"decompilation works.\n"
// +"jikes 0.47, javac 1.2 are both broken!");
// }
public void hello() {
private void hello() {
this.hashCode();
Inner.this.hashCode();
Inner.this.var = var;
@ -84,11 +77,6 @@ public class AnonymousJavac {
return o.toString();
}
};
// Hello blah = new Hello("Hello World") {
// public void hello() {
// System.err.println("overwritten");
// }
// };
Inner blub1 = new Inner("Inner param") {
Hello hii = hi;
@ -163,11 +151,6 @@ public class AnonymousJavac {
return o.toString();
}
};
// Hello blah = new Hello("Hello World") {
// public void hello() {
// System.err.println("overwritten");
// }
// };
Inner blub1 = new Inner("Inner param") {
public void test() {

@ -0,0 +1,124 @@
/* MethodScopedClass Copyright (C) 1999 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
*/
import java.util.Vector;
public class MethodScopedClass {
int var = 3;
public void test1() {
final long longVar = 5;
final int intVar = var;
final double dblVar = 3;
final float fooFloat = 5;
final float barFloat = 7;
class Hello {
int var = (int) longVar;
{
System.err.println("all constructors");
}
Hello(float f, String info) {
System.err.println("construct: "+info+" "+f);
}
Hello(float f) {
this(f, "default");
}
public void hello() {
System.err.println("HelloWorld: "+dblVar+" "+intVar);
}
};
/* This test checks if the variables longVar, intVar and dblVar
* can be detected correctly as inner values. The first parameter
* of the Hello constructor can't be an outer value, because bar
* uses a different value for this.
*/
class foo {
foo() {
new Hello(fooFloat);
}
}
class bar {
bar() {
new Hello(barFloat, "bar");
}
}
new foo();
new bar();
}
// public void test2() {
// final long longVar = 5;
// final int intVar = var;
// final double dblVar = 3;
// final float barFloat = 7;
// class Hello {
// int var = (int) longVar;
// {
// System.err.println("all constructors");
// }
// Hello(int i, String info) {
// System.err.println("construct: "+info);
// }
// Hello(int i) {
// this(i, "This can only be compiled correctly"
// +" by a recent jikes");
// }
// public void hello() {
// System.err.println("HelloWorld: "+dblVar+" "+intVar);
// }
// };
// /* Similar to the test above. But this time foo is given barFloat
// * as parameter.
// */
// class foo {
// foo(int param) {
// new Hello(param);
// }
// }
// class bar {
// bar() {
// new Hello(barFloat, "bar");
// }
// test() {
// new foo(fooFloat);
// }
// }
// new foo(barFloat);
// new bar();
// }
}

@ -0,0 +1,89 @@
/**
* This class tests accesses to private methods and fields, that can be
* accessed via a cast.
*
* The test class has some problems, that no compiler will produce
* correct code. When all constructors are declared as private all
* compilers won't even compile it.
*
* I wish there would be a clear document saying what constructs are
* allowed and a compiler that would compile them all...
*/
public class PrivateHideTest {
public static class Test {
private String id;
Test(int val) {
id = "Test"+val;
}
private void a(String descr) {
System.err.println(descr+" = "+id);
}
class Mid extends Test {
private String id;
Mid(int val) {
super(val);
id = "Mid"+val;
}
private void a(String descr) {
System.err.println(descr+" = "+id);
}
private Test getTestThis() {
return Test.this;
}
class Inner extends Mid {
private String id;
Inner(int val, Test midOuter) {
midOuter.super(val);
id = "Inner"+val;
}
private void a(String descr) {
System.err.println(descr+" = "+id);
}
private void methodTest() {
this.a("this");
super.a("super");
((Mid) this).a("(Mid) this");
((Test) this).a("(Test) this");
Mid.this.a("Mid.this");
((Test) Mid.this).a("(Test) Mid.this");
Test.this.a("Test.this");
((Mid) this).getTestThis().a("((Mid) this).getTestThis()");
Mid.this.getTestThis().a("Mid.this.getTestThis()");
}
private void fieldTest() {
System.err.println("this = "+this.id);
System.err.println("(Mid) this = " + ((Mid) this).id);
System.err.println("(Test) this = " + ((Test) this).id);
System.err.println("Mid.this = " + Mid.this.id);
System.err.println("(Test) Mid.this = " + ((Test) Mid.this).id);
System.err.println("Test.this = " + Test.this.id);
System.err.println("((Mid) this).getTestThis() = " + ((Mid)this).getTestThis().id);
System.err.println("Mid.this.getTestThis() = " + Mid.this.getTestThis().id);
}
}
}
}
public static void main(String[] argv) {
Test.Mid.Inner inner =
new Test(1).new Mid(2).new Inner(3, new Test(4).new Mid(5));
inner.methodTest();
System.err.println("--");
inner.fieldTest();
((Test.Mid)inner).a("(Test.Mid) inner");
}
}
Loading…
Cancel
Save