Skip to content

Commit efd4604

Browse files
committed
Init
1 parent 4826429 commit efd4604

32 files changed

+1696
-0
lines changed

.github/dependabot.yml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "maven" # See documentation for possible values
9+
directory: "/" # Location of package manifests
10+
schedule:
11+
interval: "weekly"

.github/workflows/ci.yml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
branches:
6+
- '**'
7+
pull_request:
8+
branches:
9+
- '**'
10+
11+
jobs:
12+
build:
13+
runs-on: ubuntu-latest
14+
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
15+
steps:
16+
- uses: actions/checkout@v2
17+
- name: Set up JDK 17
18+
uses: actions/setup-java@v2
19+
with:
20+
java-version: '17'
21+
distribution: 'temurin'
22+
- name: Build with Maven
23+
run: ./mvnw --batch-mode clean verify

.gitignore

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### JetBrains template
3+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
4+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
5+
6+
# User-specific stuff:
7+
.idea
8+
9+
# Sensitive or high-churn files:
10+
.idea/dataSources.ids
11+
.idea/dataSources.xml
12+
.idea/dataSources.local.xml
13+
.idea/sqlDataSources.xml
14+
.idea/dynamic.xml
15+
.idea/uiDesigner.xml
16+
17+
# Gradle:
18+
.idea/gradle.xml
19+
.idea/libraries
20+
21+
# Mongo Explorer plugin:
22+
.idea/mongoSettings.xml
23+
24+
## File-based project format:
25+
*.iws
26+
*.iml
27+
28+
## Plugin-specific files:
29+
30+
# IntelliJ
31+
/out/
32+
33+
# mpeltonen/sbt-idea plugin
34+
.idea_modules/
35+
36+
# JIRA plugin
37+
atlassian-ide-plugin.xml
38+
39+
# Crashlytics plugin (for Android Studio and IntelliJ)
40+
com_crashlytics_export_strings.xml
41+
crashlytics.properties
42+
crashlytics-build.properties
43+
fabric.properties
44+
### Maven template
45+
target/
46+
pom.xml.tag
47+
pom.xml.releaseBackup
48+
pom.xml.versionsBackup
49+
pom.xml.next
50+
release.properties
51+
dependency-reduced-pom.xml
52+
buildNumber.properties
53+
.mvn/timing.properties
54+

.mvn/jvm.config

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--add-exports jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-exports jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang.reflect=ALL-UNNAMED --add-opens=java.base/java.text=ALL-UNNAMED --add-opens=java.desktop/java.awt.font=ALL-UNNAMED

.mvn/wrapper/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
maven-wrapper.jar

.mvn/wrapper/maven-wrapper.properties

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.1/apache-maven-3.8.1-bin.zip
18+
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.1.1/maven-wrapper-3.1.1.jar

