Skip to content

Commit 376540e

Browse files
reflection api using and isInstance vs instanceof
1 parent 816a97f commit 376540e

File tree

6 files changed

+325
-0
lines changed

6 files changed

+325
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
public class IsInstanceVsInstanceof {
4+
5+
/*
6+
isInstance -> class is decided at run time. (late binding) e.g.
7+
instanceof -> you need to know the exact class at compile time.
8+
*/
9+
10+
public static void main(String[] args) {
11+
12+
TestExampleCls obj1 = new TestExampleCls(1,"A");
13+
14+
Integer i = 4;
15+
String s = "Hello";
16+
17+
if(obj1 instanceof TestExampleCls)
18+
{
19+
System.out.println("Yes TestExampleCls");
20+
}
21+
22+
// it is error in compile time -> IDE warn -> Cannot cast Integer
23+
// if(obj1 instanceof Integer)
24+
// {
25+
//
26+
// }
27+
28+
29+
// it is error in compile time -> IDE warn -> Cannot cast TestExampleCls
30+
// if(i instanceof TestExampleCls)
31+
// {
32+
//
33+
// }
34+
35+
try
36+
{
37+
boolean intComp = isInstanceTest(i,"java.lang.Integer");
38+
boolean strComp = isInstanceTest(s,"java.lang.String");
39+
40+
System.out.println(" intComp : " + intComp + " strComp :" + strComp);
41+
}
42+
catch (ClassNotFoundException e)
43+
{
44+
e.printStackTrace();
45+
}
46+
47+
48+
}
49+
50+
private static boolean isInstanceTest(Object obj, String str) throws ClassNotFoundException {
51+
52+
return Class.forName(str).isInstance(obj);
53+
}
54+
55+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
4+
import java.lang.reflect.Constructor;
5+
import java.lang.reflect.Field;
6+
import java.lang.reflect.InvocationTargetException;
7+
import java.lang.reflect.Method;
8+
9+
class TestClass
10+
{
11+
12+
private String stringValue;
13+
14+
public TestClass() { stringValue = "TestClass"; }
15+
16+
17+
public void testMethod1() {
18+
System.out.println("TestClass, public testMethod1 method call and private string value : " + stringValue);
19+
}
20+
21+
22+
public void testMethod2(int n) {
23+
System.out.println("TestClass, public testMethod2 method call with arg : " + n);
24+
}
25+
26+
private void testMethod3() {
27+
System.out.println("TestClass, private testMethod3 call");
28+
}
29+
}
30+
31+
public class ReflectionExample1 {
32+
33+
34+
public static void main(String[] args) {
35+
36+
TestClass testClass = new TestClass();
37+
38+
Class cls = testClass.getClass();
39+
40+
System.out.println("class name : " + cls.getName());
41+
42+
System.out.println("--------------------------");
43+
try
44+
{
45+
Constructor constructor = cls.getConstructor();
46+
47+
System.out.println(" class constructor name : " + constructor.getName());
48+
}
49+
catch (NoSuchMethodException e)
50+
{
51+
System.out.println("getConstructor exception : " + e.getMessage());
52+
}
53+
54+
System.out.println("--------------------------");
55+
56+
//methods of class
57+
Method[] methods = cls.getMethods();
58+
59+
for (Method method:methods)
60+
{
61+
System.out.println("class method : " + method.getName());
62+
}
63+
64+
System.out.println("--------------------------");
65+
66+
try
67+
{
68+
// NoSuchMethodException
69+
Method testMethod1Call = cls.getDeclaredMethod("testMethod1");
70+
71+
//IllegalAccessException and InvocationTargetException
72+
testMethod1Call.invoke(testClass);
73+
}
74+
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
75+
{
76+
System.out.println(" error : " + e.getMessage());
77+
}
78+
79+
System.out.println("--------------------------");
80+
81+
try
82+
{
83+
// NoSuchMethodException
84+
Method testMethod2Call = cls.getDeclaredMethod("testMethod2", int.class);
85+
86+
//IllegalAccessException and InvocationTargetException
87+
testMethod2Call.invoke(testClass, 10);
88+
}
89+
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
90+
{
91+
92+
System.out.println(" error : " + e.getMessage());
93+
}
94+
95+
System.out.println("--------------------------");
96+
97+
try
98+
{
99+
//get property -> NoSuchFieldException
100+
Field field = cls.getDeclaredField("stringValue");
101+
102+
field.setAccessible(true);
103+
104+
//new value set property -> IllegalAccessException
105+
field.set(testClass, "TestClass field set new value");
106+
}
107+
catch (NoSuchFieldException | IllegalAccessException e)
108+
{
109+
System.out.println("error : " + e.getMessage());
110+
}
111+
112+
System.out.println("--------------------------");
113+
114+
try
115+
{
116+
Method testMethod3Call = cls.getDeclaredMethod("testMethod3");
117+
118+
testMethod3Call.setAccessible(true);
119+
120+
testMethod3Call.invoke(testClass);
121+
}
122+
catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e)
123+
{
124+
System.out.println("error : " + e.getMessage());
125+
}
126+
127+
128+
}
129+
130+
131+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.util.Arrays;
5+
6+
public class ReflectionExample2 {
7+
8+
class Test1 {
9+
10+
private String str = "Test1 str";
11+
12+
public Test1() {
13+
14+
}
15+
}
16+
17+
private static class Test2 {
18+
19+
private class Test2InnerClass {
20+
21+
private int i;
22+
23+
private Test2InnerClass(int i) {
24+
this.i = i;
25+
}
26+
}
27+
}
28+
29+
public static void main(String[] args) throws NoSuchMethodException {
30+
31+
Class<Test2.Test2InnerClass> c = Test2.Test2InnerClass.class;
32+
33+
Constructor<Test2.Test2InnerClass> constructor = c.getDeclaredConstructor(Test2.class, int.class);
34+
35+
System.out.println(constructor);
36+
System.out.println(constructor.getName());
37+
System.out.println(Arrays.toString(constructor.getParameters()));
38+
39+
}
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
import java.lang.reflect.Constructor;
4+
import java.lang.reflect.InvocationTargetException;
5+
6+
public class ReflectionExample3 {
7+
8+
/*
9+
getConstructor() -> public Constructor
10+
getDeclaredConstructor -> any acess Constructor (public,private,protected )
11+
*/
12+
13+
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, ClassNotFoundException {
14+
15+
16+
Class testclz = Class.forName("tr.salkan.code.java.pure.examples.reflection.TestReflectionClass");
17+
18+
System.out.println(testclz);
19+
Constructor<?> constructor = testclz.getConstructor();
20+
21+
System.out.println(constructor);
22+
23+
Class[] parameterType = new Class[1];
24+
parameterType[0] = int.class;
25+
26+
System.out.println(TestReflectionClass.class.getDeclaredConstructor(parameterType));
27+
28+
Constructor<TestReflectionClass> constructorParam = testclz.getDeclaredConstructor(new Class[] {int.class});
29+
30+
System.out.println(constructorParam);
31+
32+
Constructor[] constructors = testclz.getDeclaredConstructors();
33+
34+
System.out.println("----------------------------------------");
35+
36+
for (Constructor c : constructors) {
37+
38+
String name = c.getName();
39+
System.out.println("Constructor name= " + name);
40+
41+
Class[] paramterTypes = c.getParameterTypes();
42+
for (Class p : paramterTypes) {
43+
System.out.println("Param type name = " + p.getName());
44+
}
45+
System.out.println("----------------------------------------");
46+
}
47+
48+
49+
}
50+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
public class TestExampleCls {
4+
5+
private int v1;
6+
private String v2;
7+
8+
public TestExampleCls(int v1, String v2) {
9+
this.v1 = v1;
10+
this.v2 = v2;
11+
}
12+
13+
public int getV1() {
14+
return v1;
15+
}
16+
17+
public void setV1(int v1) {
18+
this.v1 = v1;
19+
}
20+
21+
public String getV2() {
22+
return v2;
23+
}
24+
25+
public void setV2(String v2) {
26+
this.v2 = v2;
27+
}
28+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tr.salkan.code.java.pure.examples.reflection;
2+
3+
public class TestReflectionClass {
4+
5+
public TestReflectionClass() {
6+
7+
System.out.println("TestReflectionClass default constructor");
8+
}
9+
10+
public TestReflectionClass(int a){
11+
System.out.println("TestReflectionClass Constructor int : "+a);
12+
}
13+
14+
private TestReflectionClass(String s){
15+
System.out.println("TestReflectionClass Constructor string : "+s);
16+
}
17+
18+
protected TestReflectionClass(Long l){
19+
System.out.println("TestReflectionClass Constructor Long : "+l);
20+
}
21+
}

0 commit comments

Comments
 (0)