Skip to content
464 changes: 0 additions & 464 deletions jcodemodel/src/test/java/com/helger/jcodemodel/JRecordTest.java

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions jcodemodeltests/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.helger.jcodemodel</groupId>
<artifactId>jcodemodel-parent-pom</artifactId>
<version>4.2.0-SNAPSHOT</version>
</parent>
<artifactId>JCodeModel-Tests</artifactId>

<dependencies>
<dependency>
<groupId>com.helger</groupId>
<artifactId>jcodemodel</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<id>add-test-source</id>
<phase>generate-test-sources</phase>
<goals>
<goal>add-test-source</goal>
</goals>
<configuration>
<sources>
<source>src/generated/javatest</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.6.3</version>
<executions>
<execution>
<id>generatetestfiles</id>
<phase>generate-test-sources</phase>
<goals>
<goal>java</goal>
</goals>
<configuration>
<arguments>${project.basedir}</arguments>
<mainClass>com.helger.jcodemodel.compile.annotation.GenerateTestFiles</mainClass>
</configuration>
</execution>
</executions>
</plugin>
</plugins>

</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.basic;

public class Simple1 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.basic;

public class Simple2 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record AnnotatedPerson(@JRecordTestGen.RecordAnnotationExample String name, int age) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record ArrayRecord(String[] names, int[][] matrix) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record BasicPoint(int x, int y) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record Empty() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.helger.jcodemodel.tests.record;

public record NamedPoint(int x, int y, String name)
implements Comparable<NamedPoint>
{

public int compareTo(NamedPoint other) {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.helger.jcodemodel.tests.record;

public class Outer {

public record Inner(String value) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record Pair<T, U>(T first, U second) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record PairNumber<T extends Number>(T first, T second) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record Person(String name, Integer age) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.helger.jcodemodel.tests.record;

public record PointDistance(int x, int y) {

public double distance() {
return Math.sqrt(((x*x)+(y*y)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.helger.jcodemodel.tests.record;


/**
* Represents a 2D point.
*
* @param x
* the x coordinate
* @param y
* the y coordinate
*/
public record PointJavadoc(int x, int y) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.helger.jcodemodel.tests.record;

public record PointStatic(int x, int y) {
public static final PointStatic ORIGIN = new PointStatic(0, 0);

public static PointStatic of(int x, int y) {
return new PointStatic(x, y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.helger.jcodemodel.tests.record;

public record Range(int lo, int hi) {

public Range {
if (lo >hi) {
throw new IllegalArgumentException("High must be greater or equal to Low");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.helger.jcodemodel.tests.record;

public record RangeCanonical(int lo, int hi) {

public RangeCanonical(int lo, int hi) {
if (lo >hi) {
throw new IllegalArgumentException("lo must be < hi");
}
this.lo = lo;
this.hi = hi;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.helger.jcodemodel.tests.record;

public record SeriesVarArgs(String name, int... values) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package com.helger.jcodemodel.compile.annotation;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import com.helger.jcodemodel.JCodeModel;
import com.helger.jcodemodel.writer.JCMWriter;
import com.helger.jcodemodel.writer.ProgressCodeWriter.IProgressTracker;

public class GenerateTestFiles {

private static final String OUTPUT_DIR = "src/generated/javatest";

private static final String CLASS_SCAN_DIR = "src/main/java";

private final File outputDir;

private final File classScanDir;

public static void main(String[] args) {
String rootPath = args == null || args.length == 0 ? "." : args[0];
File outputFile = new File(rootPath, OUTPUT_DIR);
File classScanDir = new File(rootPath, CLASS_SCAN_DIR);
new GenerateTestFiles(outputFile, classScanDir)
.apply();
}

public GenerateTestFiles(File outputDir, File classScanDir) {
this.outputDir = outputDir;
this.classScanDir = classScanDir;
}

void apply() {
delete(outputDir);
outputDir.mkdirs();
scanClasses(classScanDir).forEach(this::applyCandidateClass);

}

void delete(File file) {
if (file.isDirectory()) {
for (File sub : file.listFiles()) {
delete(sub);
}
}
file.delete();
}

Stream<String> scanClasses(File rootDir) {
return scanClasses(rootDir, "", Stream.of());
}

Stream<String> scanClasses(File dir, String packageName, Stream<String> stream) {
List<String> newFound = new ArrayList<>();
if(!dir.isDirectory()) {
throw new RuntimeException("file " + dir.getAbsolutePath() + " expected to be a dir");
}
for (File child : dir.listFiles()) {
if (child.isDirectory()) {
stream = scanClasses(child, (packageName.isEmpty() ? "" : packageName + ".") + child.getName(), stream);

} else if (child.isFile() && child.getName().endsWith(".java")) {
newFound.add(packageName + "." + child.getName().replace(".java", ""));
}
}
if (!newFound.isEmpty()) {
stream = Stream.concat(stream, newFound.stream());
}
return stream;
}

void applyCandidateClass(String className) {
Class<?> clazz;
try {
clazz = Class.forName(className);
if (clazz.getAnnotation(TestJCM.class) != null) {
runGeneration(clazz);
}
} catch (ClassNotFoundException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | InstantiationException | NoSuchMethodException | SecurityException
| IOException e) {
throw new RuntimeException(e);
}
}

private void runGeneration(Class<?> clazz)
throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, InstantiationException,
NoSuchMethodException, SecurityException, IOException {
for (Method m : clazz.getDeclaredMethods()) {
// only apply to methods public, with 0 args, and that produce a JCodeModel
if ((m.getModifiers() & Modifier.PUBLIC) > 0
&& m.getParameterCount() == 0) {
if (m.getReturnType().equals(JCodeModel.class)) {
m.setAccessible(true);
JCodeModel produced = null;
if ((m.getModifiers() & Modifier.STATIC) > 0) {
produced = (JCodeModel) m.invoke(null);
} else {
Object inst = clazz.getDeclaredConstructor().newInstance();
produced = (JCodeModel) m.invoke(inst);
}
if (produced != null) {
new JCMWriter(produced).build(outputDir, (IProgressTracker) null);
}
}
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.helger.jcodemodel.compile.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TestJCM {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.helger.jcodemodel.tests.basic;

import com.helger.jcodemodel.JCodeModel;
import com.helger.jcodemodel.compile.annotation.TestJCM;
import com.helger.jcodemodel.exceptions.JCodeModelException;

@TestJCM
public class SimpleClassGenerating {

public JCodeModel createSimple1() throws JCodeModelException {
JCodeModel cm = new JCodeModel();
cm._class("com.helger.jcodemodel.tests.basic.Simple1");
return cm;
}

public JCodeModel createSimple2() throws JCodeModelException {
JCodeModel cm = new JCodeModel();
cm._class("com.helger.jcodemodel.tests.basic.Simple2");
return cm;
}

/** protected so should not be selected */
protected JCodeModel protectedCall() {
return null;
}

}
Loading