Friday, March 24, 2023

Working with java reflect

A simple code that i wanted to share:
List methods / attributes and the call of method without parameters

-Aclass is a simple class that will be loaded by java reflect
-An instance will be created and manipulated


package com.exemple.reflect;

public final class Aclass {
    
    public int i;
    
    public Aclass() {
        i = 50;
    }
    public Aclass(int i) {
        this.i = i;
    }
    
    public int computeSquare() {
        return i*i;
    }
}
 

  

package com.exemple.reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;

public final class MainClass {
    
    private static String CLASS_NAME = "com.exemple.reflect.Aclass";
    private static Aclass instance;
    private static Class theClass;
    
    static {
      try {
              theClass = Class.forName(CLASS_NAME);
              instance = (Aclass) theClass.newInstance();
      }catch(Exception e) {
          e.printStackTrace();
      }
    }

    public static void main(String... args) {
        System.out.println("List of methods");
        Arrays.asList(theClass.getDeclaredMethods()).stream().forEach(System.out::println);
        System.out.println("List of attributes");
        Arrays.asList(theClass.getDeclaredFields()).stream().forEach(System.out::println);
        System.out.println("Running first method");
        Method method = theClass.getDeclaredMethods()[0];
        try {
            System.out.println(method.invoke(instance));
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
            e.printStackTrace();
        }       
    }
} 

Monday, December 26, 2022

Jsf 2.x Spring 5 Hibernate Primefaces 11 Web application skelton / generator

Versions


Date Version Remarks
12-26-2022 0.1-SNAPSHOT First version
06-23-2024 1.0 Mavenized project

 

JSF generator

This post aims to offer to j2ee developers easy way to develop jsf/spring/primfaces applications by making downloadable:
1- An skelton of a basic securized application
2- A generator for CRUD web pages and java classes in different layers from view to model.


1- Used architecture is a  typical MVC implementation usually used in j2ee web applications:


View <----->  Backing beans (bean package)
    |                                    |
    |                      Service or business layer
    |                           (service package) <---Implementation
    |                                    |
    |                               Dao layer <---Implementation
    |                                    |
    |                                 Model  <----> DB 
    |
----|--------------------------------------------------------------------------------------------------------------------
    |                                     Spring security
----|----------------------------------------------------------------------------------------------------------------------        |                                                              
    |
Web browser

Generic Dao class was developeled to make easier complex queries manipulation
Hibernate is used as JPA implementation


Project workspace on sourceforge is

https://sourceforge.net/projects/jsf-project-generator/

First of all to make things most attractive... that's the final result
(User test / test123)










Skelton is downloadable here (Maven project)



2- Generator
generator is a simple tool to generate the above layers for CRUD operation on a specific data entity

Steps to generate files for an entity
1- We need to compile the entity !!!
    Copy the entity into folder group-id/artifact-id/model
    Example Product.java class for com.thinkopen group-id
                                                  skelton artifact-id
     will be copied to JSF-GENERATOR-FOLDER/com/thinopen/skelton/model



  2-turn operationCode parameter in config.properties file to "script"
             groupId=com.thinkopen
             artifactId=skelton
             classes=Product:
   in case of many classes put them all on the same line ":" separated


 
3- run
      java -jar generator.jar
      output will be



4- Compilation:
run command script ./script.sh.bat





If all right we will have a successful compilation message.

5- Generate all files:
Turn operationCode to all



6-run  java -jar generator.jar as last step

---> All generated will be in output folder, copy and put then in your project tree.