api/pom.xml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.cosium.json_schema_to_java_record</groupId>
7+
<artifactId>json-schema-to-java-record-parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<name>JSON schema to Java Record API</name>
12+
<artifactId>json-schema-to-java-record-api</artifactId>
13+
14+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cosium.json_schema_to_java_record_api;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Target;
5+
6+
/**
7+
* @author Réda Housni Alaoui
8+
*/
9+
@Target({ElementType.PACKAGE})
10+
public @interface GenerateRecordsFromJsonSchemas {
11+
12+
JsonSchemaFileLocation[] schemaRootFileLocations();
13+
14+
JsonSchemaConfiguration[] schemaConfigurations() default {};
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.cosium.json_schema_to_java_record_api;
2+
3+
/**
4+
* @author Réda Housni Alaoui
5+
*/
6+
public @interface JsonSchemaConfiguration {
7+
8+
/**
9+
* @return A value that should match the JSON schema $id attribute.
10+
*/
11+
String schemaId();
12+
13+
/**
14+
* Allows forcing the java type qualified name of the schema. If the type already exists, it will
15+
* be used as is.
16+
*
17+
* <p>E.g. "com.aqme.Foo"
18+
*/
19+
String javaTypeQualifiedName() default "";
20+
21+
/**
22+
* @return The interfaces that the generated record must implement.
23+
*/
24+
String[] javaInterfaceQualifiedNames() default {};
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.cosium.json_schema_to_java_record_api;
2+
3+
/**
4+
* @author Réda Housni Alaoui
5+
*/
6+
public @interface JsonSchemaFileLocation {
7+
/**
8+
* e.g. "com.aqme"
9+
*
10+
* <p>Use empty string "" for the root package
11+
*/
12+
String moduleAndPackage();
13+
14+
/** e.g. "foo.schema.json" */
15+
String relativeName();
16+
}

bom/pom.xml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.cosium.json_schema_to_java_record</groupId>
7+
<artifactId>json-schema-to-java-record-parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<name>JSON schema to Java Record BOM</name>
12+
<artifactId>json-schema-to-java-record-bom</artifactId>
13+
14+
<packaging>pom</packaging>
15+
16+
<dependencyManagement>
17+
<dependencies>
18+
<dependency>
19+
<groupId>${project.groupId}</groupId>
20+
<artifactId>json-schema-to-java-record-api</artifactId>
21+
<version>${project.version}</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>${project.groupId}</groupId>
25+
<artifactId>json-schema-to-java-record</artifactId>
26+
<version>${project.version}</version>
27+
</dependency>
28+
</dependencies>
29+
</dependencyManagement>
30+
31+
</project>

core/pom.xml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<parent>
6+
<groupId>com.cosium.json_schema_to_java_record</groupId>
7+
<artifactId>json-schema-to-java-record-parent</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
11+
<name>JSON schema to Java Record</name>
12+
<artifactId>json-schema-to-java-record</artifactId>
13+
14+
<dependencies>
15+
<dependency>
16+
<groupId>${project.groupId}</groupId>
17+
<artifactId>json-schema-to-java-record-api</artifactId>
18+
<version>${project.version}</version>
19+
</dependency>
20+
21+
<dependency>
22+
<groupId>com.fasterxml.jackson.core</groupId>
23+
<artifactId>jackson-databind</artifactId>
24+
</dependency>
25+
26+
<dependency>
27+
<groupId>com.google.auto.service</groupId>
28+
<artifactId>auto-service</artifactId>
29+
<optional>true</optional>
30+
</dependency>
31+
32+
<dependency>
33+
<groupId>com.palantir.javapoet</groupId>
34+
<artifactId>javapoet</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>io.github.zenwave360</groupId>
39+
<artifactId>json-schema-ref-parser-jvm</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>com.cosium.logging</groupId>
44+
<artifactId>annotation-processor-logger</artifactId>
45+
</dependency>
46+
</dependencies>
47+
48+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.cosium.json_schema_to_java_record;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import io.zenwave360.jsonrefparser.$Ref;
6+
import io.zenwave360.jsonrefparser.resolver.Resolver;
7+
import java.io.InputStream;
8+
import java.net.URI;
9+
import java.nio.charset.StandardCharsets;
10+
11+
/**
12+
* @author Réda Housni Alaoui
13+
*/
14+
class $RefClasspathResolver implements Resolver {
15+
16+
private final ReadonlyClassPathResources classPathResources;
17+
18+
$RefClasspathResolver(ReadonlyClassPathResources classPathResources) {
19+
this.classPathResources = requireNonNull(classPathResources);
20+
}
21+
22+
@Override
23+
public String resolve($Ref $ref) {
24+
try {
25+
URI uri = $ref.getURI();
26+
if (uri.toString().startsWith("classpath:") && !uri.toString().startsWith("classpath:/")) {
27+
// gracefully handle classpath: without the slash
28+
uri = URI.create(uri.toString().replace("classpath:", "classpath:/"));
29+
}
30+
31+
String uriPath = uri.getPath().replaceFirst("^/", "");
32+
int lastIndexOfSlash = uriPath.lastIndexOf("/");
33+
34+
String packageName = uriPath.substring(0, lastIndexOfSlash).replace("/", ".");
35+
String relativeName = uriPath.substring(lastIndexOfSlash + 1);
36+
try (InputStream inputStream =
37+
classPathResources.openInputStream(packageName, relativeName)) {
38+
return new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
39+
}
40+
41+
} catch (MissingResourceException missingResourceException) {
42+
throw missingResourceException;
43+
} catch (Exception e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.cosium.json_schema_to_java_record;
2+
3+
import com.cosium.json_schema_to_java_record_api.GenerateRecordsFromJsonSchemas;
4+
import com.cosium.logging.annotation_processor.AbstractLoggingProcessor;
5+
import com.google.auto.service.AutoService;
6+
import java.util.Set;
7+
import java.util.concurrent.CopyOnWriteArraySet;
8+
import java.util.function.Predicate;
9+
import java.util.stream.Collectors;
10+
import javax.annotation.processing.ProcessingEnvironment;
11+
import javax.annotation.processing.Processor;
12+
import javax.annotation.processing.RoundEnvironment;
13+
import javax.lang.model.SourceVersion;
14+
import javax.lang.model.element.TypeElement;
15+
import org.jspecify.annotations.Nullable;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
/**
20+
* @author Réda Housni Alaoui
21+
*/
22+
@AutoService(Processor.class)
23+
public class AnnotationProcessor extends AbstractLoggingProcessor {
24+
25+
private static final Logger LOGGER = LoggerFactory.getLogger(AnnotationProcessor.class);
26+
27+
private static final Boolean ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS = false;
28+
29+
private final Set<Command> processedCommands = new CopyOnWriteArraySet<>();
30+
31+
@Nullable private JavaTypes javaTypes;
32+
@Nullable private JsonSchemas jsonSchemas;
33+
34+
@Override
35+
public synchronized void init(ProcessingEnvironment processingEnv) {
36+
super.init(processingEnv);
37+
this.javaTypes = new JavaTypes(processingEnv.getFiler());
38+
jsonSchemas = new JsonSchemas(new ReadonlyClassPathResources(processingEnv.getFiler()));
39+
}
40+
41+
@Override
42+
protected boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
43+
if (roundEnv.processingOver() || annotations.isEmpty()) {
44+
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
45+
}
46+
47+
Set<Command> commands =
48+
roundEnv.getElementsAnnotatedWith(GenerateRecordsFromJsonSchemas.class).stream()
49+
.map(
50+
element ->
51+
new Command(
52+
element, element.getAnnotation(GenerateRecordsFromJsonSchemas.class)))
53+
.filter(Predicate.not(processedCommands::contains))
54+
.collect(Collectors.toUnmodifiableSet());
55+
56+
commands.stream()
57+
.peek(command -> LOGGER.debug("Executing {}", command))
58+
.forEach(command -> command.execute(javaTypes, jsonSchemas));
59+
60+
processedCommands.addAll(commands);
61+
62+
return ALLOW_OTHER_PROCESSORS_TO_CLAIM_ANNOTATIONS;
63+
}
64+
65+
@Override
66+
public Set<String> getSupportedAnnotationTypes() {
67+
return Set.of(GenerateRecordsFromJsonSchemas.class.getCanonicalName());
68+
}
69+
70+
@Override
71+
public SourceVersion getSupportedSourceVersion() {
72+
return SourceVersion.latestSupported();
73+
}
74+
}

0 commit comments

Comments
 (0)