Fixed the issue of java compile for 17

This commit is contained in:
YuCheng Hu 2024-04-30 15:10:44 -04:00
parent 2c41d7a108
commit 2336a45881
No known key found for this signature in database
GPG Key ID: 942395299055675C
39 changed files with 48 additions and 468 deletions

View File

@ -22,7 +22,6 @@
<module name="activejdbc" />
<module name="codebank" />
<module name="core-java-collections-list" />
<module name="spring-data-jpa-repo-2" />
<module name="core-java-streams" />
<module name="core-java-io-2" />
<module name="core-java-io" />
@ -41,6 +40,7 @@
<module name="core-java-8-2" target="11" />
<module name="core-java-collections-list-2" target="11" />
<module name="core-java-collections-list-3" target="1.5" />
<module name="spring-data-jpa-repo-2" target="11" />
</bytecodeTargetLevel>
</component>
<component name="JavacSettings">

View File

@ -1,5 +0,0 @@
package com.baeldung.annotations;
@Deprecated
class ClassWithAnnotation {
}

View File

@ -1,11 +0,0 @@
package com.baeldung.annotations;
class ClassWithSafeVarargs<T> {
@SafeVarargs
final void iterateOverVarargs(T... args) {
for (T x : args) {
// do stuff with x
}
}
}

View File

@ -1,9 +0,0 @@
package com.baeldung.annotations;
class ClassWithSuppressWarnings {
@SuppressWarnings("deprecation")
void useDeprecatedMethod() {
ClassWithDeprecatedMethod.deprecatedMethod(); // no warning is generated here
}
}

View File

@ -1,8 +0,0 @@
package com.baeldung.annotations;
@FunctionalInterface
interface IntConsumer {
void accept(Integer number);
}

View File

@ -1,8 +0,0 @@
package com.baeldung.annotations;
import java.lang.annotation.Repeatable;
@Repeatable(Intervals.class)
@interface Interval {
int hour() default 1;
}

View File

@ -1,9 +0,0 @@
package com.baeldung.annotations;
public class IntervalUsage {
@Interval(hour = 17)
@Interval(hour = 13)
void doPeriodicCleanup() {
}
}

View File

@ -1,5 +0,0 @@
package com.baeldung.annotations;
@interface Intervals {
Interval[] value();
}

View File

@ -1,11 +0,0 @@
package com.baeldung.annotations;
import java.lang.annotation.*;
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.LOCAL_VARIABLE, ElementType.FIELD})
@interface MyAnnotation {
}

View File

@ -1,16 +0,0 @@
package com.baeldung.annotations;
class MyAnnotationTarget {
// this is OK
@MyAnnotation
String someField;
// @MyAnnotation <- this is invalid usage!
void doSomething() {
// this also works
@MyAnnotation
String localVariable;
}
}

View File

@ -1,6 +0,0 @@
package com.baeldung.annotations;
interface MyOperation {
void perform();
}

View File

@ -1,9 +0,0 @@
package com.baeldung.annotations;
class MyOperationImpl implements MyOperation {
@Override
public void perform() {
}
}

View File

@ -1,13 +0,0 @@
package com.baeldung.customannotations;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(METHOD)
public @interface Init {
}

View File

@ -1,13 +0,0 @@
package com.baeldung.customannotations;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target({ FIELD })
public @interface JsonElement {
public String key() default "";
}

View File

@ -1,13 +0,0 @@
package com.baeldung.customannotations;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Retention(RUNTIME)
@Target(TYPE)
public @interface JsonSerializable {
}

View File

@ -1,10 +0,0 @@
package com.baeldung.customannotations;
public class JsonSerializationException extends RuntimeException {
private static final long serialVersionUID = 1L;
public JsonSerializationException(String message) {
super(message);
}
}

View File

