Thursday, April 14, 2011

List - Array Conversion Notes

1) Converting a List of 

(i)private static String[] createStringArrayWithList(List pel) {
Field[] attribute_fields = (Field[]) pel.toArray(new Field[0]);
String string_Array[]=new String[attribute_fields.length];
for (int i=0;i
{
string_Array[i]=attribute_fields[i].toString();
}
return string_Array;
}

(ii) Set langSet = new HashSet();
Object[] langArr = langSet.toArray(); // this returns an object array

(iii) Set langSet = new HashSet();
String[] arr2 = new String[5];
arr2 = langSet.toArray(arr2); // this returns an array of same type as arr2 i.e a String array

2) Converting a List

private static List convertFieldList_to_StringList(List propEventEle) {
List fields = new ArrayList();
for(Field f: propEventEle)
{
fields.add(f.getName());
}
return fields;
}


3) Converting a Array to List

Integer[] numArr = {100, 200, 300, 400, 500};
List numList = Arrays.asList(numArr);

note : any changes in the list numList updates the array numArr also.

Sunday, March 6, 2011

How to see the source code for JVM classes in Eclipse

reference link:

http://www.devdaily.com/blog/post/eclipse-ide/eclipse-faq-attach-jvm-classes-source-code

When using Eclipse straight out of the box, if you click on a Java-provided class, like the FileWriter class, you won't get much help when the Eclipse Class File Editor is shown. All you'll see is some binary information, with these messages at the top of the file:

Source not found
The jar file classes.jar has no source attachment.
You can attach the source by clicking the Attach Source below:

(The message really should end with "by clicking the Attach Source button below:")

On my Mac OS X laptop, when I click this button I'm prompted to select the location containing the source for classes.jar. (Sarcasm: Well, if I knew that I wouldn't be here, now would I?)

After a little digging, I found the source code on Sun's website. Specifically, for Mac OS X 10.4.10, which uses Java 5 (JDK 1.5.x, also known as J2SE 5.0 JDK), the source code is available from this page. After following a few more links, this requires an account with Sun to continue, so I created an account, then downloaded a file named jdk-1_5_0-src-scsl.zip.

Next, I didn't expand this file, but just moved it to a Reference folder that I keep in my home directory. Then, back in Eclipse, I specified this file as one containing the Java JVM source code files. As soon as Eclipse finished this process it switched over to showing the source code for the FileWriter class, which was the class I was trying to look at when I started this process.

This is very cool. It's much more helpful to see the actual Java source when you click on a class that's in the JVM than it is to see the default binary-output information.

Monday, February 21, 2011

Java Reflections

_________________________________________________________

reference links: http://tutorials.jenkov.com/java-reflection/classes.html

Through Java's Reflection API's one can inspect classes, interfaces, fields and methods at runtime, without knowing the names of the classes, methods etc. at compile time.

It also enables instantiation of new objects, invoke methods and get/set field values using reflection.

list of the topics covered :
>>>>Classes :

Using Java Reflection you can inspect Java classes at runtime.
Before you can do any inspection on a class you need to obtain its java.lang.Class object.

If you know the name of the class at compile time you can obtain a Class object like this:
    Class myObjectClass = MyObject.class
If you don't know the name at compile time, but have the class name as a string at runtime, you can do like this:

String className = ... //obtain class name as string at runtime ;

Class class = Class.forName(className);

When using the Class.forName() method you must supply the fully qualified class name including the class package.

From the classes you can obtain information aboutaother elements of class such as, To access with fully qualified class name :
      Class aClass = ... //obtain Class object. See prev. section
String className = aClass.getName();
(or)
Class aClass = ... //obtain Class object. See prev. section
String simpleClassName = aClass.getSimpleName();

You can access the modifiers of a class via the Class object. The class modifiers are the keywords "public", "private", "static" etc. You obtain the class modifiers like this:

  Class myObjectClass = MyObject.class
int modifiers = aClass.getModifiers();

The modifiers are packed into an int where each modifier is a flag bit that is either set or cleared. You can check the modifiers using these methods in the class java.lang.reflect.Modifier:

    Modifier.isAbstract(int modifiers)
Modifier.isFinal(int modifiers)
Modifier.isInterface(int modifiers)
Modifier.isNative(int modifiers)
Modifier.isPrivate(int modifiers)
Modifier.isProtected(int modifiers)
Modifier.isPublic(int modifiers)
Modifier.isStatic(int modifiers)
Modifier.isStrict(int modifiers)
Modifier.isSynchronized(int modifiers)
Modifier.isTransient(int modifiers)
Modifier.isVolatile(int modifiers)
       Class myObjectClass = MyObject.class
Package package = myObjectClass.getPackage();
       Class myObjectClass = MyObject.class
Class superclass = myObjectClass.getSuperclass();
  • Fields :
    Method[] method = aClass.getFields();
  • Annotations :
     Annotation[] annotations = aClass.getAnnotations();

***************************
>>>>Constructor

Using Java Reflection you can inspect the constructors of classes and instantiate objects at runtime. This is done via the Java class java.lang.reflect.Constructor.

Obtaining Constructor Objects

The Constructor class is obtained from the Class object. Here is an example:

Class aClass = ...//obtain class object
Constructor[] constructors = aClass.getConstructors();

