Skip to content

Commit

Permalink
Record processing backport (#531)
Browse files Browse the repository at this point in the history
Record processing backport

Signed-off-by: David Kral <[email protected]>
  • Loading branch information
Verdent authored Jan 13, 2022
1 parent ed5071d commit 6e48dc2
Show file tree
Hide file tree
Showing 16 changed files with 498 additions and 14 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#
# Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0,
# or the Eclipse Distribution License v. 1.0 which is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#

name: Yasson

on: [push, pull_request]

jobs:
build:
name: Test on JDK ${{ matrix.java_version }}
runs-on: ubuntu-latest

strategy:
matrix:
java_version: [ 11, 17 ]

steps:
- name: Checkout for build
uses: actions/[email protected]
with:
fetch-depth: 0
- name: Set up compile JDK
uses: actions/setup-java@v2
with: #Compile java needs to be the highest to ensure proper compilation of the multi-release jar
distribution: 'adopt'
java-version: 17
- name: Maven cache
uses: actions/cache@v2
env:
cache-name: maven-cache
with:
path:
~/.m2
key: build-${{ env.cache-name }}
- name: Copyright
run: bash etc/copyright.sh
- name: Checkstyle
run: mvn -B -Pstaging checkstyle:checkstyle
- name: Yasson install
run: mvn -U -C -Pstaging clean install -DskipTests
- name: Set up JDK for tests
uses: actions/setup-java@v2
with:
distribution: 'adopt'
java-version: ${{ matrix.java_version }}
- name: Yasson tests
run: mvn -U -C -Dmaven.javadoc.skip=true -Pstaging verify
78 changes: 74 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -36,6 +36,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jakarta.json.version>1.1.6</jakarta.json.version>
<jakarta.json.bind.version>1.0.2</jakarta.json.bind.version>
<jakarta.enterprise.cdi-api.version>2.0.2</jakarta.enterprise.cdi-api.version>
<netbeans.hint.jdkPlatform>JDK_9</netbeans.hint.jdkPlatform>
</properties>

Expand All @@ -62,7 +63,7 @@
<dependency>
<groupId>jakarta.enterprise</groupId>
<artifactId>jakarta.enterprise.cdi-api</artifactId>
<version>2.0.2</version>
<version>${jakarta.enterprise.cdi-api.version}</version>
<optional>true</optional>
<scope>provided</scope>
</dependency>
Expand Down Expand Up @@ -222,6 +223,50 @@
</plugins>
</build>
</profile>
<profile>
<id>jdk16</id>
<activation>
<jdk>[16,)</jdk>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<executions>
<execution>
<id>default-testCompile</id>
<configuration>
<release>16</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/test/java</compileSourceRoot>
<compileSourceRoot>${project.basedir}/src/test/java16</compileSourceRoot>
</compileSourceRoots>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
<configuration>
<includes>
<include>**/RecordTest.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<build>
Expand Down Expand Up @@ -253,6 +298,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -271,6 +321,19 @@
</compilerArgs>
</configuration>
</execution>
<execution>
<id>multi-release-compile-16</id>
<goals>
<goal>compile</goal>
</goals>
<configuration>
<release>16</release>
<compileSourceRoots>
<compileSourceRoot>${project.basedir}/src/main/java16</compileSourceRoot>
</compileSourceRoots>
<multiReleaseOutput>true</multiReleaseOutput>
</configuration>
</execution>
<execution>
<id>base-compile</id>
<goals>
Expand Down Expand Up @@ -305,6 +368,9 @@
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
<manifestEntries>
<Multi-Release>true</Multi-Release>
</manifestEntries>
</archive>
</configuration>
</plugin>
Expand Down Expand Up @@ -366,7 +432,7 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<version>3.5.0</version>
<version>5.1.1</version>
<executions>
<execution>
<id>osgi-bundle</id>
Expand All @@ -375,13 +441,14 @@
<goal>manifest</goal>
</goals>
<configuration>
<niceManifest>true</niceManifest>
<instructions>
<_failok>true</_failok>
<Bundle-Name>${project.name}</Bundle-Name>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Version>${project.version}</Bundle-Version>
<Export-Package>org.eclipse.yasson;version=${project.version}</Export-Package>
<Private-Package>org.eclipse.yasson.*;version=${project.version}</Private-Package>
<Multi-Release>true</Multi-Release>
<Import-Package>
javax.enterprise.context.spi;version=!;resolution:="optional",
javax.enterprise.inject.spi;version=!;resolution:="optional",
Expand All @@ -396,6 +463,7 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<executions>
Expand All @@ -410,12 +478,14 @@
<excludes>
<exclude>**/JavaxNamingExcludedTest.class</exclude>
<exclude>**/AnnotationIntrospectorWithoutOptionalModulesTest.class</exclude>
<exclude>**/*Record*</exclude>
</excludes>
<argLine>
<!--Remove when CDI is updated to support modules
(Yasson does read CDI which is on CP, CDI access yasson's classes with reflection)
-->
--add-reads org.eclipse.yasson=ALL-UNNAMED --add-opens org.eclipse.yasson/org.eclipse.yasson.internal.cdi=ALL-UNNAMED
--add-opens=java.base/java.lang=ALL-UNNAMED

--add-exports org.eclipse.yasson/org.eclipse.yasson.internal.cdi=java.naming
</argLine>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2020 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -175,12 +175,15 @@ public JsonbCreator getCreator(Class<?> clazz) {
}
}
if (jsonbCreator == null) {
jsonbCreator = constructorPropertiesIntrospector.getCreator(declaredConstructors);
jsonbCreator = ClassMultiReleaseExtension.findCreator(clazz, declaredConstructors, this);
if (jsonbCreator == null) {
jsonbCreator = constructorPropertiesIntrospector.getCreator(declaredConstructors);
}
}
return jsonbCreator;
}

private JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz) {
JsonbCreator createJsonbCreator(Executable executable, JsonbCreator existing, Class<?> clazz) {
if (existing != null) {
throw new JsonbException(Messages.getMessage(MessageKeys.MULTIPLE_JSONB_CREATORS, clazz));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/

package org.eclipse.yasson.internal;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Optional;

import javax.json.bind.JsonbException;

import org.eclipse.yasson.internal.model.JsonbCreator;
import org.eclipse.yasson.internal.model.Property;

/**
* Search for instance creator from other sources.
* Mainly intended to add extensibility for different java versions and new features.
*/
public class ClassMultiReleaseExtension {

private ClassMultiReleaseExtension() {
throw new IllegalStateException("This class cannot be instantiated");
}

static boolean shouldTransformToPropertyName(Method method) {
return true;
}

static boolean isSpecialAccessorMethod(Method method, Map<String, Property> classProperties) {
return false;
}

static JsonbCreator findCreator(Class<?> clazz,
Constructor<?>[] declaredConstructors,
AnnotationIntrospector introspector) {
return null;
}

public static Optional<JsonbException> exceptionToThrow(Class<?> clazz) {
return Optional.empty();
}

}
10 changes: 7 additions & 3 deletions src/main/java/org/eclipse/yasson/internal/ClassParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -192,10 +192,14 @@ private void parseMethods(Class<?> clazz,
for (Method method : declaredMethods) {
String name = method.getName();
//isBridge method filters out methods inherited from interfaces
if (!isPropertyMethod(method) || method.isBridge() || isSpecialCaseMethod(clazz, method)) {
boolean isAccessorMethod = ClassMultiReleaseExtension.isSpecialAccessorMethod(method, classProperties)
|| isPropertyMethod(method);
if (!isAccessorMethod || method.isBridge() || isSpecialCaseMethod(clazz, method)) {
continue;
}
final String propertyName = toPropertyMethod(name);
final String propertyName = ClassMultiReleaseExtension.shouldTransformToPropertyName(method)
? toPropertyMethod(name)
: name;

registerMethod(propertyName, method, classElement, classProperties);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -60,6 +60,10 @@ public enum MessageKeys {
* Class does not have default constructor.
*/
NO_DEFAULT_CONSTRUCTOR("noDefaultConstructor"),
/**
* Class does not have default constructor.
*/
RECORD_MULTIPLE_CONSTRUCTORS("recordMultipleConstructors"),
/**
* There has been an error while invoking getter.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2021 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand All @@ -23,6 +23,7 @@
import javax.json.bind.serializer.JsonbDeserializer;
import javax.json.stream.JsonParser;

import org.eclipse.yasson.internal.ClassMultiReleaseExtension;
import org.eclipse.yasson.internal.JsonbContext;
import org.eclipse.yasson.internal.JsonbParser;
import org.eclipse.yasson.internal.JsonbRiParser;
Expand Down Expand Up @@ -98,7 +99,8 @@ public T getInstance(Unmarshaller unmarshaller) {
} else {
Constructor<T> defaultConstructor = (Constructor<T>) getClassModel().getDefaultConstructor();
if (defaultConstructor == null) {
throw new JsonbException(Messages.getMessage(MessageKeys.NO_DEFAULT_CONSTRUCTOR, rawType));
throw ClassMultiReleaseExtension.exceptionToThrow(rawType)
.orElse(new JsonbException(Messages.getMessage(MessageKeys.NO_DEFAULT_CONSTRUCTOR, rawType)));
}
instance = ReflectionUtils.createNoArgConstructorInstance(defaultConstructor);
}
Expand Down
Loading

0 comments on commit 6e48dc2

Please sign in to comment.