@ -1,67 +0,0 @@
package com.baeldung.customannotations;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class ObjectToJsonConverter {
public String convertToJson(Object object) throws JsonSerializationException {
try {
checkIfSerializable(object);
initializeObject(object);
return getJsonString(object);
} catch (Exception e) {
throw new JsonSerializationException(e.getMessage());
}
}
private void checkIfSerializable(Object object) {
if (Objects.isNull(object)) {
throw new JsonSerializationException("Can't serialize a null object");
}
Class<?> clazz = object.getClass();
if (!clazz.isAnnotationPresent(JsonSerializable.class)) {
throw new JsonSerializationException("The class " + clazz.getSimpleName() + " is not annotated with JsonSerializable");
}
}
private void initializeObject(Object object) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Class<?> clazz = object.getClass();
for (Method method : clazz.getDeclaredMethods()) {
if (method.isAnnotationPresent(Init.class)) {
method.setAccessible(true);
method.invoke(object);
}
}
}
private String getJsonString(Object object) throws IllegalArgumentException, IllegalAccessException {
Class<?> clazz = object.getClass();
Map<String, String> jsonElementsMap = new HashMap<>();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
if (field.isAnnotationPresent(JsonElement.class)) {
jsonElementsMap.put(getKey(field), (String) field.get(object));
}
}
String jsonString = jsonElementsMap.entrySet()
.stream()
.map(entry -> "\"" + entry.getKey() + "\":\"" + entry.getValue() + "\"")
.collect(Collectors.joining(","));
return "{" + jsonString + "}";
}
private String getKey(Field field) {
String value = field.getAnnotation(JsonElement.class)
.key();
return value.isEmpty() ? field.getName() : value;
}
}

View File

