Skip to content

Commit

Permalink
Move all calls to Class.forName to a central place
Browse files Browse the repository at this point in the history
... so I can deal with plugin loading in a decent way.
  • Loading branch information
amolenaar committed Dec 27, 2017
1 parent 97149a8 commit e4fb325
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/fitnesse/authentication/PasswordFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.List;
import java.util.Map;

import fitnesse.util.ClassUtils;
import util.FileUtil;

public class PasswordFile {
Expand Down Expand Up @@ -71,7 +72,7 @@ private void loadCipher(LinkedList<String> lines) throws IllegalAccessException,
}

public PasswordCipher instantiateCipher(String cipherClassName) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
cipher = (PasswordCipher) Class.forName(cipherClassName).newInstance();
cipher = (PasswordCipher) ClassUtils.forName(cipherClassName).newInstance();
return cipher;
}

Expand Down
3 changes: 2 additions & 1 deletion src/fitnesse/components/ComponentFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Properties;

import fitnesse.ConfigurationParameter;
import fitnesse.util.ClassUtils;

/**
* Create components for FitNesse.
Expand Down Expand Up @@ -40,7 +41,7 @@ public <T> T createComponent(String componentType, Class<T> defaultComponent) th
Class<?> componentClass;
try {
if (componentClassName != null)
componentClass = Class.forName(componentClassName);
componentClass = ClassUtils.forName(componentClassName);
else
componentClass = defaultComponent;
} catch (Exception e) {
Expand Down
5 changes: 4 additions & 1 deletion src/fitnesse/plugins/PropertyBasedPluginFeatureFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import fitnesse.testsystems.slim.CustomComparatorRegistry;
import fitnesse.testsystems.slim.tables.SlimTable;
import fitnesse.testsystems.slim.tables.SlimTableFactory;
import fitnesse.util.ClassUtils;
import fitnesse.wiki.WikiPageFactory;
import fitnesse.wiki.WikiPageFactoryRegistry;
import fitnesse.wikitext.parser.SymbolProvider;
Expand Down Expand Up @@ -195,7 +196,9 @@ private static Collection<PluginFeatureFactory> createWrappersForLegacyPlugins(C
@SuppressWarnings("unchecked")
private static <T> Class<T> forName(String className) throws PluginException {
try {
return (Class<T>) Class.forName(className);
// TODO: Defer loading to ComponentFactory
// return componentFactory.createComponent(className);
return ClassUtils.forName(className);
} catch (ClassNotFoundException e) {
throw new PluginException("Unable to load class " + className, e);
}
Expand Down
18 changes: 10 additions & 8 deletions src/fitnesse/socketservice/SslParameters.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package fitnesse.socketservice;

import fitnesse.util.ClassUtils;

import javax.net.ssl.SSLServerSocketFactory;
import javax.net.ssl.SSLSocketFactory;

Expand All @@ -25,18 +27,18 @@ protected void setKeyStorePassword(String value) {
protected void setTrustStoreFilename(String filename) {
if(filename != null) this.trustStoreFilename = filename;
}


private void setProperty(String tag, String value, String defaultValue) {
if ( value == null) value = defaultValue;
if (value == null){
System.clearProperty(tag);
if (value == null){
System.clearProperty(tag);
}
else{
System.setProperty(tag, value);
}
}

protected SslParameters(){
}

Expand All @@ -45,13 +47,13 @@ protected SslParameters(String keyStoreFilename, String keyStorePassword, String
setKeyStorePassword(keyStorePassword);
setTrustStoreFilename(trustStoreFilename);
}

protected void prepareGlobalConfiguration(){
// Save the current values so that they can be restored
keyStoreFilenameOld = System.getProperty("javax.net.ssl.keyStore" );
keyStorePasswordOld = System.getProperty("javax.net.ssl.keyStorePassword");
trustStoreFilenameOld= System.getProperty("javax.net.ssl.trustStore");

setProperty("javax.net.ssl.keyStore", keyStoreFilename, "fitnesse.jks" );
setProperty("javax.net.ssl.keyStorePassword", keyStorePassword, "FitNesse42");
setProperty("javax.net.ssl.trustStore", trustStoreFilename, "fitnesse.jks");
Expand All @@ -71,7 +73,7 @@ public static SslParameters setSslParameters(String sslParameterClassName) {
sslParametersInstance= SslParameters.class;
}else{
try {
sslParametersInstance= Class.forName(sslParameterClassName).asSubclass(SslParameters.class);
sslParametersInstance= ClassUtils.forName(sslParameterClassName).asSubclass(SslParameters.class);
} catch (ClassNotFoundException e) {
throw new RuntimeException("Preparing SSL Parameters with Class " + sslParameterClassName + " failed. Class Not Found.", e);
}
Expand All @@ -82,7 +84,7 @@ public static SslParameters setSslParameters(String sslParameterClassName) {
throw new RuntimeException("Preparing SSL Parameters with Class " + sslParameterClassName + " failed.", e);
}
}

public SSLServerSocketFactory createSSLServerSocketFactory(){
SSLServerSocketFactory ssf;
prepareGlobalConfiguration();
Expand Down
3 changes: 2 additions & 1 deletion src/fitnesse/testsystems/fit/CommandRunningFitClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import fitnesse.testsystems.CommandRunner;
import fitnesse.testsystems.ExecutionLogListener;
import fitnesse.testsystems.MockCommandRunner;
import fitnesse.util.ClassUtils;
import org.apache.commons.lang.ArrayUtils;

public class CommandRunningFitClient extends FitClient {
Expand Down Expand Up @@ -269,7 +270,7 @@ private boolean tryCreateTestRunner(Method testRunnerMethod, String[] args) {

private Method getTestRunnerMethod(String testRunner) {
try {
return Class.forName(testRunner).getDeclaredMethod("main", String[].class);
return ClassUtils.forName(testRunner).getDeclaredMethod("main", String[].class);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
Expand Down
13 changes: 13 additions & 0 deletions src/fitnesse/util/ClassUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fitnesse.util;

public class ClassUtils {

private ClassUtils() {
}

@SuppressWarnings("unchecked")
public static <T> Class<T> forName(String className) throws ClassNotFoundException {
return (Class<T>) Class.forName(className);
}

}
3 changes: 2 additions & 1 deletion src/fitnesse/wikitext/parser/ColoredSlimTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import fitnesse.testsystems.slim.tables.ScriptTable;
import fitnesse.testsystems.slim.tables.SlimTable;
import fitnesse.testsystems.slim.tables.SlimTableFactory;
import fitnesse.util.ClassUtils;

public class ColoredSlimTable extends SymbolTypeDecorator{

Expand Down Expand Up @@ -114,7 +115,7 @@ public String toTarget(Translator translator, Symbol symbol) {

private boolean isValidClass(String potentialClass) {
try {
return Class.forName(potentialClass) != null;
return ClassUtils.forName(potentialClass) != null;
} catch (Exception e) {
return false;
}catch (NoClassDefFoundError e) {
Expand Down

0 comments on commit e4fb325

Please sign in to comment.