snippetjournal

– notes, snippet codes, and my journal

Example Basic Reflection In Java

leave a comment »

Apa itu reflection, silahkan baca di link berikut😀
http://docs.oracle.com/javase/tutorial/reflect/index.html

http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful

konsep singkat dari reflection yang saya pahami adalah dengan reflection kita dapat memanggil suatu method pada class lain dan pada package tertentu, sifat reflection adalah dinamis.

Dengan reflection kita dapat membuat suatu method pada class lain dan kemudian memanggil suatu method dengan “less codes”.

Pada reflection parameter yang “dilempar” ketika memanggil suatu method bisa kita kendalikan jenis object nya.

Apa yang saya rasakan ketika memakai reflection adalah susunan class class yang saya pakai menjadi lebih terstruktur. dan kemudahan dalam memanggil method method pada class lain.

berikut ini snippet codes untuk example reflection yang saya buat.

ReflectApp.java


package ASN.Coders.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReflectApp{

public static Map callBusinessClass(String packageClassName, String methodName, Object object) throws InstantiationException, IllegalAccessException {
 Class cls;
 Class[] paramPassMap = new Class[1];
 paramPassMap[0] = Map.class;

 Class[] paramPassList = new Class[1];
 paramPassList[0] = List.class;

 Class noparams[] = {};

 try {
 cls = Class.forName(packageClassName);
 Object obj = cls.newInstance();

 if(object != null){
 if(object instanceof Map){
 Method method = cls.getDeclaredMethod(methodName, paramPassMap);
 method.invoke(obj, object);
 }else if(object instanceof List){
 Method method = cls.getDeclaredMethod(methodName, paramPassList);
 method.invoke(obj, object);
 }else{
 System.out.println("Paramater Unknown . . . .");
 }
 }else{
 Method method = cls.getDeclaredMethod(methodName, noparams);
 method.invoke(obj, null);
 }

 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 } catch (SecurityException e) {
 e.printStackTrace();
 } catch (NoSuchMethodException e) {
 e.printStackTrace();
 } catch (IllegalArgumentException e) {
 e.printStackTrace();
 } catch (InvocationTargetException e) {
 e.printStackTrace();
 }

 return null;

 }

}

AppTest.java

package ASN.Coders.reflection;

import java.util.List;
import java.util.Map;

public class AppTest {

private int counter;

public void printIt() {
 System.out.println("printIt() no param");
 }

public void printItString(String temp) {
 System.out.println("printIt() with param String : " + temp);
 }

public void printItInt(int temp) {
 System.out.println("printIt() with param int : " + temp);
 }

public void setCounter(int counter) {
 this.counter = counter;
 System.out.println("setCounter() set counter to : " + counter);
 }

public void printCounter() {
 System.out.println("printCounter() : " + this.counter);
 }

public Map getQuery(Map map) {
 System.out.println("map Contents" + map);
 return map;
 }

public List getQuery(List list) {
 System.out.println("list Contents" + list);
 return list;
 }

 public void getQuery() {
 System.out.println("No Parameter ");
 }
}

ReflectMain.JAVA

package ASN.Coders.reflection;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ReflectMain {

public static void main(String[] args) {

 try{

 Map paramMap = new HashMap();
 paramMap.put("dodol1", "dodol_A");
 paramMap.put("dodol2", "dodol_B");
 paramMap.put("dodol3", "dodol_C");
 paramMap.put("dodol4", "dodol_D");
 paramMap.put("dodol5", "dodol_E");
 paramMap.put("dodol6", "dodol_F");
 paramMap.put("dodol7", "dodol_G");
 paramMap.put("dodol8", "dodol_H");
 paramMap.put("dodol9", "dodol_I");
 paramMap.put("dodol10", "dodol_J");

 List list = new ArrayList();
 list.add("dodol_A");
 list.add( "dodol_B");
 list.add( "dodol_C");
 list.add("dodol_D");
 list.add("dodol_E");
 list.add("dodol_F");
 list.add("dodol_G");
 list.add("dodol_H");
 list.add("dodol_I");
 list.add("dodol_J");

 String string = "";

 paramMap = ReflectApp.callBusinessClass("ASN.Coders.reflection.AppTest", "getQuery", paramMap);
 paramMap = ReflectApp.callBusinessClass("ASN.Coders.reflection.AppTest", "getQuery", list);
 paramMap = ReflectApp.callBusinessClass("ASN.Coders.reflection.AppTest", "getQuery", null);

 }catch(Exception ex){
 ex.printStackTrace();
 }
 }
}

Written by snippetjournal

April 17, 2013 at 6:31 am

Posted in Programming

Tagged with , , , ,

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

%d bloggers like this: