Reflection Modifiers (Part 2)

by using Reflection API, we can actually see the content of the type so we can find out more information about its modifiers and its parent class in case if that was extended from a class or we can find out if that type is actually implemented an interface.

below is the same example I wrote in Part 1 Reflection, but this time we are extending an inner class from our inner class and then try to investigate further on that subclass instance. (I’ve highlighted the updated parts related to modifiers.)

Tip#1: you can see there are two ways to deal with modifiers, you can use BitWise operation or just using Modifier Class methods to verify and investigate on object instance details.


/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.navid.training.classtester;

import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Unknown_
*/
public class ClassTesting {

private static class MyInnerClass {

String name = "";
int code = 0;

public MyInnerClass(int code) {
this.code = code;
}

}

/**
* just extending this class to express the Class related methods leverage.
*/
private static final class MySubClass extends MyInnerClass implements Runnable {

public MySubClass(int code) {
super(code);
}

@Override
public void run() {
System.out.println("triggering and runnin this subclass as a thread.");
}

}

public void doWork(Object obj) {
Class<?> c = obj.getClass();
showClassInfo(c);
}

/**
* extracting and showing the class information.
*
* @param inputClass
*/
public void showClassInfo(Class<?> inputClass) {
System.out.println("SimpleName: " + inputClass.getSimpleName());
System.out.println("CanonicalName: " + inputClass.getCanonicalName());
System.out.println("Name: " + inputClass.getName());
System.out.println("TypeName: " + inputClass.getTypeName());
}

public static void main(String[] args) {

try {

//working with Class methods BASICS.
System.out.println("working with BASICS of CLASS methods.");
MyInnerClass innerClass = new MyInnerClass(12);
// Testing Innerclass instance
ClassTesting tester = new ClassTesting();
tester.doWork(innerClass);

//another test with a JDK Collection util package class
Map m = new HashMap<String, String>();
tester.doWork(m);
// Another test --
System.out.println("Using: Class.forName() -- it needs name and not choronical name!");
System.out.println("This may through \" java.lang.ClassNotFoundException\" - so to call inner classes you need something liek this "
+ "com.navid.training.classtester.ClassTesting$MyInnerClass");
Class<?> c = Class.forName("com.navid.training.classtester.ClassTesting$MyInnerClass");
tester.showClassInfo(c);

//Another test using class operator
System.out.println("Using: type literals for every Object - e.g: MyClass.class");
Class<?> c2 = MyInnerClass.class;
tester.showClassInfo(c2);

// create an isntance of MySubClass
System.out.println(" ------\n working on SUBCLASSES and Modifier in Class ...");
MySubClass sub = new MySubClass(20);
//its name
Class<?> itself = sub.getClass();
System.out.println("Class itself: " + itself.getName());
//its parent
Class<?> parent = itself.getSuperclass();
System.out.println("SupserClassName: " + parent.getName());
//its interfaces
Class<?>[] interfaces = itself.getInterfaces();
for (Class cc : interfaces) {
System.out.println("Interfaces: " + cc.getName());
}

int modifier = itself.getModifiers();
if (Modifier.isFinal(modifier)) {
System.out.println("its modifier is FINAL - using isFinal()");
}
if ((modifier & Modifier.FINAL) > 0) {
System.out.println("its modifier is FINAL - using BITWISE operation");
}
if (Modifier.isPrivate(modifier)) {
System.out.println("its modifier is PRIVATE");
}
if (Modifier.isProtected(modifier)) {
System.out.println("it's PROTECTED.");
}

// working with Methods and Fields in CLASS
System.out.println("Working with Methods and Fields by Class methods...");



} catch (ClassNotFoundException ex) {
Logger.getLogger(ClassTesting.class.getName()).log(Level.SEVERE, null, ex);
}
}

}

output is like this:

run:
SimpleName: MyInnerClass
CanonicalName: com.navid.training.classtester.ClassTesting.MyInnerClass
Name: com.navid.training.classtester.ClassTesting$MyInnerClass
TypeName: com.navid.training.classtester.ClassTesting$MyInnerClass
SimpleName: HashMap
CanonicalName: java.util.HashMap
Name: java.util.HashMap
TypeName: java.util.HashMap
Using: Class.forName() -- it needs name and not choronical name!
This may through " java.lang.ClassNotFoundException" - so to call inner classes you need something liek this com.navid.training.classtester.ClassTesting$MyInnerClass
SimpleName: MyInnerClass
CanonicalName: com.navid.training.classtester.ClassTesting.MyInnerClass
Name: com.navid.training.classtester.ClassTesting$MyInnerClass
TypeName: com.navid.training.classtester.ClassTesting$MyInnerClass
Using: type literals for every Object - e.g: MyClass.class
SimpleName: MyInnerClass
CanonicalName: com.navid.training.classtester.ClassTesting.MyInnerClass
Name: com.navid.training.classtester.ClassTesting$MyInnerClass
TypeName: com.navid.training.classtester.ClassTesting$MyInnerClass
 ------
 working on Subclasses
Class itself: com.navid.training.classtester.ClassTesting$MySubClass
SupserClassName: com.navid.training.classtester.ClassTesting$MyInnerClass
Interfaces: java.lang.Runnable
its modifier is FINAL - using isFinal()
its modifier is FINAL - using BITWISE operation
its modifier is PRIVATE
BUILD SUCCESSFUL (total time: 0 seconds)
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s