Skip to content

Commit 3dafe09

Browse files
committed
[add] add examples of type info
1 parent b1bdbe1 commit 3dafe09

File tree

9 files changed

+333
-0
lines changed

9 files changed

+333
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
public class BoundedClassReferences {
4+
public static void main(String[] args) {
5+
Class<? extends Number> bounded = int.class;
6+
bounded = double.class;
7+
bounded = Number.class;
8+
// Or anything else derived from Number.
9+
}
10+
} ///:~
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
class Building {
4+
}
5+
6+
class House extends Building {
7+
}
8+
9+
public class ClassCasts {
10+
public static void main(String[] args) {
11+
Building b = new House();
12+
Class<House> houseType = House.class;
13+
House h = houseType.cast(b);
14+
h = (House) b; // ... or just do this.
15+
16+
try {
17+
Class<House> hClass = House.class;
18+
//Produces exact type
19+
House house = hClass.newInstance();
20+
Class<? super House> up = hClass.getSuperclass();
21+
// won't compile:
22+
//Class<Building> up2 = hClass.getSuperclass();
23+
24+
//Only produces Object
25+
Object obj = up.newInstance();
26+
27+
System.out.println(house);
28+
System.out.println(obj);
29+
} catch (Exception e) {
30+
e.printStackTrace();
31+
}
32+
}
33+
} ///:~
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
import java.util.Random;
4+
5+
/**
6+
* 1.仅使用.class语法来获得对类的引用不会引发初始化
7+
* 2."编译器常量",无需类初始化即可读取
8+
* 3.static final域不一定是编译器常量,static非final域一定不是
9+
*/
10+
public class ClassInitialization {
11+
public static Random rand = new Random(47);
12+
13+
public static void main(String[] args) throws Exception {
14+
Class initable = Initable.class;
15+
System.out.println("After creating Initable ref");
16+
// Does not trigger initialization:
17+
System.out.println(Initable.staticFinal);
18+
// Does trigger initialization:
19+
System.out.println(Initable.staticFinal2);
20+
// Does trigger initialization:
21+
System.out.println(Initable2.staticNonFinal);
22+
Class initable3 = Class.forName("com.brianway.learning.java.base.typeinfo.Initable3");
23+
System.out.println("After creating Initable3 ref");
24+
System.out.println(Initable3.staticNonFinal);
25+
}
26+
}
27+
28+
class Initable {
29+
static final int staticFinal = 47;
30+
static final int staticFinal2 =
31+
ClassInitialization.rand.nextInt(1000);
32+
33+
static {
34+
System.out.println("Initializing Initable");
35+
}
36+
}
37+
38+
class Initable2 {
39+
static int staticNonFinal = 147;
40+
41+
static {
42+
System.out.println("Initializing Initable2");
43+
}
44+
}
45+
46+
class Initable3 {
47+
static int staticNonFinal = 74;
48+
49+
static {
50+
System.out.println("Initializing Initable3");
51+
}
52+
}
53+
54+
/* Output:
55+
After creating Initable ref
56+
47
57+
Initializing Initable
58+
258
59+
Initializing Initable2
60+
147
61+
Initializing Initable3
62+
After creating Initable3 ref
63+
74
64+
*///:~
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
/**
4+
* The difference between instanceof and class.
5+
*/
6+
public class FamilyVsExactType {
7+
static void test(Object x) {
8+
System.out.println("Testing x of type " + x.getClass());
9+
System.out.println("x instanceof Base " + (x instanceof Base));
10+
System.out.println("x instanceof Derived " + (x instanceof Derived));
11+
System.out.println("Base.isInstance(x) " + Base.class.isInstance(x));
12+
System.out.println("Derived.isInstance(x) "
13+
+ Derived.class.isInstance(x));
14+
System.out.println("x.getClass() == Base.class "
15+
+ (x.getClass() == Base.class));
16+
System.out.println("x.getClass() == Derived.class "
17+
+ (x.getClass() == Derived.class));
18+
System.out.println("x.getClass().equals(Base.class)) "
19+
+ (x.getClass().equals(Base.class)));
20+
System.out.println("x.getClass().equals(Derived.class)) "
21+
+ (x.getClass().equals(Derived.class)));
22+
}
23+
24+
public static void main(String[] args) {
25+
test(new Base());
26+
test(new Derived());
27+
}
28+
}
29+
30+
class Base {
31+
}
32+
33+
class Derived extends Base {
34+
}
35+
/* Output:
36+
Testing x of type class typeinfo.Base
37+
x instanceof Base true
38+
x instanceof Derived false
39+
Base.isInstance(x) true
40+
Derived.isInstance(x) false
41+
x.getClass() == Base.class true
42+
x.getClass() == Derived.class false
43+
x.getClass().equals(Base.class)) true
44+
x.getClass().equals(Derived.class)) false
45+
Testing x of type class typeinfo.Derived
46+
x instanceof Base true
47+
x instanceof Derived true
48+
Base.isInstance(x) true
49+
Derived.isInstance(x) true
50+
x.getClass() == Base.class false
51+
x.getClass() == Derived.class true
52+
x.getClass().equals(Base.class)) false
53+
x.getClass().equals(Derived.class)) true
54+
*///:~
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class CountedInteger {
7+
private static long counter;
8+
private final long id = counter++;
9+
10+
public String toString() {
11+
return Long.toString(id);
12+
}
13+
}
14+
15+
public class FilledList<T> {
16+
private Class<T> type;
17+
18+
public FilledList(Class<T> type) {
19+
this.type = type;
20+
}
21+
22+
public List<T> create(int nElements) {
23+
List<T> result = new ArrayList<T>();
24+
try {
25+
for (int i = 0; i < nElements; i++)
26+
result.add(type.newInstance());
27+
} catch (Exception e) {
28+
throw new RuntimeException(e);
29+
}
30+
return result;
31+
}
32+
33+
public static void main(String[] args) {
34+
FilledList<CountedInteger> fl =
35+
new FilledList<CountedInteger>(CountedInteger.class);
36+
System.out.println(fl.create(15));
37+
}
38+
}
39+
/* Output:
40+
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
41+
*///:~
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
public class GenericClassReferences {
4+
public static void main(String[] args) {
5+
6+
Class<Integer> genericIntClass = int.class;
7+
genericIntClass = Integer.class; // Same thing
8+
9+
Class intClass = int.class;
10+
intClass = double.class;
11+
// genericIntClass = double.class; // Illegal
12+
13+
System.out.println(int.class == Integer.class);
14+
}
15+
} ///:~
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.brianway.learning.java.base.typeinfo;//: typeinfo/SimpleDynamicProxy.java
2+
3+
import java.lang.reflect.InvocationHandler;
4+
import java.lang.reflect.Method;
5+
import java.lang.reflect.Proxy;
6+
7+
class DynamicProxyHandler implements InvocationHandler {
8+
private Object proxied;
9+
10+
public DynamicProxyHandler(Object proxied) {
11+
this.proxied = proxied;
12+
}
13+
14+
public Object
15+
invoke(Object proxy, Method method, Object[] args)
16+
throws Throwable {
17+
System.out.println("**** proxy: " + proxy.getClass().getSimpleName() +
18+
", method: " + method + ", args: " + args);
19+
if (args != null) {
20+
for (Object arg : args)
21+
System.out.println(" " + arg);
22+
}
23+
return method.invoke(proxied, args);
24+
}
25+
}
26+
27+
class SimpleDynamicProxy {
28+
public static void consumer(Interface iface) {
29+
iface.doSomething();
30+
iface.somethingElse("bonobo");
31+
}
32+
33+
public static void main(String[] args) {
34+
RealObject real = new RealObject();
35+
consumer(real);
36+
// Insert a proxy and call again:
37+
Interface proxy = (Interface) Proxy.newProxyInstance(
38+
Interface.class.getClassLoader(),
39+
new Class[] {Interface.class},
40+
new DynamicProxyHandler(real));
41+
consumer(proxy);
42+
}
43+
}
44+
/* Output: (95% match)
45+
doSomething
46+
somethingElse bonobo
47+
**** proxy: $Proxy0, method: public abstract void com.brianway.learning.java.base.typeinfo.Interface.doSomething(), args: null
48+
doSomething
49+
**** proxy: $Proxy0, method: public abstract void com.brianway.learning.java.base.typeinfo.Interface.somethingElse(java.lang.String), args: [Ljava.lang.Object;@d716361
50+
bonobo
51+
somethingElse bonobo
52+
*///:~
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
interface Interface {
4+
void doSomething();
5+
6+
void somethingElse(String arg);
7+
}
8+
9+
class RealObject implements Interface {
10+
public void doSomething() {
11+
System.out.println("doSomething");
12+
}
13+
14+
public void somethingElse(String arg) {
15+
System.out.println("somethingElse " + arg);
16+
}
17+
}
18+
19+
class SimpleProxy implements Interface {
20+
private Interface proxied;
21+
22+
public SimpleProxy(Interface proxied) {
23+
this.proxied = proxied;
24+
}
25+
26+
public void doSomething() {
27+
System.out.println("SimpleProxy doSomething");
28+
proxied.doSomething();
29+
}
30+
31+
public void somethingElse(String arg) {
32+
System.out.println("SimpleProxy somethingElse " + arg);
33+
proxied.somethingElse(arg);
34+
}
35+
}
36+
37+
class SimpleProxyDemo {
38+
public static void consumer(Interface iface) {
39+
iface.doSomething();
40+
iface.somethingElse("bonobo");
41+
}
42+
43+
public static void main(String[] args) {
44+
consumer(new RealObject());
45+
consumer(new SimpleProxy(new RealObject()));
46+
}
47+
}
48+
49+
/* Output:
50+
doSomething
51+
somethingElse bonobo
52+
SimpleProxy doSomething
53+
doSomething
54+
SimpleProxy somethingElse bonobo
55+
somethingElse bonobo
56+
*///:~
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.brianway.learning.java.base.typeinfo;
2+
3+
public class WildcardClassReferences {
4+
public static void main(String[] args) {
5+
Class<?> intClass = int.class;
6+
intClass = double.class;
7+
}
8+
} ///:~

0 commit comments

Comments
 (0)