java/ch/wlkl/javaExamples/ExGeneric.java
package ch.wlkl.javaExamples;
import static ch.wlkl.wsh.Top.sTrc;
import static ch.wlkl.env.Env.implement;
import java.io.FileInputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import static ch.wlkl.wsh.Top.sTrc;
import java.util.Arrays;
/**
*
* @author walter
*/
public class ExGeneric <T, U> extends ClassLoader {
T myT = null;
U myU = null;
/** Creates a new instance of ExGenericf */
public ExGeneric(T my, U yo) {
myT = my;
myU = yo;
}
public String toString () {
return "exGeneric(" + asString(myT) + ", " + asString(myU) + ")";
}
public static String asString(Object o) {
return o == null ? "NULL:null" : o.getClass().getName() + ":" + o;
}
public static <X, Y> ExGeneric<X, Y> pair (X x, Y y) {
return new ExGeneric<X, Y> (x, y);
}
public static <X, Y> ExGeneric<Y, X> swap(ExGeneric<X, Y> o) {
return new ExGeneric<Y, X> (o.myU, o.myT);
}
public void crack(ExGeneric<String, Integer> par) {
System.out.println("crack(" + par +")");
};
public <X extends C, Y extends C> void crackC(ExGeneric<X,Y> par) {
System.out.println("crack(" + par +")");
};
public static <X> ExGeneric<X, X> dupl(X x) {
return pair(x, x);
}
public void run()
{
System.out.println("valueOf " + asString(null));
System.out.println("valueOf " + asString(123));
System.out.println("null = " + null);
System.out.println("start ExGeneric");
System.out.println(new ExGeneric <Double, String> (1234., "abcd"));
System.out.println(new ExGeneric <String, ExGeneric <Integer, Integer>>
("zwei", new ExGeneric <Integer, Integer> (56, 78)));
System.out.println(pair(pair(12, "zw�lf"), pair("zwei", pair("dreiDreiDrei", pair(3,33)))));
System.out.println(swap(pair(1, "zwei")));
System.out.println(swap(dupl(null)));
// crack(dupl(null)); => syntax cannot apply
System.out.println(swap(dupl(new C("@0"))));
crackC(dupl(new CD("@CD")));
// crack(dupl(new CD("CDeins"))); ==> syntax cannot find symbol....
}
public static <T> void pollution(Class<T> c, ArrayList<T> l) {
for (T o: l)
System.out.println("pollution<" + c + " l: " + o.getClass().toString() + " -> " + o);
}
public static void pollution() {
ArrayList<String> sl = new ArrayList<String>();
sl.add("eins");
sl.add("zwei");
ArrayList<?> el = (ArrayList<?>) sl;
ArrayList<Integer> il = (ArrayList<Integer>) el;
il.add(new Integer(123));
for (Object o: il)
System.out.println("ele of il: " + o.getClass().toString() + " -> " + o);
pollution(String.class, sl);
pollution(Integer.class, il);
for (String o: sl)
System.out.println("ele of sl: " + o.getClass().toString() + " -> " + o);
}
public static <R> R receiverCast(Object o) {
return (R) o;
}
public static void receiverCast() {
String s = receiverCast(null);
System.out.println("receveiverCast String - null " + s);
s = receiverCast("string");
System.out.println("receveiverCast String - string " + s);
s = receiverCast(123);
System.out.println("receveiverCast String - 123 " + s);
}
public static void arrayCast() {
// String [] s0 = {};
// System.out.println("s0");
// Object [] o0 = (Object[] ) s0;
// System.out.println("o0");
// Integer [] i0 = (Integer[] ) o0;
System.out.println("o0");
String [] sa = {"eins", "zwei"};
System.out.println("sa[0] = " + sa[0] + ' ' + sa[0].getClass() +", sa[1] = " + sa[1] + ' ' + sa[01].getClass());
Object [] oa = (Object[] ) sa;
System.out.println("oa[0] = " + oa[0] + ' ' + oa[0].getClass() +", oa[1] = " + oa[1] + ' ' + oa[01].getClass());
Integer [] ia = (Integer[] ) oa;
System.out.println("ia[0] = " + ia[0] + ' ' + ia[0].getClass() +", ia[1] = " + ia[1] + ' ' + ia[01].getClass());
Number[] na = new Number[2];
oa = na;
ia = (Integer[]) na;
}
public static <T> T[] toArray(T... eles) {
return eles;
}
public void compile() throws Exception {
String name = "eins";
String path = "E:\\java\\src\\" + name;
String [] args = {path + ".java"};
implement("java compiler" /*
int res = com.sun.tools.javac.Main.compile(args, new java.io.PrintWriter(System.out));
sTrc("compile rc " + res); */ );
FileInputStream f = new FileInputStream(path + ".class");
int len = f.available();
sTrc(path + " avaiable " + len);
byte[] byteCode = new byte[len+123];
sTrc("read " + f.read(byteCode) + " available after " + f.available());
f.close();
Class<? extends Object> cl = defineClass(name, byteCode, 0, len);
sTrc("defined class " + cl);
Object o = cl.newInstance();
sTrc("create object " + o);
Method m = cl.getMethod("say");
sTrc("found method " + m);
m.invoke(o);
m = cl.getMethod("sayStatic");
sTrc("found method " + m);
m.invoke(null);
}
public static void main(String [] arg)
{
Object[] a;
System.out.println("toArray(a, b) = " + (a = toArray("a", "b")).getClass() + ' ' + Arrays.deepToString(a));
System.out.println("toArray(1, 2) = " + (a = toArray(1, 2)).getClass() + ' ' + Arrays.deepToString(a));
System.out.println("toArray(1, 2., dr) = " + (a = toArray(1, 2., "dr")).getClass() + ' ' + Arrays.deepToString(a));
System.out.println("toArray(1, 2., dr, null) = " + (a = toArray(1, 2., "dr", null)).getClass() + ' ' + Arrays.deepToString(a));
arrayCast();
receiverCast();
pollution();
pair(0, '0').run();
}
class C {
public C(String v) {
val = v;
}
public String toString () {
return val;
}
String val;
}
class CD extends C {
public CD(String v) {
super(v);
}
}
class CE extends C {
public CE(String v) {
super(v);
}
}
}