Skip to content

Commit 165d898

Browse files
committed
JCR-5154 Remove deprecated Class.newInstance()
1 parent efff5f6 commit 165d898

File tree

16 files changed

+38
-48
lines changed

16 files changed

+38
-48
lines changed

jackrabbit-core/src/main/java/org/apache/jackrabbit/core/SearchManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public NodeState next() {
431431
protected AbstractQueryImpl createQueryInstance() throws RepositoryException {
432432
try {
433433
String queryImplClassName = handler.getQueryClass();
434-
Object obj = Class.forName(queryImplClassName).newInstance();
434+
Object obj = Class.forName(queryImplClassName).getDeclaredConstructor().newInstance();
435435
if (obj instanceof AbstractQueryImpl) {
436436
return (AbstractQueryImpl) obj;
437437
} else {

jackrabbit-core/src/main/java/org/apache/jackrabbit/core/config/SimpleBeanFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.apache.jackrabbit.core.config;
1818

1919

20+
import java.lang.reflect.InvocationTargetException;
21+
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
2224

@@ -39,12 +41,12 @@ public Object newInstance(Class<?> klass, BeanConfig config) throws Configuratio
3941
}
4042

4143
// Instantiate the object using the default constructor
42-
return objectClass.newInstance();
44+
return objectClass.getDeclaredConstructor().newInstance();
4345
} catch (ClassNotFoundException e) {
4446
throw new ConfigurationException(
4547
"Configured bean implementation class " + cname
4648
+ " was not found.", e);
47-
} catch (InstantiationException e) {
49+
} catch (InstantiationException|IllegalArgumentException|InvocationTargetException|NoSuchMethodException|SecurityException e) {
4850
throw new ConfigurationException(
4951
"Configured bean implementation class " + cname
5052
+ " can not be instantiated.", e);

jackrabbit-core/src/main/java/org/apache/jackrabbit/core/query/lucene/SearchIndex.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ public ExcerptProvider createExcerptProvider(Query query)
933933
throws IOException {
934934
ExcerptProvider ep;
935935
try {
936-
ep = (ExcerptProvider) excerptProviderClass.newInstance();
936+
ep = (ExcerptProvider) excerptProviderClass.getDeclaredConstructor().newInstance();
937937
} catch (Exception e) {
938938
throw Util.createIOException(e);
939939
}
@@ -1307,7 +1307,7 @@ protected SynonymProvider createSynonymProvider() {
13071307
SynonymProvider sp = null;
13081308
if (synonymProviderClass != null) {
13091309
try {
1310-
sp = (SynonymProvider) synonymProviderClass.newInstance();
1310+
sp = (SynonymProvider) synonymProviderClass.getDeclaredConstructor().newInstance();
13111311
sp.initialize(createSynonymProviderConfigResource());
13121312
} catch (Exception e) {
13131313
log.warn("Exception initializing synonym provider: "
@@ -1331,7 +1331,7 @@ protected DirectoryManager createDirectoryManager()
13311331
throw new IOException(directoryManagerClass +
13321332
" is not a DirectoryManager implementation");
13331333
}
1334-
DirectoryManager df = (DirectoryManager) clazz.newInstance();
1334+
DirectoryManager df = (DirectoryManager) clazz.getDeclaredConstructor().newInstance();
13351335
df.init(this);
13361336
return df;
13371337
} catch (IOException e) {
@@ -1356,7 +1356,7 @@ protected RedoLogFactory createRedoLogFactory() throws IOException {
13561356
throw new IOException(redoLogFactoryClass +
13571357
" is not a RedoLogFactory implementation");
13581358
}
1359-
return (RedoLogFactory) clazz.newInstance();
1359+
return (RedoLogFactory) clazz.getDeclaredConstructor().newInstance();
13601360
} catch (Exception e) {
13611361
IOException ex = new IOException();
13621362
ex.initCause(e);
@@ -1420,7 +1420,7 @@ protected SpellChecker createSpellChecker() {
14201420
SpellChecker spCheck = null;
14211421
if (spellCheckerClass != null) {
14221422
try {
1423-
spCheck = (SpellChecker) spellCheckerClass.newInstance();
1423+
spCheck = (SpellChecker) spellCheckerClass.getDeclaredConstructor().newInstance();
14241424
spCheck.init(this);
14251425
} catch (Exception e) {
14261426
log.warn("Exception initializing spell checker: "
@@ -2422,7 +2422,7 @@ public String getSynonymProviderConfigPath() {
24222422
public void setSimilarityClass(String className) {
24232423
try {
24242424
Class<?> similarityClass = Class.forName(className);
2425-
similarity = (Similarity) similarityClass.newInstance();
2425+
similarity = (Similarity) similarityClass.getDeclaredConstructor().newInstance();
24262426
} catch (Exception e) {
24272427
log.warn("Invalid Similarity class: " + className, e);
24282428
}

jackrabbit-core/src/main/java/org/apache/jackrabbit/core/security/principal/ProviderRegistryImpl.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.jackrabbit.core.security.principal;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.util.Collection;
2021
import java.util.LinkedHashMap;
2122
import java.util.Map;
@@ -128,17 +129,11 @@ private PrincipalProvider createProvider(Properties config)
128129
}
129130

130131
try {
131-
Class pc = Class.forName(className, true, BeanConfig.getDefaultClassLoader());
132-
PrincipalProvider pp = (PrincipalProvider) pc.newInstance();
132+
Class<?> pc = Class.forName(className, true, BeanConfig.getDefaultClassLoader());
133+
PrincipalProvider pp = (PrincipalProvider) pc.getDeclaredConstructor().newInstance();
133134
pp.init(config);
134135
return pp;
135-
} catch (ClassNotFoundException e) {
136-
throw new RepositoryException("Unable to create new principal provider.", e);
137-
} catch (IllegalAccessException e) {
138-
throw new RepositoryException("Unable to create new principal provider.", e);
139-
} catch (InstantiationException e) {
140-
throw new RepositoryException("Unable to create new principal provider.", e);
141-
} catch (ClassCastException e) {
136+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
142137
throw new RepositoryException("Unable to create new principal provider.", e);
143138
}
144139
}

jackrabbit-core/src/test/java/org/apache/jackrabbit/core/query/lucene/directory/DirectoryManagerTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
public class DirectoryManagerTest extends TestCase {
3434

35-
private static final Collection IMPLEMENTATIONS = Arrays.asList(
35+
private static final Collection<Class<?>> IMPLEMENTATIONS = Arrays.asList(
3636
new Class[]{FSDirectoryManager.class, RAMDirectoryManager.class});
3737

3838
private static final SearchIndex INDEX = new SearchIndex();
@@ -91,9 +91,9 @@ public void call(DirectoryManager directoryManager) throws Exception {
9191
}
9292

9393
private void execute(Callable callable) throws Exception {
94-
for (Iterator it = IMPLEMENTATIONS.iterator(); it.hasNext(); ) {
95-
Class clazz = (Class) it.next();
96-
DirectoryManager dirMgr = (DirectoryManager) clazz.newInstance();
94+
for (Iterator<Class<?>> it = IMPLEMENTATIONS.iterator(); it.hasNext(); ) {
95+
Class<?> clazz = it.next();
96+
DirectoryManager dirMgr = (DirectoryManager) clazz.getDeclaredConstructor().newInstance();
9797
dirMgr.init(INDEX);
9898
try {
9999
callable.call(dirMgr);

jackrabbit-data/src/main/java/org/apache/jackrabbit/core/util/db/ConnectionFactory.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.jackrabbit.core.util.db;
1818

19+
import java.lang.reflect.InvocationTargetException;
1920
import java.sql.Connection;
2021
import java.sql.Driver;
2122
import java.sql.SQLException;
@@ -286,18 +287,15 @@ private DataSource getJndiDataSource(
286287
Class<Context> contextClass, String name)
287288
throws RepositoryException {
288289
try {
289-
Object object = contextClass.newInstance().lookup(name);
290+
Object object = contextClass.getDeclaredConstructor().newInstance().lookup(name);
290291
if (object instanceof DataSource) {
291292
return (DataSource) object;
292293
} else {
293294
throw new RepositoryException(
294295
"Object " + object + " with JNDI name "
295296
+ name + " is not a JDBC DataSource");
296297
}
297-
} catch (InstantiationException e) {
298-
throw new RepositoryException(
299-
"Invalid JNDI context: " + contextClass.getName(), e);
300-
} catch (IllegalAccessException e) {
298+
} catch (InstantiationException|IllegalAccessException|IllegalArgumentException|InvocationTargetException|NoSuchMethodException|SecurityException e) {
301299
throw new RepositoryException(
302300
"Invalid JNDI context: " + contextClass.getName(), e);
303301
} catch (NamingException e) {
@@ -328,7 +326,7 @@ private BasicDataSource getDriverDataSource(
328326
// The JDBC specification recommends the Class.forName
329327
// method without the .newInstance() method call,
330328
// but it is required after a Derby 'shutdown'
331-
instance = (Driver) driverClass.newInstance();
329+
instance = (Driver) driverClass.getDeclaredConstructor().newInstance();
332330
} catch (Throwable e) {
333331
// Ignore exceptions as there's no requirement for
334332
// a JDBC driver class to have a public default constructor

jackrabbit-jcr-client/src/main/java/org/apache/jackrabbit/client/RepositoryFactoryImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Repository getRepository(@SuppressWarnings("rawtypes") Map parameters) th
6565
Class<?> repositoryFactoryClass = Class.forName(repositoryFactoryName, true,
6666
Thread.currentThread().getContextClassLoader());
6767

68-
repositoryFactory = repositoryFactoryClass.newInstance();
68+
repositoryFactory = repositoryFactoryClass.getDeclaredConstructor().newInstance();
6969
}
7070
catch (Exception e) {
7171
throw new RepositoryException(e);

jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/server/remoting/davex/ProtectedRemoveManager.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.FileNotFoundException;
2222
import java.io.IOException;
2323
import java.io.InputStream;
24+
import java.lang.reflect.InvocationTargetException;
2425
import java.util.ArrayList;
2526
import java.util.List;
2627

@@ -91,14 +92,10 @@ ProtectedItemRemoveHandler createHandler(String className) {
9192
if (!className.isEmpty()) {
9293
Class<?> irHandlerClass = Class.forName(className);
9394
if (ProtectedItemRemoveHandler.class.isAssignableFrom(irHandlerClass)) {
94-
irHandler = (ProtectedItemRemoveHandler) irHandlerClass.newInstance();
95+
irHandler = (ProtectedItemRemoveHandler) irHandlerClass.getDeclaredConstructor().newInstance();
9596
}
9697
}
97-
} catch (ClassNotFoundException e) {
98-
log.error(e.getMessage(), e);
99-
} catch (InstantiationException e) {
100-
log.error(e.getMessage(), e);
101-
} catch (IllegalAccessException e) {
98+
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
10299
log.error(e.getMessage(), e);
103100
}
104101
return irHandler;

jackrabbit-jcr-server/src/main/java/org/apache/jackrabbit/webdav/simple/ResourceConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private static Object buildClassFromConfig(Element parent) {
351351
String className = DomUtil.getAttribute(classElem, "name", null);
352352
if (className != null) {
353353
Class<?> c = Class.forName(className);
354-
instance = c.newInstance();
354+
instance = c.getDeclaredConstructor().newInstance();
355355
} else {
356356
log.error("Invalid configuration: missing 'class' element");
357357
}

jackrabbit-jcr-tests/src/main/java/org/apache/jackrabbit/test/api/RepositoryFactoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected RepositoryFactory getRepositoryFactory()
4747
fail("Property '" + RepositoryStub.REPOSITORY_FACTORY + "' is not defined.");
4848
} else {
4949
try {
50-
return (RepositoryFactory) Class.forName(className).newInstance();
50+
return (RepositoryFactory) Class.forName(className).getDeclaredConstructor().newInstance();
5151
} catch (Exception e) {
5252
fail(e.toString());
5353
}

0 commit comments

Comments
 (0)