You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2024/01/02 05:07:26 UTC
(logging-log4j2) branch main updated (838f562f7e -> 2da4108da5)
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
from 838f562f7e Avoid NPE in extension code
new 229218fb12 Revert change in type checks
new 2da4108da5 Add nullability annotation stereotypes
The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
.../log4j/test/junit/TestPropertyResolver.java | 8 ++-----
log4j-api/pom.xml | 7 +++++-
.../org/apache/logging/log4j/lang/NonNull.java | 18 ++++++++++------
.../org/apache/logging/log4j/lang/NullMarked.java | 24 ++++++++++-----------
.../apache/logging/log4j/lang/NullUnmarked.java | 25 +++++++++++-----------
.../org/apache/logging/log4j/lang/Nullable.java | 19 ++++++++++------
.../apache/logging/log4j/lang}/package-info.java | 7 +++---
log4j-parent/pom.xml | 8 +++++++
8 files changed, 66 insertions(+), 50 deletions(-)
copy log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/FactoryType.java => log4j-api/src/main/java/org/apache/logging/log4j/lang/NonNull.java (73%)
copy log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java => log4j-api/src/main/java/org/apache/logging/log4j/lang/NullMarked.java (60%)
copy log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/UsingAnyThreadContext.java => log4j-api/src/main/java/org/apache/logging/log4j/lang/NullUnmarked.java (59%)
copy log4j-plugins/src/main/java/org/apache/logging/log4j/plugins/FactoryType.java => log4j-api/src/main/java/org/apache/logging/log4j/lang/Nullable.java (68%)
copy {log4j-plugins-test/src/main/java/org/apache/logging/log4j/plugins/test/validation => log4j-api/src/main/java/org/apache/logging/log4j/lang}/package-info.java (75%)
(logging-log4j2) 02/02: Add nullability annotation stereotypes
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 2da4108da52653dbb43f00b0967f16917c6975c0
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Mon Jan 1 23:07:05 2024 -0600
Add nullability annotation stereotypes
This adds JSpecify annotations as JSR-305-aliased annotations for ease with tool integration.
Signed-off-by: Matt Sicker <ma...@apache.org>
---
log4j-api/pom.xml | 7 +++-
.../org/apache/logging/log4j/lang/NonNull.java | 35 ++++++++++++++++++++
.../org/apache/logging/log4j/lang/NullMarked.java | 36 +++++++++++++++++++++
.../apache/logging/log4j/lang/NullUnmarked.java | 37 ++++++++++++++++++++++
.../org/apache/logging/log4j/lang/Nullable.java | 36 +++++++++++++++++++++
.../apache/logging/log4j/lang/package-info.java | 28 ++++++++++++++++
log4j-parent/pom.xml | 8 +++++
7 files changed, 186 insertions(+), 1 deletion(-)
diff --git a/log4j-api/pom.xml b/log4j-api/pom.xml
index bb22c40e74..5db39f09bd 100644
--- a/log4j-api/pom.xml
+++ b/log4j-api/pom.xml
@@ -36,14 +36,19 @@
<bnd-module-name>org.apache.logging.log4j</bnd-module-name>
<bnd-extra-package-options>
<!-- No JSR 305 at runtime -->
- !javax.annotation.*
+ !javax.annotation.*,
+ <!-- No JSpecify at runtime -->
+ !org.jspecify.annotations.*
</bnd-extra-package-options>
<bnd-extra-module-options>
<!-- No JSR 305 at runtime -->
jsr305;ignore=true,
+ <!-- No JSpecify at runtime -->
+ org.jspecify;ignore=true,
<!-- Used in StringBuilders through reflection -->
java.sql;static=true
</bnd-extra-module-options>
</properties>
+
</project>
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/lang/NonNull.java b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NonNull.java
new file mode 100644
index 0000000000..8f2e743e7e
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NonNull.java
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.lang;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierNickname;
+
+/**
+ * Annotates an element that should never be null.
+ *
+ * @see org.jspecify.annotations.NonNull
+ */
+@Documented
+@Target(ElementType.TYPE_USE)
+@TypeQualifierNickname
+@Nonnull
+@org.jspecify.annotations.NonNull
+public @interface NonNull {}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullMarked.java b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullMarked.java
new file mode 100644
index 0000000000..4470082671
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullMarked.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.lang;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierDefault;
+
+/**
+ * Annotates an element to indicate all its members should never be null. When applied to a package, this applies
+ * to all classes in the package.
+ *
+ * @see org.jspecify.annotations.NullMarked
+ */
+@Documented
+@Target({ElementType.MODULE, ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
+@TypeQualifierDefault({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE_PARAMETER})
+@Nonnull
+@org.jspecify.annotations.NullMarked
+public @interface NullMarked {}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullUnmarked.java b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullUnmarked.java
new file mode 100644
index 0000000000..e5634ab406
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/lang/NullUnmarked.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.lang;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierDefault;
+import javax.annotation.meta.When;
+
+/**
+ * Annotates an element to indicate all its members may be null. When applied to a package, this applies
+ * to all classes in the package.
+ *
+ * @see org.jspecify.annotations.NullUnmarked
+ */
+@Documented
+@Target({ElementType.PACKAGE, ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR})
+@TypeQualifierDefault({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE_PARAMETER})
+@Nonnull(when = When.MAYBE)
+@org.jspecify.annotations.NullUnmarked
+public @interface NullUnmarked {}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/lang/Nullable.java b/log4j-api/src/main/java/org/apache/logging/log4j/lang/Nullable.java
new file mode 100644
index 0000000000..f9c28667be
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/lang/Nullable.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.lang;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Target;
+import javax.annotation.Nonnull;
+import javax.annotation.meta.TypeQualifierNickname;
+import javax.annotation.meta.When;
+
+/**
+ * Annotates an element that may be null and should be checked for such at runtime.
+ *
+ * @see org.jspecify.annotations.Nullable
+ */
+@Documented
+@Target(ElementType.TYPE_USE)
+@Nonnull(when = When.MAYBE)
+@TypeQualifierNickname
+@org.jspecify.annotations.Nullable
+public @interface Nullable {}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/lang/package-info.java b/log4j-api/src/main/java/org/apache/logging/log4j/lang/package-info.java
new file mode 100644
index 0000000000..292eb40f6a
--- /dev/null
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/lang/package-info.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+/**
+ * Provides essential type checking annotations for use with tools like SpotBugs. To avoid runtime issues with
+ * optionally-available annotations, annotations are defined in this package, and annotation-related reflection
+ * can easily skip over these annotations.
+ */
+@Export
+@Version("1.0.0")
+package org.apache.logging.log4j.lang;
+
+import org.osgi.annotation.bundle.Export;
+import org.osgi.annotation.versioning.Version;
diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml
index 79de6205c5..b80bbdc42f 100644
--- a/log4j-parent/pom.xml
+++ b/log4j-parent/pom.xml
@@ -127,6 +127,7 @@
<jmdns.version>3.5.9</jmdns.version>
<jmh.version>1.37</jmh.version>
<json-unit.version>2.38.0</json-unit.version>
+ <jspecify.version>0.3.0</jspecify.version>
<junit.version>4.13.2</junit.version>
<junit-jupiter.version>5.10.1</junit-jupiter.version>
<junit-pioneer.version>2.2.0</junit-pioneer.version>
@@ -897,6 +898,13 @@
<scope>provided</scope>
</dependency>
+ <dependency>
+ <groupId>org.jspecify</groupId>
+ <artifactId>jspecify</artifactId>
+ <version>${jspecify.version}</version>
+ <scope>provided</scope>
+ </dependency>
+
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.annotation</artifactId>
(logging-log4j2) 01/02: Revert change in type checks
Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
mattsicker pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 229218fb127f95d4b98dde48122a551dc4ac2332
Author: Matt Sicker <ma...@apache.org>
AuthorDate: Mon Jan 1 22:45:21 2024 -0600
Revert change in type checks
Signed-off-by: Matt Sicker <ma...@apache.org>
---
.../org/apache/logging/log4j/test/junit/TestPropertyResolver.java | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
index 4af5001566..7957331d49 100644
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
@@ -39,11 +39,9 @@ public class TestPropertyResolver extends TypeBasedParameterResolver<TestPropert
.forEach(setProperty -> props.setProperty(setProperty.key(), setProperty.value()));
final Class<?> testClass = context.getRequiredTestClass();
final Object testInstance = context.getRequiredTestInstance();
- final Class<? extends TestProperties> testPropertiesType = props.getClass();
ReflectionSupport.findFields(
testClass,
- field -> ModifierSupport.isNotStatic(field)
- && field.getType().isAssignableFrom(testPropertiesType),
+ field -> ModifierSupport.isNotStatic(field) && TestProperties.class.equals(field.getType()),
HierarchyTraversalMode.BOTTOM_UP)
.forEach(field -> ReflectionUtil.setFieldValue(field, testInstance, props));
}
@@ -54,11 +52,9 @@ public class TestPropertyResolver extends TypeBasedParameterResolver<TestPropert
AnnotationSupport.findRepeatableAnnotations(context.getRequiredTestClass(), SetTestProperty.class)
.forEach(setProperty -> props.setProperty(setProperty.key(), setProperty.value()));
final Class<?> testClass = context.getRequiredTestClass();
- final Class<? extends TestProperties> testPropertiesType = props.getClass();
ReflectionSupport.findFields(
testClass,
- field -> ModifierSupport.isStatic(field)
- && field.getType().isAssignableFrom(testPropertiesType),
+ field -> ModifierSupport.isStatic(field) && TestProperties.class.equals(field.getType()),
HierarchyTraversalMode.BOTTOM_UP)
.forEach(field -> ReflectionUtil.setStaticFieldValue(field, props));
}