Sunday, 28 February 2016

Reflection in Java



Reflection is one of the very powerful mechanism in Java which gives your program the ability to inspect characteristic and modify the internal state of a object at runtime.







Steps

1. Compiler insert a public static final field called class, which is an instance of java.lang.Class into the bytecode of compiled class.

2. java.lang.Class has various methods that can be used to inspect and modify the object characteristic.

3. Assume we have a class as shown below

package sample;

import java.util.ArrayList;
import java.util.List;

public class SampleDemo {

    public String name = "Manas"; // Public Methods
    private int salary = 0; // Private Methods
    public List<String> location = new ArrayList<String>();

    // private constructor
    private SampleDemo() {

    }

    // public constructor
    public SampleDemo(String name) {

    }

    // public method
    public void getName() {
        System.out.println("My Name is XXX");
    }

    // private methods
    private void getSalary() {
        System.out.println("Salary is confidential");
    }

    // protected methos
    protected void getGirlFriend() {
        System.out.println("Cannot be disclosed to all the list is....");
    }

    // public setter method
    public void setName(String name) {
        this.name = name;
    }

}



4. We create a class to inspect the demo class
package sample;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectionInspection {

    public static void main(String[] args) throws NoSuchFieldException, SecurityException {
        String className = SampleDemo.class.getSimpleName();
        System.out.println("Class Name: " + className);

        // inspect all public field
        Field[] fields = SampleDemo.class.getFields();
        for (int i = 0; i < fields.length; i++) {
            System.out.println("Field name:" + fields[i]);
        }

        // To inspect the private variable we have to specify them explicitly
        Field f = SampleDemo.class.getDeclaredField("salary");
        System.out.println("private field  " + f);

        // inspect all public methods including getter and setter
        Method[] methods = SampleDemo.class.getMethods();
        for (int i = 0; i < methods.length; i++) {
            methods[i].setAccessible(true);
            System.out.println("Method name:" + methods[i]);
        }

        // inspect all constructors
        Constructor[] constructors = SampleDemo.class.getConstructors();
        for (int i = 0; i < constructors.length; i++) {
            System.out.println("Constructor Name:" + constructors[i]);
        }

    }

}










5 Output




6. Conclusion

1. As we can see that all public methods, public constructors, and public fields can be easily listed.
2. For private fields and methods we have to specify the name explicitly to inspected
3. As all the class also extends the Object class we can see all the methods from Object class when we list all the methods.

7. Modify using reflection
package sample;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectionModify {

    public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException,
            IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {

        String className = SampleDemo.class.getSimpleName();
        System.out.println("Class Name: " + className);

        Constructor c = SampleDemo.class.getDeclaredConstructor();
        c.setAccessible(true);

        SampleDemo sampleDemo = (SampleDemo) c.newInstance();
        sampleDemo.getName();

        Field f = SampleDemo.class.getDeclaredField("salary");
        f.setAccessible(true);34e334xzgetInt(sampleDemo));
        f.setInt(sampleDemo, 10);
        System.out.println("private field  " + f.getInt(sampleDemo));

        Method m = SampleDemo.class.getDeclaredMethod("getSalary");
        m.setAccessible(true);
        m.invoke(sampleDemo);

    }

}

8. Output


9 Conclusion

We can use the setAccessible(true) to modify access to the private field, method and constructor.
Once we have access to the private attribute we can even modify the value of that attribute


Additional information

1. Other ways to get a handle to the class we will be inspecting or modifying are as follows
  1.1.  If we know the complete path of class file we can use below statement
              Class c=Class.forName("pkgName.SampleDemo");
    1.2  We create an instance of the class and use getClass() as shown below
                Class c=obj.getClass();   
  
2. Once we have Class instance it has number of methods to inspect or modify.



3. Reflection is part of java.reflect package.


Preventing Reflection


Basically it boils down to two question.

1. Whether you can prevent other peoples code to modify your class’s private attribute.

The answer is No. Once you distribute your code in form of jar, you do not have control over it as your code will be executed in another person JVM.
But if your code is accessed through some protocol such as HTTP and you have control over the server where code is getting executed you can definitively deploy security manager to prevent unwanted modification or access.


2. Whether you want to run your code in your environment in a manner that private attribute are not modified

The answer is Yes. Java by itself allows setAccessible to be used for only classes in JAVA_HOME and it does not allow modification from the class through untrustworthy applet.
You can also install security manager and use a more restrictive security policy which disallow classes referencing certain package.
One important point is permission for setAccessible is binary so you cannot selectively set some attribute as accessible and other as not-accessible in a class.

Areas where Reflection is used

 

1. Integrated Development Environment such as Eclipse.
 Ever wondered when typing a dot(.) after object, IDE gives you list of all the method for that object or list of method that that needs to be implemented when you implement an interface.That is the power of reflection which inspect the class at runtime.

2. Framework such as struts and spring etc.

