Compare commits

...

3 commits
master ... jsb

Author SHA1 Message Date
Augusto Dwenger J. 8b9c4d8064 jsb: Add a link for a java code generation lib 2022-08-31 17:41:04 +02:00
Augusto Dwenger J. 1eb03ed692 jsb: Rework method construction
This makes it not that type save and not so easy to generate with code,
but I hope to improve it in the future. Currently I am just trying to
make it work and get a feeling for possible improvements.
2022-08-31 17:41:04 +02:00
Augusto Dwenger J. fb831d072c jsb: Init module 2022-08-31 17:40:52 +02:00
14 changed files with 237 additions and 5 deletions

27
jsb/pom.xml Normal file
View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<artifactId>parent-pom</artifactId>
<groupId>de.hhhammer.prozessor</groupId>
<version>0.1.0-SNAPSHOT</version>
</parent>
<artifactId>jsb</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-source-builder</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View file

@ -0,0 +1,8 @@
package de.hhhammer.prozessor.jsb;
//https://github.com/makkax/JavaCodeGenerator
public interface ExpressionWithArguments<R> extends ImportRepresentation {
String representArguments();
String representExpression();
}

View file

@ -0,0 +1,6 @@
package de.hhhammer.prozessor.jsb;
public interface Field<T> extends Type<T> {
T getValue();
}

View file

@ -0,0 +1,5 @@
package de.hhhammer.prozessor.jsb;
public interface ImportRepresentation {
String representImports();
}

View file

@ -0,0 +1,37 @@
package de.hhhammer.prozessor.jsb;
public enum JAccessor implements Representation {
/**
* The modifier {@code public}
*/
PUBLIC("public"),
/**
* The modifier {@code protected}
*/
PROTECTED("protected"),
/**
* The modifier {@code private}
*/
PRIVATE("private"),
/**
* The modifier {@code abstract}
*/
ABSTRACT("abstract"),
/**
* The modifier {@code default}
*
* @since 1.8
*/
DEFAULT("");
private final String representation;
JAccessor(String representation) {
this.representation = representation;
}
@Override
public String represent() {
return this.representation;
}
}

View file

@ -0,0 +1,4 @@
package de.hhhammer.prozessor.jsb;
public record JClass() {
}

View file

@ -0,0 +1,16 @@
package de.hhhammer.prozessor.jsb;
public record JMethod<R extends Type<?>>(JAccessor accessor, JModifier modifier, R returnType, Name methodName,
ExpressionWithArguments<R> expressionWithArguments) implements Representation, ImportRepresentation {
@Override
public String representImports() {
return expressionWithArguments.representImports();
}
@Override
public String represent() {
return accessor.represent() + " " + modifier.represent() + " " + returnType.represent() + " " + methodName.represent() + "(" + expressionWithArguments.representArguments() + ")" + " " + "{" + "\n" +
"\t" + expressionWithArguments.representExpression() + "\n" +
"}" + "\n";
}
}

View file

@ -0,0 +1,18 @@
package de.hhhammer.prozessor.jsb;
public enum JModifier implements Representation {
NONE(""),
FINAL("final"),
STATIC("static");
private final String representation;
JModifier(final String representation) {
this.representation = representation;
}
@Override
public String represent() {
return this.representation;
}
}

View file

@ -0,0 +1,4 @@
package de.hhhammer.prozessor.jsb;
public interface Name extends Representation {
}

View file

@ -0,0 +1,5 @@
package de.hhhammer.prozessor.jsb;
public interface Representation {
String represent();
}

View file

@ -0,0 +1,4 @@
package de.hhhammer.prozessor.jsb;
public interface Type<T> extends Representation, ImportRepresentation {
}

View file

@ -0,0 +1,2 @@
module de.hhhammer.prozessor.jsb {
}

View file

@ -0,0 +1,88 @@
package de.hhhammer.prozessor.jsb;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class JMethodTest {
@Test
void should() {
final var method = new JMethod<>(JAccessor.PUBLIC, JModifier.FINAL, new IntegerType(), new MethodName(), new MethodLogic());
final var expect = """
public final Integer methodFoo(Integer arg0, Integer arg1) {
\treturn arg0 + arg1;
}
""";
assertEquals(expect, method.represent());
}
static class IntegerType implements Type<Integer> {
@Override
public String representImports() {
return "";
}
@Override
public String represent() {
return Integer.class.getSimpleName();
}
}
static class IntegerField implements Field<Integer> {
private final Integer value;
IntegerField(final Integer value) {
this.value = value;
}
@Override
public Integer getValue() {
return value;
}
@Override
public String representImports() {
return "";
}
@Override
public String represent() {
return Integer.class.getSimpleName();
}
}
static class MethodName implements Name {
@Override
public String represent() {
return "methodFoo";
}
}
static class MethodLogic implements ExpressionWithArguments<IntegerType> {
private final IntegerField arg0 = new IntegerField(1);
private final IntegerField arg1 = new IntegerField(2);
@Override
public String representArguments() {
return arg0.represent() + " arg0, " + arg1.represent() + " arg1";
}
@Override
public String representExpression() {
return "return arg0 + arg1;";
}
@Override
public String representImports() {
return "";
}
}
}

18
pom.xml
View file

@ -16,6 +16,12 @@
<junit.version>5.8.2</junit.version>
</properties>
<modules>
<module>core</module>
<module>app</module>
<module>jsb</module>
</modules>
<dependencyManagement>
<dependencies>
<dependency>
@ -30,6 +36,13 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>4.6.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>
@ -82,9 +95,4 @@
</pluginManagement>
</build>
<modules>
<module>core</module>
<module>app</module>
</modules>
</project>