Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sviperll/writejava4me

Simple and easy Java code-generator
https://github.com/sviperll/writejava4me

Last synced: about 1 month ago
JSON representation

Simple and easy Java code-generator

Awesome Lists containing this project

README

        

writejava4me Easy code generation for Java
==========================================

Works as annotation for annotation.
User defines new annotation and annotate it with @GeneratesClass annotation.
@GeneratesClass defines wich class will be generated.

Example
-------

Imagine that we would like to define both checked exception and RuntimeException when checked exception can't be used.

We can write it like this:

```java
public class MyException extends Exception {
private final MyData valuableData;
public MyException(String message, MyData valuableData) {
super(message);
this.valuableData = valuableData;
}
public MyData valuableData() {
return valuableData;
}
}

public class RuntimeMyException extends RuntimeException {
private final MyException cause;
public RuntimeMyException(MyException cause) {
super(cause);
this.cause = cause;
}
@Override
public MyException getCause() {
return cause;
}
}
```

If we are going to define many exceptions pairs like this we may want to generate Runtime wrappers automatically.
It will be good to have annotation for this.

```java
@GenerateRuntimeExceptionWrapper
public class MyException extends Exception {
private final MyData valuableData;
public MyException(String message, MyData valuableData) {
super(message);
this.valuableData = valuableData;
}
public MyData valuableData() {
return valuableData;
}
}
```

With writejava4me it's easy to define such annotations. Here is an implementation for @GenerateRuntimeExceptionWrapper

GenerateRuntimeExceptionWrapper.java:

```java
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@Documented
@GeneratesClass(classNameTemplateString = "Runtime{{annotated}}", classTemplateResourcePath = "RuntimeExceptionWrapper.mustache")
@interface GenerateRuntimeExceptionWrapper {
}
```

RuntimeExceptionWrapper.mustache located in resources folder:

```java
package {{package}};

public class {{class}} extends RuntimeException {
private final {{annotated}} cause;

public {{class}}({{annotated}} cause) {
super(cause);
this.cause = cause;
}

@Override
public {{annotated}} getCause() {
return cause;
}
}
```

See [examples project](https://github.com/sviperll/writejava4me/tree/master/writejava4me-examples) for more examples

License
-------

writejava4me is under BSD 3-clause license.

Flattr
------

[![Flattr this git repo](http://api.flattr.com/button/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=sviperll&url=https%3A%2F%2Fgithub.com%2Fsviperll%2Fwritejava4me&title=writejava4me&language=Java&tags=github&category=software)

Installation
------------

Use maven dependency:


com.github.sviperll
writejava4me
0.1