Framework use something like this in configuration file
<bean id="someId" class="com.sample.SampleDemo’/"> 
  <property name="someField" value="someValue" />
</bean>


It is through reflection that the framework will invoke class.forName(“pkgName.className”) when it encounter bean tag in the xml and also set the property.

3. Testing tools
Testing tools such as Junit uses reflection to get set of @Test annotated method to invoke.


2.http://www.javatpoint.com/java-reflection
3. http://tutorials.jenkov.com/java-reflection/index.html
4. http://www.programmerinterview.com/index.php/java-questions/java-reflection-example/


Sunday, 21 February 2016

Java Class Linking and Initialization

My previous blog explains how a class is loaded. Once Java class is compiled, it’s binary is loaded into the JVM. Next two phase are Linking and Initialization.

Linking


Linking involves three steps.
Verification
Preparation
Resolution.

Verification:

One of the advantage of java architecture is that once a java source file is compiled and converted to bytecode it can be executed in any JVM. This also introduce security risk as the source is untrustworthy. So verification is like a security guard which check the byte code before it is allowed to be executed by JVM. Verifier is a part of VM and programmer does not have any control over it.

  
    

Preparation:

Memory allocation and initialization of class variable to default value is done at this steps
Two important point is class variable are assigned initial value later and the value for Boolean is not false but integer zero(0).

  

Resolution:

  
Resolution involves resolving symbolic reference of classes, interfaces, fields and methods with direct reference.
"symbolic references" are strings that can be used to retrieve the actual object
  

    Class and Interface resolution





  Field Resolution



  
  
Exception :
      NoSuchFiledError : Field that cannot be resolved
      IllegalAccessError : Field is not accessible
  
  

Method Resolution:




Exception
    NoSuchMethodError: Method Resolution Fails.
    AbstractMethodError: Method resolution succeed but method is abstract and class is not abstract
    IllegalAccessError:  Field is not accessible


Initialization

Once linking is completed the last phase Initialization is executed. In summary this phase assign initial value to class variable and static initializers.

JVM compiles the java class and collects the class variable and static initializer into a no name methods call “”. Programmer does not have any control on this special methods and only VM execute this method to assign initial value to class variable and static initializer


This special methods is called Class Initialization method for class and Interface initialization method for interface.

Note: If a java class does not have class variable or static initializer then the compiled class will not have no name special method “”.

Class Initialization
Initialize the class’s direct superclass if not initialized and recursively keep on going up unless all the superclass in the hierarchy is initialize or reached at the top of hierarchy that is object class
Initialize the class Initialization method.

Interface Initialization
1. Initialize the interface Initialization method.

Additional resource can be found here



Sunday, 14 February 2016

Java Class Loading


Loading is the process of finding the binary representation of a class or interface with a particular name and creating class or interface from the binary representation.

One of the important concepts before we can go into details is ClassLoaders. The Java ClassLoaders is a part of JRE that loads classes dynamically into the JVM. Basically they are responsible for locating libraries, reading their content and loading the classes contained within the libraries.

Below are three important types of ClassLoaders and the sequence in which they are invoked

1. Bootstrap ClassLoaders: This class loader is provided by the JVM and it loads the trusted classes that are located in jre\lib\rt.jar. Class in rt.jar provide core API functionality.

2. Extension ClassLoaders:  rt.jar has a class called sun.misc.Launcher$ExtClassLoader which invokes the extension class loader. This class loaders loads the class in lib/ext directory. Classes loaded by extension class loader extends the core API functionality.
By packaging a class in a jar and saving the jar in lib/ext directory we can make the class load by Extension Class loader . One of the implication of making a class loaded by extension class loader is that we do not have to specify the classpath while executing the that class.

3. System ClassLoader: rt.jar has a class sun.misc.Launcher$AppClassLoader which invoke the System ClassLoader. This will load all the class in java.class.path which is specified in environment variable CLASSPATH.

A simple illustration.
 
1. Create a simple class. 
public class StartMyApp {
public static void main(String[] args) {
System.out.println("in StartMyApp");
DependentApp dependentApp=new DependentApp();
dependentApp.myDependentMethod();
}
}

2. Another class

public class DependentApp {

public void myDependentMethod(){
System.out.println("In dependent methods...");
}
}

3. Run java StartMyApp -verbose:class command. You will get the below output on the console.



One important things to note is that any class is uniquely identified by (Name, Package, Class Loader).

Below figure illustrate what happen when we compile and run a java class from the perspective of loading the class.





Below figure illustrate the flow inside loadClass methods.






The above figure demonstrate delegation hierarchy in ClassLoaders.

Classloaders delegate finding classes and resources to their parent before searching their own classpath. If the parent class loader cannot find a class or resource, only then does the class loader attempt to find them locally.

This delegation model also ensure two things: Uniqueness and Visibility

A class is loaded only once which is uniqueness and second is that a child class loader can see class loaded by parent class loader but opposite is not true and this principle is visibility of class loader





