Java Example – Code Generation By CodeModel

Maven Dependency

 <!-- CodeModel -->
 <dependency>
 <groupId>com.sun.codemodel</groupId>
 <artifactId>codemodel</artifactId>
 <version>2.5</version>
 </dependency>

Example

package com.ittechoffice.codemodel;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import com.sun.codemodel.JClassAlreadyExistsException;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JFieldVar;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JMod;
import com.sun.codemodel.JPackage;

public class CodeModelGenerator {
 
 private JCodeModel codeModel;
 private JPackage jPackage;
 private JDefinedClass jClass;
 
 private CodeModelGenerator(String packageName, String className) throws JClassAlreadyExistsException{
 codeModel = new JCodeModel();
 jPackage = codeModel._package(packageName);
 jClass = jPackage._class(className);
 }
 
 public static CodeModelGenerator buildClass(String packageName, String className) throws JClassAlreadyExistsException{
 CodeModelGenerator codeModelGenerator = new CodeModelGenerator(packageName, className);
 return codeModelGenerator;
 }
 
 public JFieldVar addField(int mods, Class<?> type, String name){
 return jClass.field(mods, type, name);
 }
 
 public JMethod addMethod(int mods, String name){
 return jClass.method(mods, codeModel.VOID, name);
 }
 
 public JMethod addMethod(int mods, Class<?> type, String name){
 return jClass.method(mods, type, name);
 }
 
 public void build(String path) throws IOException{
 codeModel.build(new File(path));
 }
 
 public static void main(String[] args) throws JClassAlreadyExistsException, IOException{
 CodeModelGenerator generator = CodeModelGenerator.buildClass("com.ittechoffice", "GeneratedClass");
 generator.build(".");
 }
}

Generated Code

package com.ittechoffice;


public class GeneratedClass {


}

 

 

 

 

Maven Problem when Company Firewall Blocks Connection

Even if you can use browser to connect to central repository, https://repo1.maven.org/maven2/, your maven may be able to connect it.

Detect your proxy setting by Command Line in Window:

netsh winhttp show proxy

It would show your PC’s proxy information.

Configure your Maven Setting (in ${USER_HOME}/.m2/settting.xml)

<settings>
  ...
  <proxies>
    <proxy>
      <active>true</active>
      <protocol>http</protocol>
      <host>${Specify in PROXY INFO}</host>
      <port>${Specify in PROXY INFO}</port>
      <username></username>
      <password></password>
      <nonProxyHosts></nonProxyHosts>
    </proxy>
  </proxies>
  ...
</settings>

 

 

Maven Conflict of Dom4j/Hibernate and Selenium

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.ittechoffice</groupId>

<artifactId>HelloWorld-Selenium</artifactId>

<version>0.0.1-SNAPSHOT</version>

<repositories>

     <repository>

        <id>JBoss repository</id>

        <url>http://repository.jboss.com/maven2/</url>

     </repository>

      <repository>

     <id>Java.Net</id>

     <url>http://download.java.net/maven/2/</url>

    </repository>

   </repositories>

<dependencies>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>selenium-java</artifactId>

<version>2.53.0</version>

</dependency>

<dependency>

<groupId>org.seleniumhq.selenium</groupId>

<artifactId>htmlunit-driver</artifactId>

<version>2.20</version>

</dependency>

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>commons-math3</artifactId>

<version>3.2</version>

</dependency>

<!-- H2 database -->

<dependency>

    <groupId>com.h2database</groupId>

      <artifactId>h2</artifactId>

      <version>1.4.191</version>

</dependency>

<!-- MySQL database driver -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.9</version>

</dependency>

<!-- Hibernate core -->

<dependency>

<groupId>hibernate</groupId>

<artifactId>hibernate3</artifactId>

<version>3.2.3.GA</version>

</dependency>

<!-- Hibernate annotation -->

<dependency>

<groupId>hibernate-annotations</groupId>

<artifactId>hibernate-annotations</artifactId>

<version>3.3.0.GA</version>

</dependency>

<dependency>

<groupId>hibernate-commons-annotations</groupId>

<artifactId>hibernate-commons-annotations</artifactId>

<version>3.0.0.GA</version>

</dependency>

<!-- Hibernate library dependecy start -->

<dependency>

<groupId>dom4j</groupId>

<artifactId>dom4j</artifactId>

<version>1.6.1</version>

</dependency>

<dependency>

<groupId>commons-logging</groupId>

<artifactId>commons-logging</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>commons-collections</groupId>

<artifactId>commons-collections</artifactId>

<version>3.2.1</version>

</dependency>

<dependency>

<groupId>cglib</groupId>

<artifactId>cglib</artifactId>

<version>2.2</version>

</dependency>

<!-- Hibernate library dependecy end -->

<dependency>

<groupId>javax.transaction</groupId>

<artifactId>jta</artifactId>

<version>1.1</version>

</dependency>

      <!-- Javaee API -->

<dependency>

      <groupId>javax</groupId>

      <artifactId>javaee-api</artifactId>

      <version>6.0</version>

</dependency>

</dependencies>

</project>

 

Solution

<dependency>

<groupId>dom4j</groupId>

<artifactId>dom4j</artifactId>

<version>1.6.1</version>

<exclusions>

        <exclusion>

            <artifactId>xml-apis</artifactId>

            <groupId>xml-apis</groupId>

        </exclusion>        

      </exclusions>

</dependency>

 

Thread safety

It is piece of code is safety executed to manipulate shared data by multiple thread at the same time.

Implementation Approaches

Thread-local storage: Variables are localized

Immutable object: State of object cannot be changed after construction

Mutual Exclusion: no concurrent process, e.g. Locking

Atomic Operation: e.g. version