The Constructor[] array will have one Constructor instance for each public constructor declared in the class.

If you know the precise parameter types of the constructor you want to access, you can do so rather than obtain the array all constructors. This example returns the public constructor of the given class which takes a String as parameter:

Class aClass = ...//obtain class object
Constructor constructor = aClass.getConstructor(new Class[]{String.class});

Accessing constructor parameters :
Constructor constructor = ... // obtain constructor - see above
Class[] parameterTypes = constructor.getParameterTypes();

You can instantiate an object like this:

//get constructor that takes a String as argument
Constructor constructor = MyObject.class.getConstructor(String.class);
MyObject myObject = (MyObject)constructor.newInstance("constructor-arg1");
****************
>>>>Fields

Using Java Reflection you can inspect the fields (member variables) of classes and get / set them at runtime. This is done via the Java class java.lang.reflect.Field.

Obtaining Field Objects :

Class aClass = ...//obtain class object
Field[] methods = aClass.getFields();

If you know the name of the field you want to access, you can access it like this:

Class  aClass = MyObject.class
Field field = aClass.getField("someField");

Field Name :

Field field = ... //obtain field object
String fieldName = field.getName();

Field Type:

Field field = aClass.getField("someField");
Object fieldType = field.getType();

Getting and Setting Field Values :

Class  aClass = MyObject.class
Field field = aClass.getField("someField");
MyObject objectInstance = new MyObject();
Object value = field.get(objectInstance);
field.set(objetInstance, value);

**************************

>>>>Methods :

Using Java Reflection you can inspect the methods of classes and invoke them at runtime. This is done via the Java class java.lang.reflect.Method.

Obtaining Method Objects:

Class aClass = ...//obtain class object
Method[] methods = aClass.getMethods();
The Method[] array will have one Method instance for each public method declared in the class.
This example below, returns the public method named "doSomething", in the given class which takes a String as parameter:
Class  aClass = ...//obtain class object
Method method = aClass.getMethod("doSomething", new Class[]{String.class}); (or)
Method method = aClass.getMethod("doSomething", null);// method with no parameters

Method Parameters and Return Types:

You can read what parameters a given method takes like this:
Class[] parameterTypes = method.getParameterTypes();

or the return type like this :
Class returnType = method.getReturnType();

Invoking Methods using Method Object:

//get method that takes a String as argument
Method method = MyObject.class.getMethod("doSomething", String.class);
Object returnValue = method.invoke(null, "parameter-value1");

*********************************

>>>>Getters and Setters :

Using Java Reflection you can
1.inspect the methods of classes
2. them at runtime.
3. detect what getters and setters a given class has.

You cannot ask for getters and setters explicitly, so you will have to scan through all the methods of a class and check if each method is a getter or setter.
Here is a code example that finds getter and setters of a class:
-----
public static void printGettersSetters(Class aClass){
Method[] methods = aClass.getMethods();

for(Method method : methods){
if(isGetter(method)) System.out.println("getter: " + method);
if(isSetter(method)) System.out.println("setter: " + method);
}
}

public static boolean isGetter(Method method){
if(!method.getName().startsWith("get")) return false;
if(method.getParameterTypes().length != 0) return false;
if(void.class.equals(method.getReturnType()) return false;
return true;
}

public static boolean isSetter(Method method){
if(!method.getName().startsWith("set")) return false;
if(method.getParameterTypes().length != 1) return false;
return true;
}
-----
*********************************

>>>>Private Fields & Methods :

It is actually possible to access private fields and methods of other classes via Java Reflection.

Note : only works when running the code as a standalone Java application, like you do with unit tests and regular applications. If you try to do this inside a Java Applet, you will need to fiddle around with the SecurityManager.

Accessing Private Fields :

Example class :
public class PrivateObject {

private String privateString = null;

public PrivateObject(String privateString) {
this.privateString = privateString;
}
}
Accessing the private contents using reflect:
PrivateObject privateObject = new PrivateObject("The Private Value");
Field privateStringField = PrivateObject.class.getDeclaredField("privateString");
privateStringField.setAccessible(true);
String fieldValue = (String) privateStringField.get(privateObject);
System.out.println("fieldValue = " + fieldValue);
  • Notice the use of the method PrivateObject.class.getDeclaredField("privateString"). It is this method call that returns the private field.
  • This method only returns fields declared in that particular class, not fields declared in any superclasses.
  • By calling Field.setAcessible(true) you turn off the access checks for this particular Field instance, for reflection only.

Accessing Private Methods:

Example class :
public class PrivateObject {

private String privateString = null;

public PrivateObject(String privateString) {
this.privateString = privateString;
}

private String getPrivateString(){
return this.privateString;
}
}
Accessing method:
PrivateObject privateObject = new PrivateObject("The Private Value");
Method privateStringMethod = PrivateObject.class.getDeclaredMethod("getPrivateString", null);
privateStringMethod.setAccessible(true);
String returnValue = (String)privateStringMethod.invoke(privateObject, null);
System.out.println("returnValue = " + returnValue);
This code example will print out the text "returnValue = The Private Value", which is the value returned by the method getPrivateString() when invoked on the PrivateObject instance created at the beginning of the code sample.

*******************
Ignored contents under the topic :
Annotations

Generics

Arrays

Dynamic Proxies


Dynamic Class Loadin and Reloading