Skip to content

Commit

Permalink
Remove usages of deprecated (in Java 9 and above) Class#newInstance (
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Jun 13, 2024
1 parent 16643b7 commit 6b249de
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 8 deletions.
8 changes: 6 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/AnnotationHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.kohsuke.stapler;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import javax.servlet.ServletException;
import org.apache.commons.beanutils.Converter;

Expand Down Expand Up @@ -82,8 +83,11 @@ protected AnnotationHandler computeValue(Class<?> at) {
InjectedParameter ip = at.getAnnotation(InjectedParameter.class);
if (ip != null) {
try {
return ip.value().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return ip.value().getDeclaredConstructor().newInstance();
} catch (NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
throw new RuntimeException("Failed to instantiate parameter injector for " + at, e);
}
} else {
Expand Down
8 changes: 6 additions & 2 deletions core/src/main/java/org/kohsuke/stapler/Facet.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
Expand Down Expand Up @@ -103,8 +104,11 @@ public static <T> List<T> discoverExtensions(Class<T> type, ClassLoader... cls)
continue;
}
try {
r.add(c.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
r.add(c.getDeclaredConstructor().newInstance());
} catch (NoSuchMethodException
| InstantiationException
| IllegalAccessException
| InvocationTargetException e) {
LOGGER.log(Level.WARNING, "Failed to instantiate " + c, e);
}
}
Expand Down
20 changes: 19 additions & 1 deletion core/src/main/java/org/kohsuke/stapler/Function.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package org.kohsuke.stapler;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.lang.annotation.Annotation;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.WrongMethodTypeException;
Expand Down Expand Up @@ -285,7 +286,7 @@ final Function wrapByInterceptors(AnnotatedElement m) {
final InterceptorAnnotation ia = a.annotationType().getAnnotation(InterceptorAnnotation.class);
if (ia != null) {
try {
Interceptor i = ia.value().newInstance();
Interceptor i = ia.value().getDeclaredConstructor().newInstance();
switch (ia.stage()) {
case SELECTION:
f = new SelectionInterceptedFunction(f, i);
Expand All @@ -296,6 +297,10 @@ final Function wrapByInterceptors(AnnotatedElement m) {
default:
throw new IllegalArgumentException("Unknown Stage: " + ia.stage());
}
} catch (NoSuchMethodException e) {
throw (Error)
new NoSuchMethodError("Failed to instantiate interceptor for " + f.getDisplayName())
.initCause(e);
} catch (InstantiationException e) {
throw (Error)
new InstantiationError("Failed to instantiate interceptor for " + f.getDisplayName())
Expand All @@ -304,6 +309,19 @@ final Function wrapByInterceptors(AnnotatedElement m) {
throw (Error)
new IllegalAccessError("Failed to instantiate interceptor for " + f.getDisplayName())
.initCause(e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(e);
}
}
}
}
Expand Down
17 changes: 16 additions & 1 deletion core/src/main/java/org/kohsuke/stapler/RequestImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ public <T> List<T> bindParametersToList(Class<T> type, String prefix) {
// no designated data binding constructor. use reflection
try {
for (int i = 0; i < len; i++) {
T t = type.newInstance();
T t = type.getDeclaredConstructor().newInstance();
r.add(t);

e = getParameterNames();
Expand All @@ -532,10 +532,25 @@ public <T> List<T> bindParametersToList(Class<T> type, String prefix) {
}
}
}
} catch (NoSuchMethodException x) {
throw new NoSuchMethodError(x.getMessage());
} catch (InstantiationException x) {
throw new InstantiationError(x.getMessage());
} catch (IllegalAccessException x) {
throw new IllegalAccessError(x.getMessage());
} catch (InvocationTargetException x) {
Throwable t = x.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(x);
}
}
}

Expand Down
20 changes: 19 additions & 1 deletion core/src/main/java/org/kohsuke/stapler/Stapler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UncheckedIOException;
import java.lang.reflect.InvocationTargetException;
import java.net.JarURLConnection;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -1188,7 +1189,7 @@ public static Converter lookupConverter(Class type) {
return null;
}
Class<?> cl = type.getClassLoader().loadClass(type.getName() + "$StaplerConverterImpl");
c = (Converter) cl.newInstance();
c = (Converter) cl.getDeclaredConstructor().newInstance();
CONVERT_UTILS.register(c, type);
return c;
} catch (ClassNotFoundException e) {
Expand All @@ -1197,10 +1198,27 @@ public static Converter lookupConverter(Class type) {
IllegalAccessError x = new IllegalAccessError();
x.initCause(e);
throw x;
} catch (NoSuchMethodException e) {
NoSuchMethodError x = new NoSuchMethodError();
x.initCause(e);
throw x;
} catch (InstantiationException e) {
InstantiationError x = new InstantiationError();
x.initCause(e);
throw x;
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof IOException) {
throw new UncheckedIOException((IOException) t);
} else if (t instanceof Exception) {
throw new RuntimeException(t);
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new Error(e);
}
}

// bean utils doesn't check the super type, so converters that apply to multiple types
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public final class JellyBuilder extends GroovyObjectSupport {
@Override
protected GroovyClosureScript computeValue(Class<?> type) {
try {
GroovyClosureScript o = (GroovyClosureScript) type.newInstance();
GroovyClosureScript o =
(GroovyClosureScript) type.getDeclaredConstructor().newInstance();
o.setDelegate(JellyBuilder.this);
adjunct(type.getName());
return o;
Expand Down

0 comments on commit 6b249de

Please sign in to comment.