It doesn't even have to be multiple threads. Multiple instances in the same thread can clobber a static structure if careless. They just take turns messing each other up.
Hi,
I put together a more complete example ( however it is not so small).
Here is the class ( see below).
The map will contain more values. Maybe I can do it final since it
will not change when values are loaded.
I will use the output from runtime value of
test.TextHelp.getCurrentActivity().
When I run the code below I get:
java.lang.NoSuchMethodException: test.TextHelp.getCurrentActivity()()
at java.lang.Class.getMethod(Class.java:1581)
at test.TextHelp.getValue(TextHelp.java:70)
at test.TextHelp.replace(TextHelp.java:47)
at test.TextHelp.main(TextHelp.java:21)
Any ideas?
I am using java 1.5 and cannot go with 1.6 yet.
Thanks for all your comments.
//mike
package test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TextHelp {
public String getCurrentActivity(){
return "my activity name";
}
public static void main (String [] args){
TextHelp th = new TextHelp();
System.out.println("Output before is: act-{stream}");
String runtimeText = TextHelp.replace("act-{stream}");
System.out.println("Output after is: "+runtimeText);
}
private static Map<String, String> replacements = new HashMap<String,
String>();
static{
replacements.put("{stream}","test.TextHelp:getCurrentActivity()");
}
public static String replace(final String msg) {
if (msg == null || "".equals(msg) || replacements == null
|| replacements.isEmpty()) {
return msg;
}
StringBuilder regexBuilder = new StringBuilder();
Iterator<String> it = replacements.keySet().iterator();
regexBuilder.append(Pattern.quote(it.next()));
while (it.hasNext()) {
regexBuilder.append('|').append(Pattern.quote(it.next()));
}
Matcher matcher =
Pattern.compile(regexBuilder.toString()).matcher(msg);
StringBuffer out = new StringBuffer(msg..length() + (msg.length() /
10));
while (matcher.find()) {
String toBeSubstituted = replacements.get(matcher.group());
String replacement = getValue(toBeSubstituted);
matcher.appendReplacement(out, replacement);
//matcher.appendReplacement(out,
replacements.get(matcher.group()));
}
matcher.appendTail(out);
System.out.println("OUT "+out);
return out.toString();
}
public static String getValue(String expression) {
String[] parts = expression.split(":");
// Obtain the Class instance
String result = "";
try {
// Obtain the Class
// Obtain the Class instance
Class cls = Class.forName(parts[0]);
// Get the method
Method method = cls.getMethod(parts[1],null);
// Create the object thatwe want to invoke the methods on
TextHelp provider = (TextHelp) cls
.newInstance();
// Call the method. Sincenone of them takes arguments we just
// pass an empty array assecond parameter.
result = (String)method..invoke(provider, new Object[0]);
} catch (IllegalArgumentException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SecurityException ex) {
ex.printStackTrace();
} catch (NoSuchMethodException ex) {
ex.printStackTrace();
} catch (InstantiationException ex) {
ex.printStackTrace();
}
System.out.println("Result " + ": " + result);
return result;
}
}