Additional resource can be found here

http://www.onjava.com/pub/a/onjava/2005/01/26/classloading.html
https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-5.html
http://javapapers.com/core-java/java-class-loader/
http://zeroturnaround.com/rebellabs/rebel-labs-tutorial-do-you-really-get-classloaders/2/
https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/api/java.lang.ClassLoader.html

 

 

 

Sunday, 7 February 2016

How to setup Jenkins with GitHub and Maven


Continuous Integration

Jenkins is a continuous integration tool.Continuous Integration (CI) is a development practice that requires developers to integrate code into a shared repository several times a day. Each check-in is then verified by an automated build, allowing teams to detect problems early.
  
More information on CI can be found     https://en.wikipedia.org/wiki/Continuous_integration

  

Use case:

  • Whenever we made some change in our code and push it to repository in GitHub, Jenkins will detect the change and trigger the build process using Maven
  • Create a Jenkin job to build a project using maven for a repository in Github and run it on demand

Prerequisite:

Before we can start working on integrating Jenkins with Maven and Github we need to complete below steps

1. Download Tomcat

Apache Tomcat, often referred to as Tomcat, is an open-source web server developed by the Apache Software Foundation (ASF). It implements specification for Java Servlet. JSP, Java EL and websocket.
Download tomcat from https://tomcat.apache.org/download-70.cgi
More information on Tomact can be found here http://tomcat.apache.org/

  

2. Download Jenkins

This is an open source continuous integration tool written in Java. This is a server based system running in a servlet container such as Apache Tomcat.
More information on Jenkins can be found here https://jenkins-ci.org/

3. Create Repository in Github

This is a source code management system for software development. You should create a repository and push your code in repository
Additional information on how to create repository can be found here
   https://help.github.com/articles/creating-a-new-repository/

4.  Download Maven

This is a build automation tool used primarily for Java projects.  More information can be found here https://maven.apache.org/



Quick Steps:


1. Once Tomcat and jenkins.war are downloaded, Go to tomcat installation directory and to webapps folder.

2. Copy Jenkins.war in webapps directory ( ..\apache-tomcat-7.0.64\webapps)

3. Start the tomcat server.
   D:\apache-tomcat-7.0.64\bin>startup.bat

4. Once server is started, open a browser and go to URL.
    http://localhost:8080/jenkins/
  
Note: Port number in above URL can be different depending on tomcat configuration file server.xml which can be found in the location ...apache-tomcat-7.0.64\conf
  
5. Once Jenkins homepage is opened go to Manage jenkins and then to Configure System


6. In configuration page you configure java, git and maven as show below. There are lots of other advance configuration that can be done from here as well. We did only the minimum required to configure Jenkins with GitHub and Maven

6.1 Java







  
  
 6.2 Git
    6.2.1 Add path to Git in the PATH environment variable as shown

    
    6.2.2 Configure Git in Jenkins as shown below

    
  
  
   
 6.3 Maven: Configure Maven as show below


  
 
7. Apply and save the changes in configuration page. Ensure that there is no error while you configure java, git and maven.
Tips: You can go back to configuration page to check if there is any error in above configuration.

8. Go to Manage Jenkins and then to Manage Plugins


9.  In Manage Plugin page we have to ensure that we have all the plugin necessary for Git and Maven. Following are the Maven and Git plugin respectively.




Note: If plugins are not present in Installed tab, go to Available tab check for the plugin and download/install the same.

Configuration is now all set.


Create a basic Jenkins job


1. From the Jenkins homepage go to ‘New Item’. In new item page give a name to the job and select ‘Freesytle project’ as shown below. Hit OK.


2. Start configuring the job.

3. Project Name will be automatically populated.

4. You can give a description to the job.

5. In Source Code Management Configure following
   5.1 Git
   5.2 Repository/Credentials (For the first time use click on ‘Add’ to add new credential.)
   5.3 The Git repository branch you would like to work with. By default it is the master

  
6. In Build configuration configuration click on ‘Add build step’ and select ‘Invoke Maven 3’
Configure Maven as shown below.


7. Build trigger can be configured to trigger the job. For our use case we have selected to poll github every minute. If there is any change in repository job will be triggered within 1 minute.




8. We can also trigger the Jenkins job manually with the option' build now' in the job home page.


The post is a quick steps to configure Jenkins with Maven and GitHub.

For more advance Jenkins configuration you can go to
1. https://wiki.jenkins-ci.org/display/JENKINS/Building+a+software+project
2.https://wiki.jenkins-ci.org/display/JENKINS/Step+by+step+guide+to+set+up+master+and+slave+machines
3.http://michal.karzynski.pl/blog/2014/04/19/continuous-integration-server-for-django-using-jenkins/








Streaming with Kafka API

The Kafka Streams API is a Java library for building real-time applications and microservices that efficiently process and analyze large-sca...