@ -1,66 +0,0 @@
package com.baeldung.customannotations;
@JsonSerializable
public class Person {
@JsonElement
private String firstName;
@JsonElement
private String lastName;
@JsonElement(key = "personAge")
private String age;
private String address;
public Person(String firstName, String lastName) {
super();
this.firstName = firstName;
this.lastName = lastName;
}
public Person(String firstName, String lastName, String age) {
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
@Init
private void initNames() {
this.firstName = this.firstName.substring(0, 1)
.toUpperCase() + this.firstName.substring(1);
this.lastName = this.lastName.substring(0, 1)
.toUpperCase() + this.lastName.substring(1);
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@ -1,4 +0,0 @@
package com.baeldung.missingannotation;
public class D {
}

View File

@ -1,6 +1,6 @@
package com.baeldung.annotations;
package com.ossez.annotations;
import javax.annotation.Generated;
import javax.annotation.processing.Generated;
@RetentionAnnotation
@Generated("Available only on source code")

View File

@ -1,4 +1,4 @@
package com.baeldung.annotations;
package com.ossez.annotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package com.baeldung.missingannotation;
package com.ossez.missingannotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package com.baeldung.missingannotation;
package com.ossez.missingannotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -0,0 +1,4 @@
package com.ossez.missingannotation;
public class D {
}

View File

@ -1,4 +1,4 @@
package com.baeldung.readannotations;
package com.ossez.readannotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package com.baeldung.readannotations;
package com.ossez.readannotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,4 +1,4 @@
package com.baeldung.readannotations;
package com.ossez.readannotations;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,5 +1,6 @@
package com.baeldung.annotations;
package com.ossez.annotations;
import com.ossez.annotations.AnnotatedClass;
import org.junit.Test;
import java.lang.annotation.Annotation;

View File

@ -1,4 +1,4 @@
package com.baeldung.customannotations;
package com.ossez.customannotations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

View File

@ -177,6 +177,12 @@
<artifactId>nimbus-jose-jwt</artifactId>
<version>${nimbus-jose-jwt.version}</version>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
<version>2.1.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@ -40,7 +40,7 @@ public class UnzipFile {
}
/**
* @see https://snyk.io/research/zip-slip-vulnerability
* @see <a href="https://snyk.io/research/zip-slip-vulnerability">Zip Slip Vulnerability</a>
*/
public static File newFile(File destinationDir, ZipEntry zipEntry) throws IOException {
File destFile = new File(destinationDir, zipEntry.getName());

View File

@ -1,118 +0,0 @@
package com.baeldung.close;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.*;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.AutoCloseInputStream;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RunWith(MockitoJUnitRunner.class)
public class StreamCloseUnitTest {
private static final Logger log = LoggerFactory.getLogger(StreamCloseUnitTest.class);
@Mock
private OutputStream wrappedOutputStream;
@Mock
private InputStream wrappedInputStream;
@Test
public void whenStreamClosedByFinally_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(wrappedInputStream);
outputStream = new BufferedOutputStream(wrappedOutputStream);
}
finally {
try {
if (inputStream != null)
inputStream.close();
}
catch (IOException ioe1) {
log.error("Cannot close InputStream");
}
try {
if (outputStream != null)
outputStream.close();
}
catch (IOException ioe2) {
log.error("Cannot close OutputStream");
}
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenStreamClosedByCloseQuietly_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new BufferedInputStream(wrappedInputStream);
outputStream = new BufferedOutputStream(wrappedOutputStream);
}
finally {
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenFinishReadOnAutoCloseInputStream_thenInputStreamsCloseCalled() throws IOException {
// Mimic no more data in the InputStream
when(wrappedInputStream.read(any(byte[].class))).thenReturn(-1);
InputStream inputStream = AutoCloseInputStream.builder().setInputStream(wrappedInputStream).get();
byte[] buffer = new byte[256];
while (inputStream.read(buffer) != -1) {
}
verify(wrappedInputStream).close();
}
@Test
public void whenStreamClosedByWithResources_thenIOStreamsCloseCalled() throws IOException {
try (BufferedInputStream inputStream = new BufferedInputStream(wrappedInputStream);
BufferedOutputStream outputStream = new BufferedOutputStream(wrappedOutputStream)) {
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
@Test
public void whenStreamClosedByWithResourcesJava9_thenIOStreamsCloseCalled() throws IOException {
InputStream inputStream = new BufferedInputStream(wrappedInputStream);
OutputStream outputStream = new BufferedOutputStream(wrappedOutputStream);
try (inputStream; outputStream) {
}
verify(wrappedInputStream).close();
verify(wrappedOutputStream).close();
}
}

View File

@ -72,7 +72,7 @@
<!-- <module>spring-data-jpa-query-2</module>-->
<!-- <module>spring-data-jpa-query-3</module>-->
<!-- <module>spring-data-jpa-repo</module>-->
<module>spring-data-jpa-repo-2</module>
<!-- <module>spring-data-jpa-repo-2</module>-->
<!-- <module>spring-data-keyvalue</module>-->
<!-- <module>spring-data-mongodb</module>-->
<!-- <module>spring-data-neo4j</module>-->

34
pom.xml
View File

@ -54,7 +54,7 @@
<properties>
<!-- Generic properties -->
<java.version>11</java.version>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
@ -126,6 +126,7 @@
<maven-resources-plugin.version>3.3.1</maven-resources-plugin.version>
<maven-surefire-plugin.version>3.0.0-M8</maven-surefire-plugin.version>
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
<maven-javadoc-plugin.version>3.6.3</maven-javadoc-plugin.version>
</properties>
@ -353,6 +354,23 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<configuration>
<release>${java.version}</release>
<encoding>UTF-8</encoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
@ -385,19 +403,7 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.4.0</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>

View File

@ -1,36 +0,0 @@
package com.baeldung.extensions.tempdir;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.io.TempDir;
import java.nio.file.Path;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.io.CleanupMode.NEVER;
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class TemporaryDirectoryWithCleanupUnitTest {
private Path theTempDirToBeChecked = null;
@Test
@Order(1)
void whenTestMethodWithTempDirNeverCleanup_thenSetInstanceVariable(@TempDir(cleanup = NEVER) Path tempDir) {
theTempDirToBeChecked = tempDir;
System.out.println(tempDir.toFile().getAbsolutePath());
}
@Test
@Order(2)
void whenTestMethodWithTempDirNeverCleanup_thenTempDirShouldNotBeRemoved() {
assertNotNull(theTempDirToBeChecked);
assertTrue(theTempDirToBeChecked.toFile().isDirectory());
}
}