Skip to content

Commit

Permalink
reflect-config.json generation capability
Browse files Browse the repository at this point in the history
  • Loading branch information
fugerit79 committed Mar 31, 2024
1 parent 29c5302 commit 1798d05
Show file tree
Hide file tree
Showing 10 changed files with 260 additions and 10 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- reflect-config.json generation capability
- NativeHelperFacade support for typeReachable condition

## [1.2.0] - 2024-03-31

### Added
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,20 @@ Here is a full [code example](src/test/java/test/org/fugerit/java/nhg/TestSample

top level properties :

| name | default | required | type | description |
|----------|---------|----------|------------------|-----------------------------|
| generate | *none* | *true* | list of entries | list of entries to generate |
| name | default | required | type | description |
|-----------------------------|---------|----------|-----------------|------------------------------------------------------------------------|
| reflectConfigJsonOutputPath | *none* | *false* | *string* | default generation path for *reflect-config.json* file |
| createParentDirectory | *false* | *false* | *boolean* | if *true* will generate *reflectConfigJsonOutputPath* parent directory |
| generate | *none* | *true* | list of entries | list of entries to generate |

entries properties :

| name | default | required | type | description |
|----------------|---------|----------|-----------|----------------------------------------------------------------|
| className | *none* | *true* | *string* | fully qualified class name |
| mode | *none* | *true* | *string* | accept values : *getters*, *setters*, *getters_setters*, *all* |
| skipGenerators | *false* | *false* | *boolean* | if set to *true* will not generate constructors methadata |
| name | default | required | type | description |
|-----------------|---------|----------|-----------|----------------------------------------------------------------|
| className | *none* | *true* | *string* | fully qualified class name |
| mode | *none* | *true* | *string* | accept values : *getters*, *setters*, *getters_setters*, *all* |
| skipGenerators | *false* | *false* | *boolean* | if set to *true* will not generate constructors metadata |
| typeReachable | *none* | *false* | *string* | type for typeReachable condition |

## 4. Generate reflect-config.json file

Expand Down
19 changes: 19 additions & 0 deletions src/main/config/native-helper-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# native-helper-graalvm generation configuration
---
reflectConfigJsonOutputPath: src/main/resources/META-INF/native-image/reflect-config.json
createParentDirectory: true
generate:
- className: org.fugerit.java.nhg.reflect.config.Entry
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryCondition
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryField
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryMethod
mode: getters
- className: org.fugerit.java.nhg.config.model.NativeHelperConfig
mode: getters_setters
typeReachable: org.fugerit.java.nhg.config.NativeHelperFacade
- className: org.fugerit.java.nhg.config.model.GenerateConfig
mode: getters_setters
typeReachable: org.fugerit.java.nhg.config.NativeHelperFacade
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.io.helper.StreamHelper;
import org.fugerit.java.core.lang.helpers.StringUtils;
import org.fugerit.java.core.util.ObjectUtils;
import org.fugerit.java.nhg.GenerateReflectConfig;
import org.fugerit.java.nhg.ReflectConfigUtil;
import org.fugerit.java.nhg.config.model.NativeHelperConfig;
import org.fugerit.java.nhg.reflect.config.Entry;
import org.fugerit.java.nhg.reflect.config.EntryCondition;
import org.fugerit.java.nhg.reflect.config.EntryHelper;

import java.io.InputStream;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class NativeHelperFacade {

private NativeHelperFacade() {}
Expand All @@ -28,6 +32,8 @@ private NativeHelperFacade() {}

public static final String MODE_ALL = "all";

public static final String PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH = "reflectConfigJsonOutputPath";

public static NativeHelperConfig loadConfig( String path ) {
return SafeFunction.get( () -> {
try (InputStream is = StreamHelper.resolveStream( path )) {
Expand Down Expand Up @@ -65,6 +71,9 @@ public static List<Entry> generateEntries( NativeHelperConfig config ) {
Class<?> c = Class.forName(g.getClassName());
ReflectConfigUtil utils = getUtils( g.getMode() );
Entry entry = utils.toEntry( c, !g.isSkipConstructors() );
if ( StringUtils.isNotEmpty( g.getTypeReachable() ) ) {
entry.setCondition( new EntryCondition( g.getTypeReachable() ) );
}
EntryHelper.fixedOrder( entry );
list.add(entry);
} );
Expand All @@ -73,4 +82,28 @@ public static List<Entry> generateEntries( NativeHelperConfig config ) {
return list;
}

public static List<Entry> generate( NativeHelperConfig config, Writer reflectConfigJsonWriter ) {
List<Entry> entries = generateEntries( config );
GenerateReflectConfig generateReflectConfig = new GenerateReflectConfig();
generateReflectConfig.generate( reflectConfigJsonWriter, entries );
return entries;
}

public static NativeHelperConfig loadAndGenerate(String nativeHelperConfigPath ) throws IOException {
NativeHelperConfig config = loadConfig( nativeHelperConfigPath );
String reflectConfigJsonOutputPath = config.getReflectConfigJsonOutputPath();
if ( StringUtils.isNotEmpty( reflectConfigJsonOutputPath ) ) {
File reflectConfigJsonFile = new File( reflectConfigJsonOutputPath );
if ( config.isCreateParentDirectory() ) {
log.info( "createParentDirectory : {} -> {}", reflectConfigJsonFile.getParentFile(), reflectConfigJsonFile.getParentFile().mkdirs() );
}
try (FileWriter reflectConfigJsonWriter = new FileWriter( reflectConfigJsonFile ) ) {
generate( config, reflectConfigJsonWriter );
}
} else {
throw new ConfigRuntimeException( String.format( "Param %s not set.", PARAM_REFLECT_CONFIG_JSON_OUTPUT_PATH ) );
}
return config;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ public class GenerateConfig {
@Getter @Setter
private boolean skipConstructors;

@Getter @Setter
private String typeReachable;

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
@ToString
public class NativeHelperConfig {

@Getter @Setter
private String reflectConfigJsonOutputPath;

@Getter @Setter
private boolean createParentDirectory;

@Getter @Setter
private List<GenerateConfig> generate;

Expand Down
139 changes: 139 additions & 0 deletions src/main/resources/META-INF/native-image/reflect-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
[ {
"name" : "org.fugerit.java.nhg.reflect.config.Entry",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "<init>",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getCondition",
"parameterTypes" : [ ]
}, {
"name" : "getFields",
"parameterTypes" : [ ]
}, {
"name" : "getMethods",
"parameterTypes" : [ ]
}, {
"name" : "getName",
"parameterTypes" : [ ]
} ]
}, {
"name" : "org.fugerit.java.nhg.reflect.config.EntryCondition",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "<init>",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getTypeReachable",
"parameterTypes" : [ ]
} ]
}, {
"name" : "org.fugerit.java.nhg.reflect.config.EntryField",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "<init>",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getName",
"parameterTypes" : [ ]
} ]
}, {
"name" : "org.fugerit.java.nhg.reflect.config.EntryMethod",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "<init>",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getName",
"parameterTypes" : [ ]
}, {
"name" : "getParameterTypes",
"parameterTypes" : [ ]
} ]
}, {
"condition" : {
"typeReachable" : "org.fugerit.java.nhg.config.NativeHelperFacade"
},
"name" : "org.fugerit.java.nhg.config.model.NativeHelperConfig",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getGenerate",
"parameterTypes" : [ ]
}, {
"name" : "getReflectConfigJsonOutputPath",
"parameterTypes" : [ ]
}, {
"name" : "setCreateParentDirectory",
"parameterTypes" : [ "boolean" ]
}, {
"name" : "setGenerate",
"parameterTypes" : [ "java.util.List" ]
}, {
"name" : "setReflectConfigJsonOutputPath",
"parameterTypes" : [ "java.lang.String" ]
} ]
}, {
"condition" : {
"typeReachable" : "org.fugerit.java.nhg.config.NativeHelperFacade"
},
"name" : "org.fugerit.java.nhg.config.model.GenerateConfig",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
"parameterTypes" : [ ]
}, {
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getClassName",
"parameterTypes" : [ ]
}, {
"name" : "getMode",
"parameterTypes" : [ ]
}, {
"name" : "getTypeReachable",
"parameterTypes" : [ ]
}, {
"name" : "setClassName",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setMode",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setSkipConstructors",
"parameterTypes" : [ "boolean" ]
}, {
"name" : "setTypeReachable",
"parameterTypes" : [ "java.lang.String" ]
} ]
} ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package test.org.fugerit.java.nhg;

