Java tech lead to generate custom java 8 code using plugins

Java tech lead to generate custom java 8 code using plugins

In this article, you will learn about the ways to plug in your java classes into speedment to handle the code generation. You can map to virtually any type with custom modules. Learn how to do it from professional senior java tech lead.

There is a thing that most programmers detest and it is writing the boilerplate code. They need to spend endless hours for setting up entity classes and configuring database connections. To skip such circumstances, you can depend on a program like Speedment Open source that generates all this code for you. This makes it easy to acquire a database project up and running with less manual labor, but there is a question that follows. How do you regulate control of the written code when large parts are handed over to a machine?

Learn from senior java tech lead

For instance, you have a database with a table named as “user”, which contains a column: gender: and you wish to implement it as an enum in Java. When you run Speedment and apply it to generate code, the gender field will be represented as a String.

In this instance, we are using a database schema with a table named as “Person”. A person has a name, id, and a gender. The gender is announced as an ENUM that includes three possibilities- Male, Female, and Other. If we employ default settings in Speedment to create this class, Speedment will consider the ENUM as String.

Steps to create the plugin project :

To perform any custom changes to the Speedment platform, we have to define a plugin. A plugin is a software piece that can be plugged into Speedment runtime from the pom.xml-file. It is residing in its own maven project and we can share it between projects.

<dependency>
<groupId>com.speedment< /groupId >
<artifactId>speedment< / artifactId >
<version>$-(speedment.version)</version>
</dependency>

The system of plugin turns around two interfaces- Component and Component Constructor. Each component includes a number of stages in which it is eligible to execute. These are Load, Initialize, Resolve and Start.

The Component Constructor is a lightweight type interface that includes a default constructor and a method for initializing new examples of the custom component. We are using maven plugin to setup the new code.

public class final Customappingcomponenet
extends Abstract component
{
Customappingcomponenet(Speedment speedment)
{
super(speedment);
}
@Override
public void onResolve()
{
//Resolve logic here…
}
@Override
public class < customappingcomponenet > getComponentclass()
{
return Customappingcomponenet.class;
}
@Override
public software asSoftware()
{
return Abstractsoftware.with(“Custom mapping component”, “1.0”, APACHE_2);
}
@Override
public Component defaultCopy(Speedment speedment)
{
return new Customappingcomponenet(speedment);
}
}
public final class CustomappingComponentInstaller
implements ComponenetConsturctor<CustomappingComponent>
{
@Override
public component create(Speedment speedment)
{
return new CustomappingComponent(speedment);
}
}

We now got a bare-bone plugin and we can add it to a Speedment project. Further we need to define the logic that maps between genders and strings. We have to work on Gender enum first-

public enum Gender
{
MALE (“Male”),
FE MALE (“Female”),
OTHER (“Other”);
private final String databaseName;
Gender (String databaseName )
{
this.databaseName = databaseName
}
public String getdatabaseName
{
return databaseName;
}
}

In next step, we need to announce a type that implements the TypeMapper-interface in Speedment. It is very easy. It includes two methods for mapping to and from the database type and methods for retrieving the java class of both types.

public final string class StringToGenderMapper
implements TypeMapper<String, Gender>,
{
@Override
public class <Gender> getJavaType ()
{
return Gender.class;
}
@Override
public class <String> getDatabaseType () 
{
return String.class;
}
@Override
public Gender toJavaType (String value)
{
if (value == null)
{
return null;
}
else
{
return Stream.of< (Gender.values())
.filter (g -> g.getDatabaseName().equals(value))
.findAny()
.orElseThrow(() -> 
new UnsupportedOperationException
(“Unknown gender” + value + “.”) );
}
}
@Override
public String toDatabaseType (Gender value)
{
if (value == null) return null;
else return value.getDatabaseName();
}
@Override
public boolen isIdentifyMapper ()
{
return false;
}
}

You need to install this new mapper in the Speedment platform. This can be done with the help of component created by us earlier by modifying the onResolve()-method-

@Override
public void onResolve ()
{
// Resolve logic here
getSpeedment ().getTypeMapperComponent ().install (StringToGenderMapper:: new);
}

So, the new plugin is ready. You can start building the project and set to go.

This article is shared by senior java tech lead working in reputed IT Company. For feedback, kindly drop your comments below.

Guest Post Author Bio:

James Warner is a highly qualified digital marketing and entrepreneurship. I’m a contributing editor of NexSoftsys for many years in a various role including editor Technology, Health & Media editor and also working as a freelance. You can contact me on Facebook or Follow me on Twitter or add your circle Google+

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *

67 Shares
Share57
Tweet
+1
Share8
Pin1
Stumble1