Skip to content

Commit 44a3739

Browse files
committed
Merge remote-tracking branch
'origin/issue/315_Reorganize_dsf-tools_Modules' into develop_2
2 parents 59aad2b + b4c38be commit 44a3739

File tree

2 files changed

+82
-46
lines changed

2 files changed

+82
-46
lines changed

dsf-maven/dsf-maven-plugin/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@
3131
<scope>provided</scope>
3232
</dependency>
3333

34-
<dependency>
35-
<groupId>dev.dsf</groupId>
36-
<artifactId>dsf-common-documentation</artifactId>
37-
<exclusions>
38-
<exclusion>
39-
<groupId>*</groupId>
40-
<artifactId>*</artifactId>
41-
</exclusion>
42-
</exclusions>
43-
</dependency>
4434
<dependency>
4535
<groupId>dev.dsf</groupId>
4636
<artifactId>dsf-bpe-process-api-v1</artifactId>

dsf-maven/dsf-maven-plugin/src/main/java/dev/dsf/maven/config/ConfigDocGenerator.java

Lines changed: 82 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.io.InputStream;
6+
import java.lang.annotation.Annotation;
67
import java.lang.reflect.Field;
8+
import java.lang.reflect.InvocationTargetException;
79
import java.net.MalformedURLException;
810
import java.net.URL;
911
import java.net.URLClassLoader;
@@ -12,7 +14,6 @@
1214
import java.nio.file.Path;
1315
import java.nio.file.StandardCopyOption;
1416
import java.nio.file.StandardOpenOption;
15-
import java.util.ArrayList;
1617
import java.util.Collection;
1718
import java.util.Comparator;
1819
import java.util.List;
@@ -38,8 +39,6 @@
3839
import org.slf4j.LoggerFactory;
3940
import org.springframework.beans.factory.annotation.Value;
4041

41-
import dev.dsf.common.documentation.Documentation;
42-
4342
public class ConfigDocGenerator
4443
{
4544
private static final Logger logger = LoggerFactory.getLogger(ConfigDocGenerator.class);
@@ -55,16 +54,55 @@ private static record DocumentationEntry(String propertyName, String value)
5554
}
5655

5756
private final Path projectBuildDirectory;
58-
private final List<String> compileClasspathElements = new ArrayList<>();
57+
private final URLClassLoader classLoader;
58+
private final Class<? extends Annotation> coreDocumentationAnnotationClass;
5959

6060
public ConfigDocGenerator(Path projectBuildDirectory, List<String> compileClasspathElements)
6161
{
6262
Objects.requireNonNull(projectBuildDirectory, "projectBuildDirectory");
6363

6464
this.projectBuildDirectory = projectBuildDirectory;
65+
classLoader = classLoader(compileClasspathElements);
66+
coreDocumentationAnnotationClass = loadCoreDocumentationAnnotation(classLoader);
67+
}
6568

66-
if (compileClasspathElements != null)
67-
this.compileClasspathElements.addAll(compileClasspathElements);
69+
private static URLClassLoader classLoader(List<String> compileClasspathElements)
70+
{
71+
URL[] classpathElements = compileClasspathElements.stream().map(ConfigDocGenerator::toUrl)
72+
.filter(Objects::nonNull).toArray(URL[]::new);
73+
74+
return new URLClassLoader(classpathElements, Thread.currentThread().getContextClassLoader());
75+
}
76+
77+
private static URL toUrl(String path)
78+
{
79+
try
80+
{
81+
return new File(path).toURI().toURL();
82+
}
83+
catch (MalformedURLException exception)
84+
{
85+
logger.warn("Could not transform path '{}' to url, returning null - {}", path, exception.getMessage());
86+
return null;
87+
}
88+
}
89+
90+
@SuppressWarnings("unchecked")
91+
private static Class<? extends Annotation> loadCoreDocumentationAnnotation(URLClassLoader classLoader)
92+
{
93+
try
94+
{
95+
Class<?> clazz = classLoader.loadClass("dev.dsf.common.documentation.Documentation");
96+
if (clazz.isAnnotation())
97+
return (Class<? extends Annotation>) clazz;
98+
}
99+
catch (ClassNotFoundException e)
100+
{
101+
logger.info(
102+
"DSF core documentation annotation not found on classpath, for process plugins this is not an error");
103+
}
104+
105+
return null;
68106
}
69107

70108
public void generateDocumentation(List<String> configDocPackages)
@@ -79,12 +117,14 @@ private void generateDocumentation(String configDocPackage)
79117

80118
moveExistingToBackup(file);
81119

82-
URLClassLoader classLoader = classLoader();
83120
Reflections reflections = createReflections(classLoader, configDocPackage);
84121