import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.nhg.GenerateReflectConfig;
import org.fugerit.java.nhg.config.NativeHelperFacade;
import org.fugerit.java.nhg.config.model.NativeHelperConfig;
import org.fugerit.java.nhg.reflect.config.Entry;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

@Slf4j
class TestGenerateLibraryConfiguration {

private static final String NATIVE_HELPER_CONFIG_PATH = "src/main/config/native-helper-config.yaml";

@Test
void generateLibraryConfiguration() throws IOException {
// reads configuration
NativeHelperConfig config = NativeHelperFacade.loadAndGenerate( NATIVE_HELPER_CONFIG_PATH );
Assertions.assertNotNull( config );
}

}
2 changes: 1 addition & 1 deletion src/test/resources/tool/config/native-helper-config-1.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#Sample native helper config file
# Generator
---
generate:
- className: org.fugerit.java.nhg.reflect.config.Entry
Expand Down
15 changes: 15 additions & 0 deletions src/test/resources/tool/config/native-helper-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#Sample native helper config file
---
generate:
- className: org.fugerit.java.nhg.reflect.config.Entry
skipConstructors: true
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryCondition
skipConstructors: true
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryField
skipConstructors: true
mode: getters
- className: org.fugerit.java.nhg.reflect.config.EntryMethod
skipConstructors: true
mode: getters

0 comments on commit 1798d05

Please sign in to comment.