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

Support concurrent on DefaultClassResolver #46

Merged
Changes from 3 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
38 changes: 19 additions & 19 deletions src/main/java/ognl/DefaultClassResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
//--------------------------------------------------------------------------
package ognl;

import java.util.*;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* Default class resolution. Uses Class.forName() to look up classes by name.
Expand All @@ -41,28 +42,27 @@
*/
public class DefaultClassResolver extends Object implements ClassResolver
{
private Map classes = new HashMap(101);
private final Map<String, Class> classes = new ConcurrentHashMap<>(101);

public DefaultClassResolver()
{
super();
}

public Class classForName(String className, Map context) throws ClassNotFoundException
{
Class result = null;

if ((result = (Class)classes.get(className)) == null) {
try {
result = Class.forName(className);
} catch (ClassNotFoundException ex) {
if (className.indexOf('.') == -1) {
result = Class.forName("java.lang." + className);
classes.put("java.lang." + className, result);
}
}
classes.put(className, result);
}
return result;
}
public Class classForName(String className, Map context) throws ClassNotFoundException
{
Class result = classes.get(className);
if (result != null) {
return result;
}
if (className.indexOf('.') == -1) {
String langClassName = "java.lang." + className;
result = Class.forName(langClassName);
classes.put(langClassName, result);
} else {
result = Class.forName(className);
}
classes.put(className, result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will duplicate langClassName, so we will end up with two entries for the same class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment.
I think this behavior is same as the current version. Should change this behavior in this change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, which means the java.lang. classes will be always located by the short version (Integer instead of java.lang.Integer). I think .put in line 61 can be thrown aways.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've fixed your comment. please review again.
Thanks.

return result;
}
}