85-
Set<Field> dsfFields = reflections.getFieldsAnnotatedWith(Documentation.class);
86-
if (!dsfFields.isEmpty())
87-
writeFields(dsfFields, dsfDocumentationGenerator(), file, configDocPackage);
122+
if (coreDocumentationAnnotationClass != null)
123+
{
124+
Set<Field> dsfFields = reflections.getFieldsAnnotatedWith(coreDocumentationAnnotationClass);
125+
if (!dsfFields.isEmpty())
126+
writeFields(dsfFields, dsfDocumentationGenerator(), file, configDocPackage);
127+
}
88128

89129
// v1
90130
Set<Field> processFieldsV1 = reflections
@@ -117,27 +157,6 @@ private Reflections createReflections(ClassLoader classLoader, String workingPac
117157
return new Reflections(configurationBuilder);
118158
}
119159

120-
private URLClassLoader classLoader()
121-
{
122-
URL[] classpathElements = compileClasspathElements.stream().map(this::toUrl).filter(Objects::nonNull)
123-
.toArray(URL[]::new);
124-
125-
return new URLClassLoader(classpathElements, Thread.currentThread().getContextClassLoader());
126-
}
127-
128-
private URL toUrl(String path)
129-
{
130-
try
131-
{
132-
return new File(path).toURI().toURL();
133-
}
134-
catch (MalformedURLException exception)
135-
{
136-
logger.warn("Could not transform path '{}' to url, returning null - {}", path, exception.getMessage());
137-
return null;
138-
}
139-
}
140-
141160
private <D> List<String> getPluginProcessNames(Class<D> processPluginDefinitionType, ClassLoader classLoader,
142161
String workingPackage, BiFunction<D, ClassLoader, List<String>> definitionToProcessNames)
143162
{
@@ -338,7 +357,7 @@ private Function<Field, DocumentationEntry> dsfDocumentationGenerator()
338357
{
339358
return field ->
340359
{
341-
Documentation documentation = field.getAnnotation(Documentation.class);
360+
Object documentation = field.getAnnotation(coreDocumentationAnnotationClass);
342361
Value value = field.getAnnotation(Value.class);
343362

344363
PropertyNameAndDefaultValue propertyNameAndDefaultValue = getPropertyNameAndDefaultValue(value);
@@ -352,11 +371,12 @@ private Function<Field, DocumentationEntry> dsfDocumentationGenerator()
352371
? String.format("%s or %s_FILE", initialEnvironment, initialEnvironment)
353372
: initialEnvironment;
354373

355-
String required = getDocumentationString("Required", documentation.required() ? "Yes" : "No");
374+
String required = getDocumentationString("Required", getBoolean(documentation, "required") ? "Yes" : "No");
356375

357-
String description = getDocumentationString("Description", documentation.description());
358-
String recommendation = getDocumentationString("Recommendation", documentation.recommendation());
359-
String example = getDocumentationStringMonospace("Example", documentation.example());
376+
String description = getDocumentationString("Description", getString(documentation, "description"));
377+
String recommendation = getDocumentationString("Recommendation",
378+
getString(documentation, "recommendation"));
379+
String example = getDocumentationStringMonospace("Example", getString(documentation, "example"));
360380

361381
String defaultValueString = devalueValue != null && devalueValue.length() > 1
362382
&& !"#{null}".equals(devalueValue) ? getDocumentationStringMonospace("Default", devalueValue) : "";
@@ -369,6 +389,32 @@ private Function<Field, DocumentationEntry> dsfDocumentationGenerator()
369389
};
370390
}
371391

392+
private boolean getBoolean(Object coreDocumentationAnnotation, String methodName)
393+
{
394+
try
395+
{
396+
return Boolean.TRUE.equals(
397+
coreDocumentationAnnotationClass.getDeclaredMethod(methodName).invoke(coreDocumentationAnnotation));
398+
}
399+
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e)
400+
{
401+
throw new IllegalArgumentException(e);
402+
}
403+
}
404+
405+
private String getString(Object coreDocumentationAnnotation, String methodName)
406+
{
407+
try
408+
{
409+
return (String) coreDocumentationAnnotationClass.getDeclaredMethod(methodName)
410+
.invoke(coreDocumentationAnnotation);
411+
}
412+
catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | SecurityException e)
413+
{
414+
throw new IllegalArgumentException(e);
415+
}
416+
}
417+
372418
private String getDocumentationStringMonospace(String title, String value)
373419
{
374420
if (title == null || title.isBlank() || value == null || value.isBlank())

0 commit comments

Comments
 (0)