Skip to content

Commit

Permalink
Work on comliance for Java 17+
Browse files Browse the repository at this point in the history
  • Loading branch information
cuioss committed Aug 14, 2023
1 parent e74d328 commit 390c10c
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 282 deletions.
3 changes: 2 additions & 1 deletion src/main/java/de/cuioss/tools/collect/CollectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ public CollectionBuilder<E> add(E e) {
* <em>Caution:</em> with this call the return value of
* {@link java.util.Collection#add(Object)} will be ignored.
*/
public CollectionBuilder<E> add(@SuppressWarnings("unchecked") E... elements) {
@SafeVarargs
public final CollectionBuilder<E> add(E... elements) {
if (!MoreCollections.isEmpty(elements)) {
for (E element : elements) {
add(element);
Expand Down
109 changes: 8 additions & 101 deletions src/main/java/de/cuioss/tools/lang/SecuritySupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@
import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Optional;
import java.util.Properties;

import de.cuioss.tools.logging.CuiLogger;
import lombok.experimental.UtilityClass;

/**
Expand All @@ -38,56 +33,29 @@
* {@link AccessController#doPrivileged(PrivilegedAction)} for its method in
* case a {@link SecurityManager} is set.
*
* @deprecated SecurityManager hard deprecated by Java 18+
* @author Oliver Wolff
*
*/
@UtilityClass
@SuppressWarnings("java:S1905") // owolff: The casts are necessary for the return type
@Deprecated(forRemoval = true, since = "1.2")
public class SecuritySupport {

private static final String SECURITY_MANAGER_CONFIGURED = "A SecurityManager is configured, using PrivilegedAction";
private static final CuiLogger LOGGER = new CuiLogger(SecuritySupport.class);

/**
* @return the context-classloader if obtainable, {@link Optional#empty()}
* otherwise
*/
public static Optional<ClassLoader> getContextClassLoader() {
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, accessing context-class-loader");
return Optional.ofNullable(Thread.currentThread().getContextClassLoader());
}
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
return AccessController.doPrivileged((PrivilegedAction<Optional<ClassLoader>>) () -> {
try {
return Optional.ofNullable(Thread.currentThread().getContextClassLoader());
} catch (SecurityException e) {
LOGGER.warn("Unable to access context-class-loader due to SecurityException", e);
return Optional.empty();
}
});
return Optional.ofNullable(Thread.currentThread().getContextClassLoader());
}

/**
* @param object to be set accessible
* @param accessible value
*/
public static void setAccessible(AccessibleObject object, boolean accessible) {
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, setting accessible directly");
object.setAccessible(accessible);
} else {
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {

try {
object.setAccessible(accessible);
} catch (SecurityException e) {
LOGGER.warn("Unable to call 'setAccessible' due to SecurityException", e);
}
return null;
});
}
object.setAccessible(accessible);
}

/**
Expand All @@ -99,20 +67,7 @@ public static Optional<String> accessSystemProperty(String propertyName) {
if (isEmpty(propertyName)) {
return Optional.empty();
}
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, accessing System-Property directly");
return Optional.ofNullable(System.getProperty(propertyName));
}
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
return AccessController.doPrivileged((PrivilegedAction<Optional<String>>) () -> {

try {
return Optional.ofNullable(System.getProperty(propertyName));
} catch (SecurityException e) {
LOGGER.warn("Unable to call 'System.getProperty' due to SecurityException", e);
}
return Optional.empty();
});
return Optional.ofNullable(System.getProperty(propertyName));
}

/**
Expand All @@ -121,39 +76,15 @@ public static Optional<String> accessSystemProperty(String propertyName) {
* object.
*/
public static Properties accessSystemProperties() {
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, accessing System.getProperties directly");
return System.getProperties();
}
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
return AccessController.doPrivileged((PrivilegedAction<Properties>) () -> {
try {
return System.getProperties();
} catch (SecurityException e) {
LOGGER.warn("Unable to call 'System.getProperties' due to SecurityException", e);
}
return new Properties();
});
return System.getProperties();
}

/**
* @return the map derived by {@link System#getenv()}. If this can not be
* achieved it returns an empty map.
*/
public static Map<String, String> accessSystemEnv() {
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, accessing System.getenv directly");
return System.getenv();
}
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
return AccessController.doPrivileged((PrivilegedAction<Map<String, String>>) () -> {
try {
return System.getenv();
} catch (SecurityException e) {
LOGGER.warn("Unable to call 'System.getenv' due to SecurityException", e);
}
return Collections.emptyMap();
});
return System.getenv();
}

/**
Expand All @@ -171,30 +102,6 @@ public static Map<String, String> accessSystemEnv() {
// do
public static <T> Constructor<? extends T> getDeclaredConstructor(Class<T> clazz, Class<?>... paramTypes)
throws NoSuchMethodException {
if (null == System.getSecurityManager()) {
LOGGER.trace("No SecurityManager configured, accessing declared constructors directly");
return clazz.getDeclaredConstructor(paramTypes);
}
try {
LOGGER.trace(SECURITY_MANAGER_CONFIGURED);
return AccessController.doPrivileged((PrivilegedExceptionAction<Constructor<? extends T>>) () -> {
Constructor<? extends T> constructor = null;
try {
constructor = clazz.getDeclaredConstructor(paramTypes);

} catch (SecurityException e) {
LOGGER.warn(e,
"Unable to call 'getDeclaredConstructor' due to SecurityException, class='{}', paramTypes='{}'",
clazz.toString(), Arrays.toString(paramTypes));
}
return constructor;
});
} catch (PrivilegedActionException e) {
var e2 = e.getException();
if (e2 instanceof NoSuchMethodException) {
throw (NoSuchMethodException) e2;
}
throw new IllegalStateException(e2);
}
return clazz.getDeclaredConstructor(paramTypes);
}
}
9 changes: 4 additions & 5 deletions src/main/java/de/cuioss/tools/reflect/FieldWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.lang.reflect.Member;
import java.util.Optional;

import de.cuioss.tools.lang.SecuritySupport;
import de.cuioss.tools.logging.CuiLogger;
import de.cuioss.tools.string.MoreStrings;
import lombok.Getter;
Expand Down Expand Up @@ -82,7 +81,7 @@ public Optional<Object> readValue(Object object) {
synchronized (field) {
if (!initialAccessible) {
log.trace("Explicitly setting accessible flag");
SecuritySupport.setAccessible(field, true);
field.setAccessible(true);
}
try {
return Optional.ofNullable(field.get(object));
Expand All @@ -93,7 +92,7 @@ public Optional<Object> readValue(Object object) {
} finally {
if (!initialAccessible) {
log.trace("Resetting accessible flag");
SecuritySupport.setAccessible(field, false);
field.setAccessible(false);
}
}
}
Expand Down Expand Up @@ -137,7 +136,7 @@ public void writeValue(@NonNull Object object, Object value) {
synchronized (field) {
if (!initialAccessible) {
log.trace("Explicitly setting accessible flag");
SecuritySupport.setAccessible(field, true);
field.setAccessible(true);
}
try {
field.set(object, value);
Expand All @@ -149,7 +148,7 @@ public void writeValue(@NonNull Object object, Object value) {
} finally {
if (!initialAccessible) {
log.trace("Resetting accessible flag");
SecuritySupport.setAccessible(field, false);
field.setAccessible(false);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import de.cuioss.tools.reflect.MoreReflection;
import de.cuioss.tools.reflect.support.FieldNameClass;

@Deprecated
class SecuritySupportTest {

static final String TEST_VALUE = "testValue";
Expand Down

This file was deleted.

Loading

0 comments on commit 390c10c

Please sign in to comment.