writejava4me-examples

Artifact to use as universal maven parent

License

License

GroupId

GroupId

com.github.sviperll
ArtifactId

ArtifactId

writejava4me-examples
Last Version

Last Version

0.3
Release Date

Release Date

Type

Type

jar
Description

Description

writejava4me-examples
Artifact to use as universal maven parent

Download writejava4me-examples

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sviperll/writejava4me-examples/ -->
<dependency>
    <groupId>com.github.sviperll</groupId>
    <artifactId>writejava4me-examples</artifactId>
    <version>0.3</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.sviperll/writejava4me-examples/
implementation 'com.github.sviperll:writejava4me-examples:0.3'
// https://jarcasting.com/artifacts/com.github.sviperll/writejava4me-examples/
implementation ("com.github.sviperll:writejava4me-examples:0.3")
'com.github.sviperll:writejava4me-examples:jar:0.3'
<dependency org="com.github.sviperll" name="writejava4me-examples" rev="0.3">
  <artifact name="writejava4me-examples" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.sviperll', module='writejava4me-examples', version='0.3')
)
libraryDependencies += "com.github.sviperll" % "writejava4me-examples" % "0.3"
[com.github.sviperll/writejava4me-examples "0.3"]

Dependencies

compile (1)

Group / Artifact Type Version
com.github.sviperll : writejava4me jar 0.3

test (1)

Group / Artifact Type Version
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

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:

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.

@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:

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

RuntimeExceptionWrapper.mustache located in resources folder:

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 for more examples

License

writejava4me is under BSD 3-clause license.

Flattr

Flattr this git repo

Installation

Use maven dependency:

<dependency>
    <groupId>com.github.sviperll</groupId>
    <artifactId>writejava4me</artifactId>
    <version>0.1</version>
</dependency>

Versions

Version
0.3
0.1