Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove newInstance() deprecation warnings #5152

Merged
merged 1 commit into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
* Break out the things that are associated with the Application, but need to be present even when the user has replaced
* the Application instance.
* </p>
*
*
* <p>
* For example: the user replaces ApplicationFactory, and wants to intercept calls to createValueExpression() and
* createMethodExpression() for certain kinds of expressions, but allow the existing application to handle the rest.
Expand Down Expand Up @@ -139,7 +139,7 @@ public class ApplicationAssociate {

private static final String ASSOCIATE_KEY = RIConstants.FACES_PREFIX + "ApplicationAssociate";

private static ThreadLocal<ApplicationAssociate> instance = new ThreadLocal<ApplicationAssociate>() {
private static ThreadLocal<ApplicationAssociate> instance = new ThreadLocal<>() {
@Override
protected ApplicationAssociate initialValue() {
return null;
Expand Down Expand Up @@ -569,9 +569,9 @@ public ResourceBundle getResourceBundle(FacesContext context, String var) {
/**
* keys: element from faces-config
* <p>
*
*
* values: ResourceBundleBean instances.
*
*
* @param var the variable name
* @param bundle the application resource bundle
*/
Expand Down Expand Up @@ -668,12 +668,13 @@ protected void loadDecorators(Map<String, Object> appMap, Compiler newCompiler)
if (decoratorsParamValue != null) {
for (String decorator : split(appMap, decoratorsParamValue.trim(), ";")) {
try {
newCompiler.addTagDecorator((TagDecorator) forName(decorator).newInstance());
newCompiler
.addTagDecorator((TagDecorator) forName(decorator).getDeclaredConstructor().newInstance());

if (LOGGER.isLoggable(FINE)) {
LOGGER.log(FINE, "Successfully Loaded Decorator: {0}", decorator);
}
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
} catch (ReflectiveOperationException | IllegalArgumentException | SecurityException e) {
if (LOGGER.isLoggable(SEVERE)) {
LOGGER.log(SEVERE, "Error Loading Decorator: " + decorator, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ public ClassTemplateInfo() {
public ClassTemplateInfo(Class<? extends ConverterPropertyEditorBase> templateClass) {
this.templateClass = templateClass;
try {
ConverterPropertyEditorBase tc = templateClass.newInstance();
ConverterPropertyEditorBase tc = templateClass.getDeclaredConstructor().newInstance();
Class<?> templateTargetClass = tc.getTargetClass();
loadTemplateBytes();
classNameConstant = findConstant(getVMClassName(templateClass));
classNameRefConstant = findConstant(new StringBuilder(64).append('L').append(getVMClassName(templateClass)).append(';').toString());
targetClassConstant = findConstant(getVMClassName(templateTargetClass));
} catch (InstantiationException | IllegalAccessException | IOException e) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException | IOException e) {
if (LOGGER.isLoggable(Level.FINE)) {
LOGGER.log(Level.FINE, "Unexected exception ClassTemplateInfo", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ public void push(FacesContext ctx) {
throw new IllegalStateException("Error processing annotated Renderer " + ra.toString() + " on class " + rClass.getName()
+ ". Unable to find specified RenderKit.");
}
rk.addRenderer(ra.componentFamily(), ra.rendererType(), (Renderer) rClass.newInstance());
} catch (IllegalStateException | InstantiationException | IllegalAccessException e) {
rk.addRenderer(ra.componentFamily(), ra.rendererType(),
(Renderer) rClass.getDeclaredConstructor().newInstance());
} catch (IllegalStateException | ReflectiveOperationException | SecurityException e) {
throw new FacesException(e);
}
} else if (entry.getValue() instanceof FacesBehaviorRenderer) {
Expand All @@ -106,8 +107,9 @@ public void push(FacesContext ctx) {
throw new IllegalStateException("Error processing annotated ClientBehaviorRenderer " + bra.toString() + " on class "
+ rClass.getName() + ". Unable to find specified RenderKit.");
}
rk.addClientBehaviorRenderer(bra.rendererType(), (ClientBehaviorRenderer) rClass.newInstance());
} catch (IllegalStateException | InstantiationException | IllegalAccessException e) {
rk.addClientBehaviorRenderer(bra.rendererType(),
(ClientBehaviorRenderer) rClass.getDeclaredConstructor().newInstance());
} catch (IllegalStateException | ReflectiveOperationException | SecurityException e) {
throw new FacesException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,13 @@ public UIComponent createComponent(FacesContext context, Resource componentResou
if (!associate.isDevModeEnabled()) {
componentMap.put(className, clazz);
}
result = (UIComponent) clazz.newInstance();
result = (UIComponent) clazz.getDeclaredConstructor().newInstance();
}
} catch (ClassNotFoundException ex) {
if (!associate.isDevModeEnabled()) {
componentMap.put(className, ComponentResourceClassNotFound.class);
}
} catch (InstantiationException | IllegalAccessException | ClassCastException ie) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ie) {
throw new FacesException(ie);
}
}
Expand Down Expand Up @@ -623,8 +623,8 @@ private UIComponent createComponentFromScriptResource(FacesContext context, Reso
if (!associate.isDevModeEnabled()) {
componentMap.put(className, componentClass);
}
result = (UIComponent) componentClass.newInstance();
} catch (IllegalAccessException | InstantiationException | ClassNotFoundException ex) {
result = (UIComponent) componentClass.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ex) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, null, ex);
}
Expand Down Expand Up @@ -777,7 +777,7 @@ private <T> T newThing(String key, ViewMemberInstanceFactoryMetadataMap<String,
}

try {
result = clazz.newInstance();
result = clazz.getDeclaredConstructor().newInstance();
} catch (Throwable t) {
Throwable previousT;
do {
Expand Down Expand Up @@ -1018,8 +1018,8 @@ protected Object newConverter(Class<?> key, Map<Class<?>, Object> map, Class<?>
}
} else {
try {
result = clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
result = clazz.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
cause = e;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,11 @@ private UIComponent newInstance(TreeNode treeNode) throws FacesException {
}
}

UIComponent component = (UIComponent) clazz.newInstance();
UIComponent component = (UIComponent) clazz.getDeclaredConstructor().newInstance();
component.setId(treeNode.id);

return component;
} catch (ClassNotFoundException | NullPointerException | InstantiationException | IllegalAccessException e) {
} catch (NullPointerException | IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
throw new FacesException(e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -212,7 +211,7 @@ protected Object createInstance(ServletContext sc, FacesContext facesContext, St
}

if (clazz != null && returnObject == null) {
returnObject = clazz.newInstance();
returnObject = clazz.getDeclaredConstructor().newInstance();
}

ApplicationInstanceFactoryMetadataMap<String, Object> classMetadataMap = getClassMetadataMap(sc);
Expand Down Expand Up @@ -246,7 +245,7 @@ protected Object createInstance(ServletContext sc, FacesContext facesContext, St
buildMessage(format("Class ''{0}'' is missing a runtime dependency: {1}", className, ncdfe.toString()), source), ncdfe);
} catch (ClassCastException cce) {
throw new ConfigurationException(buildMessage(format("Class ''{0}'' is not an instance of ''{1}''", className, rootType), source), cce);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | FacesException e) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException | FacesException e) {
throw new ConfigurationException(buildMessage(format("Unable to create a new instance of ''{0}'': {1}", className, e.toString()), source), e);
}
}
Expand Down
7 changes: 3 additions & 4 deletions impl/src/main/java/com/sun/faces/el/ELUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.sun.faces.util.ReflectionUtils.newInstance;
import static com.sun.faces.util.Util.getCdiBeanManager;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.LinkedList;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class ELUtils {
/**
* Private cache for storing evaluation results for composite components checks.
*/
private static final HashMap<String, Boolean> compositeComponentEvaluationCache = new HashMap<String, Boolean>();
private static final HashMap<String, Boolean> compositeComponentEvaluationCache = new HashMap<>();

/**
* The maximum size of the <code>compositeComponentEvaluationCache</code>.
Expand All @@ -64,7 +63,7 @@ public class ELUtils {
/**
* FIFO queue, holding access information about the <code>compositeComponentEvaluationCache</code>.
*/
private static final LinkedList<String> evaluationCacheFifoQueue = new LinkedList<String>();
private static final LinkedList<String> evaluationCacheFifoQueue = new LinkedList<>();

/**
* Class member, indicating a <I>positive</I> evaluation result.
Expand Down Expand Up @@ -208,7 +207,7 @@ private static void addEL3_0_Resolvers(FacesCompositeELResolver composite, Appli
// jakarta.el.staticFieldELResolver
composite.addRootELResolver((ELResolver) newInstance("jakarta.el.StaticFieldELResolver"));
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException t) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException t) {
// This is normal on containers that do not have these ELResolvers
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ private final Object featureInstance(String name) {
String type = features.get(name);
if (type != null) {
try {
return ReflectionUtil.forName(type).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException t) {
return ReflectionUtil.forName(type).getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
throw new FaceletException("Could not instantiate feature[" + name + "]: " + type);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public UIComponent createComponent(FaceletContext ctx) {
UIComponent result = null;
try {
Class<?> clazz = Util.loadClass("com.sun.faces.component.PassthroughElement", this);
result = (UIComponent) clazz.newInstance();
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException cnfe) {
result = (UIComponent) clazz.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException cnfe) {
throw new FacesException(cnfe);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private PhaseListener getInstance() {
}
if (instance == null && type != null) {
try {
instance = (PhaseListener) ReflectionUtil.forName(type).newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
instance = (PhaseListener) ReflectionUtil.forName(type).getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
throw new AbortProcessingException("Couldn't Lazily instantiate PhaseListener", e);
}
if (binding != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public static Object decorateInstance(Class clazz, Class rootType, Object root)
}
}
if (clazz != null && returnObject == null) {
returnObject = clazz.newInstance();
returnObject = clazz.getDeclaredConstructor().newInstance();
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
throw new ConfigurationException(
buildMessage(MessageFormat.format("Unable to create a new instance of ''{0}'': {1}", clazz.getName(), e.toString())), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,8 +699,8 @@ protected Collection<Object> createCollection(Collection<Object> collection, Cla

if (!lookupClass.isInterface() && !isAbstract(lookupClass.getModifiers())) {
try {
return (Collection<Object>) lookupClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return (Collection<Object>) lookupClass.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
if (logger.isLoggable(SEVERE)) {
logger.log(SEVERE, "Unable to create new Collection instance for type " + lookupClass.getName(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ private static InjectionProvider getProviderInstance(String className, ExternalC
Constructor ctor = clazz.getConstructor(ServletContext.class);
return (InjectionProvider) ctor.newInstance((ServletContext) extContext.getContext());
} catch (NoSuchMethodException nsme) {
return (InjectionProvider) clazz.newInstance();
return (InjectionProvider) clazz.getDeclaredConstructor().newInstance();
} catch (InvocationTargetException ite) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.injection.provider_cannot_instantiate", new Object[] { className });
Expand All @@ -130,7 +130,7 @@ private static InjectionProvider getProviderInstance(String className, ExternalC
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.injection.provider_not_found", new Object[] { className });
}
} catch (InstantiationException | IllegalAccessException ie) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ie) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.injection.provider_cannot_instantiate", new Object[] { className });
LOGGER.log(Level.SEVERE, "", ie);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private static SerializationProvider getProviderInstance(String className) {
try {
Class<?> clazz = Util.loadClass(className, SerializationProviderFactory.class);
if (implementsSerializationProvider(clazz)) {
provider = (SerializationProvider) clazz.newInstance();
provider = (SerializationProvider) clazz.getDeclaredConstructor().newInstance();
} else {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.serialization.provider_not_implemented", new Object[] { className });
Expand All @@ -94,7 +94,7 @@ private static SerializationProvider getProviderInstance(String className) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.serialization.provider_not_found", new Object[] { className });
}
} catch (InstantiationException | IllegalAccessException ie) {
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException ie) {
if (LOGGER.isLoggable(Level.SEVERE)) {
LOGGER.log(Level.SEVERE, "faces.spi.serialization.provider_cannot_instantiate", new Object[] { className });
LOGGER.log(Level.SEVERE, "", ie);
Expand Down
9 changes: 5 additions & 4 deletions impl/src/main/java/com/sun/faces/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,8 @@ public static <T> T instance(String className) {
*/
public static <T> T instance(Class<T> clazz) {
try {
return clazz.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
return clazz.getDeclaredConstructor().newInstance();
} catch (IllegalArgumentException | ReflectiveOperationException | SecurityException e) {
throw new IllegalStateException(e);
}
}
Expand Down Expand Up @@ -347,14 +347,15 @@ public static Method lookupMethod(Class<?> clazz, String methodName, Class<?>...
* @throws InstantiationException if the class cannot be instantiated
* @throws IllegalAccessException if there is a security violation
*/
public static Object newInstance(String className) throws InstantiationException, IllegalAccessException {
public static Object newInstance(String className)
throws IllegalArgumentException, ReflectiveOperationException, SecurityException {

ClassLoader loader = Util.getCurrentLoader(null);
if (loader == null) {
return null;
}

return getMetaData(loader, className).lookupClass().newInstance();
return getMetaData(loader, className).lookupClass().getDeclaredConstructor().newInstance();
}

/**
Expand Down
Loading