– notes, snippet codes, and my journal

Example Basic Reflection In Java

leave a comment »

Apa itu reflection, silahkan baca di link berikut😀

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.

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);
 System.out.println("Paramater Unknown . . . .");
 Method method = cls.getDeclaredMethod(methodName, noparams);
 method.invoke(obj, null);

 } catch (ClassNotFoundException e) {
 } catch (SecurityException e) {
 } catch (NoSuchMethodException e) {
 } catch (IllegalArgumentException e) {
 } catch (InvocationTargetException e) {

 return null;



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 ");


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) {


 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_B");
 list.add( "dodol_C");

 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){

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: Logo

You are commenting using your 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: