Friday, February 26, 2016

Java Reflection Work

I made small work on Java Reflection API, when ever an object is passed to the inspect() method in my code, it will try to find the attributes and their values and try to insert to DB with the same name as of the ClassName==TableName and propertiesNames==ColumnNames, this is almost similar concept to Hibernate.

I thought of resembling the same and made small assignment on my own so that it will be useful for us in future..

U can download the program from the below link:
downloadCode

I passed the object of Employee class as below:

public class Employee {

int eno;
String ename;
double sal;
int dept;
String doj;
//generate getters and setter for this
}

package org.myown;

public class Person {
int id;
String name;
String occupation;
String sex;
//generate getters and setter for this
}

You can pass any Object and see the result for yourself..!

There are few more methods which you can leverage the potential of Reflection API:

getAllFields() method will return all the fields of a class and its hiearchy superclasses till Object class.

public static List<Field> getAllFields(List<Field> fields, Class<?> type) {
        fields.addAll(Arrays.asList(type.getDeclaredFields()));
        if (type.getSuperclass() != null) {
            fields = getAllFields(fields, type.getSuperclass());
        }
        return fields;
    }


setHierarchyPrivateField() method is used to set the private field of the current class or the private field of its super classes.

    public static void setHierarchyPrivateField(String fieldName,Object input, Object object){
        try {
            Class<?> myclass= object.getClass();
            Field field=null;
            List<Field> fields=new ArrayList<Field>();
            fields = getAllFields(fields, myclass);
            if(fields!=null) {
                Iterator<Field> itr = fields.iterator();
                while(itr.hasNext()){
                    Field fld= itr.next();
                    if(fld.toString().contains(fieldName)){
                        field = fld;
                        break;
                    }
                }
                if(field!=null) {
                    field.setAccessible(true);
                    field.set(object, input);
                    System.out.println(field.get(object).toString());
                }
            }
        } catch ( SecurityException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }


setThisPrivateField() method is used to set the private field of the current class.

    public static void setThisPrivateField(String fieldName,Object input, Object object){
        try {
            Class<?> myclass= object.getClass();
            Field field= myclass.getDeclaredField(fieldName);
                if(field!=null) {
                    field.setAccessible(true);
                    field.set(object, input);
                    System.out.println(field.get(object).toString());
                }
            } catch ( SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }


getThisPrivateField() method is used to get the private field of the current class outside of the class.

    public static Object getThisPrivateField(String fieldName,Object object){
        Object result=null;
        try {
            Class<?> myclass= object.getClass();
            Field field= myclass.getDeclaredField(fieldName);
            if(field!=null) {
                field.setAccessible(true);
                //System.out.println(field.get(object).toString());
                result=field.get(object);
            }
        } catch ( SecurityException | IllegalArgumentException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return result;
    }

Sunday, February 14, 2016

How to Launch Application/Server from bat file on double click? 

Hi Friends,
This is a very easy task and helpful once, u can create a shortcut for some bat files on your desktop and launch your applications straight away on double click the below is the batch program for the same, save it as a .bat file and create shortcut for it on your desktop and use it this is a very useful one and hence i thought of sharing with my friends.

setlocal
CHDIR /D D:\Tools\cis\9.1\cis-sdk\bin\
start "C:\Windows\System32\cmd.exe" launch.bat runCISAgent.py