Skip to content

Commit

Permalink
Generation based on all class in a package
Browse files Browse the repository at this point in the history
(packageName parameter)
  • Loading branch information
fugerit79 committed Apr 1, 2024
1 parent 6fec55c commit dd1ae99
Show file tree
Hide file tree
Showing 12 changed files with 138 additions and 63 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Generation based on all class in a package (packageName parameter)

## [1.3.2] - 2024-04-01

### Added
Expand Down
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,16 @@ top level properties :

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 metadata |
| typeReachable | *none* | *false* | *string* | type for typeReachable condition |
| name | default | required | type | description |
|-------------------|---------|----------|-----------|---------------------------------------------------------------------------------|
| className | *none* | *false* | *string* | fully qualified class name (alternative to *packageName*) |
| packageName | *none* | *false* | *string* | fully qualified package name (alternative to *className*) (*) |
| excludeClassNames | *none* | *false* | *string* | if *packageName* is set, comma separeted list of class simple names to excludes |
| 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 |

(*) currently package packed in a jar are not supported.

## 4. Generate reflect-config.json file

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<properties>
<java-version-compliance>8</java-version-compliance>
<fj-version>8.5.3</fj-version>
</properties>
</properties>

<organization>
<url>https://www.fugerit.org</url>
Expand Down
5 changes: 1 addition & 4 deletions src/main/config/native-helper-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ generate:
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
- packageName: org.fugerit.java.nhg.config.model
mode: getters_setters
typeReachable: org.fugerit.java.nhg.config.NativeHelperFacade
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.fugerit.java.nhg.config;


import lombok.extern.slf4j.Slf4j;
import org.fugerit.java.core.cfg.ConfigRuntimeException;
import org.fugerit.java.core.function.SafeFunction;
import org.fugerit.java.core.lang.helpers.ClassHelper;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
public class AccessingAllClassesInPackage {

public static Set<Class<?>> findAllClassesUsingClassLoader(String packageName) throws ConfigRuntimeException {
return SafeFunction.get( () -> {
InputStream stream = ClassHelper.getDefaultClassLoader().getResourceAsStream(packageName.replaceAll("[.]", "/"));
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
return reader.lines()
.filter(line -> line.endsWith(".class"))
.map(line -> getClass(line, packageName))
.collect(Collectors.toSet());
} );
}

private static Class<?> getClass(String className, String packageName) {
return SafeFunction.get( () -> {
log.debug( "packageName : {}, className : {}", packageName, className );
return Class.forName(packageName+"."+className.substring(0, className.lastIndexOf('.')));
} );
}

}
41 changes: 29 additions & 12 deletions src/main/java/org/fugerit/java/nhg/config/NativeHelperFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
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.GenerateConfig;
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.*;
import java.util.ArrayList;
import java.util.List;
import java.util.*;

@Slf4j
public class NativeHelperFacade {
Expand Down Expand Up @@ -64,20 +64,37 @@ private static ReflectConfigUtil getUtils( String mode ) {
}
}

private static void handleClass( final Class<?> c, final List<Entry> list, final GenerateConfig g ) {
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);
}

public static List<Entry> generateEntries( NativeHelperConfig config ) {
final List<Entry> list = new ArrayList<>();
config.getGenerate().forEach(g -> {
if (StringUtils.isNotEmpty(g.getClassName())) {
SafeFunction.apply( () -> {
Class<?> c = ClassHelper.getDefaultClassLoader().loadClass(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);
} );
Class<?> c = SafeFunction.get( () -> ClassHelper.getDefaultClassLoader().loadClass(g.getClassName() ) );
log.info( "generate class reflect config : {}", c );
handleClass( c, list, g );
} else if ( StringUtils.isNotEmpty(g.getPackageName()) ) {
Set<String> excludeClassNames = new HashSet<>();
if ( StringUtils.isNotEmpty( g.getExcludeClassNames() ) ) {
excludeClassNames.addAll( Arrays.asList( g.getExcludeClassNames().split( "," ) ) );
}
AccessingAllClassesInPackage.findAllClassesUsingClassLoader(g.getPackageName()).stream()
.filter(
c -> !excludeClassNames.contains( c.getSimpleName() )
).forEach( c -> {
log.info( "generate class reflect config : {} (from package : {})", c, g.getPackageName() );
handleClass( c, list, g );
} );
} else {
throw new ConfigRuntimeException( "className or packageName must be provided for each entry" );
}
});
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ public class GenerateConfig {
@Getter @Setter
private String className;

@Getter @Setter
private String packageName;

@Getter @Setter
private String excludeClassNames;

@Getter @Setter
private String mode;

Expand Down
56 changes: 34 additions & 22 deletions src/main/resources/META-INF/native-image/reflect-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"condition" : {
"typeReachable" : "org.fugerit.java.nhg.config.NativeHelperFacade"
},
"name" : "org.fugerit.java.nhg.config.model.NativeHelperConfig",
"name" : "org.fugerit.java.nhg.config.model.GenerateConfig",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
Expand All @@ -87,26 +87,44 @@
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getGenerate",
"name" : "getClassName",
"parameterTypes" : [ ]
}, {
"name" : "getReflectConfigJsonOutputPath",
"name" : "getExcludeClassNames",
"parameterTypes" : [ ]
}, {
"name" : "setCreateParentDirectory",
"parameterTypes" : [ "boolean" ]
"name" : "getMode",
"parameterTypes" : [ ]
}, {
"name" : "setGenerate",
"parameterTypes" : [ "java.util.List" ]
"name" : "getPackageName",
"parameterTypes" : [ ]
}, {
"name" : "setReflectConfigJsonOutputPath",
"name" : "getTypeReachable",
"parameterTypes" : [ ]
}, {
"name" : "setClassName",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setExcludeClassNames",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setMode",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setPackageName",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setSkipConstructors",
"parameterTypes" : [ "boolean" ]
}, {
"name" : "setTypeReachable",
"parameterTypes" : [ "java.lang.String" ]
} ]
}, {
"condition" : {
"typeReachable" : "org.fugerit.java.nhg.config.NativeHelperFacade"
},
"name" : "org.fugerit.java.nhg.config.model.GenerateConfig",
"name" : "org.fugerit.java.nhg.config.model.NativeHelperConfig",
"fields" : [ ],
"methods" : [ {
"name" : "<init>",
Expand All @@ -115,25 +133,19 @@
"name" : "getClass",
"parameterTypes" : [ ]
}, {
"name" : "getClassName",
"parameterTypes" : [ ]
}, {
"name" : "getMode",
"name" : "getGenerate",
"parameterTypes" : [ ]
}, {
"name" : "getTypeReachable",
"name" : "getReflectConfigJsonOutputPath",
"parameterTypes" : [ ]
}, {
"name" : "setClassName",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setMode",
"parameterTypes" : [ "java.lang.String" ]
}, {
"name" : "setSkipConstructors",
"name" : "setCreateParentDirectory",
"parameterTypes" : [ "boolean" ]
}, {
"name" : "setTypeReachable",
"name" : "setGenerate",
"parameterTypes" : [ "java.util.List" ]
}, {
"name" : "setReflectConfigJsonOutputPath",
"parameterTypes" : [ "java.lang.String" ]
} ]
} ]
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class TestNativeHelperFacade {

private static final String TEST_CONFIG_BLANK = BASE_TEST_CONFIG_PATH+"native-helper-config-blank.yaml";

private static final String TEST_CONFIG_PACKAGE = BASE_TEST_CONFIG_PATH+"native-helper-config-package.yaml";

private NativeHelperConfig loadConfig(String path ) {
NativeHelperConfig config = NativeHelperFacade.loadConfig( path );
config.getGenerate().forEach( g -> log.info( "generation config : {}", g ) );
Expand All @@ -44,8 +46,9 @@ private void printConfig( NativeHelperConfig config ) {
}

private Map<String, Boolean> CONFIG_MAP = Stream.of(
new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_1, true),
new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_2, false),
//new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_1, true),
//new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_2, false),
new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_PACKAGE, true),
new AbstractMap.SimpleImmutableEntry<>(TEST_CONFIG_BLANK, true))
. collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class TestSampleNativeHelperFacade {

@Test
void nativeHelperFacadeSample() throws IOException {
// path the native-helper-config.yaml file
// path the native-helper-config-package.yaml file
String path = "src/test/resources/tool/config/native-helper-config-1.yaml";
// reads configuration
NativeHelperConfig config = NativeHelperFacade.loadConfig( path );
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/tool/config/native-helper-config-package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# native-helper-graalvm generation configuration
---
reflectConfigJsonOutputPath: target/native-image-package/reflect-config.json
createParentDirectory: true
generate:
- packageName: org.fugerit.java.nhg.reflect.config
excludeClassNames: EntryHelper
mode: getters
- packageName: org.fugerit.java.nhg.config.model
mode: getters_setters
typeReachable: org.fugerit.java.nhg.config.NativeHelperFacade
15 changes: 0 additions & 15 deletions src/test/resources/tool/config/native-helper-config.yaml

This file was deleted.

0 comments on commit dd1ae99

Please sign in to comment.