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
Improve LocalizedTextUtil defend NPE #35
Conversation
depend NPE if not ValueStack presented, for example non-struts request or backend thread.
Struts-JDK7-pull-request #24 SUCCESS |
could you add a unit test, please? |
I'm not sure if this is a good solution, can you provide more details? |
@lukaszlenart , It doesn't add new thing just null-check added, It's safe if existing unit tests passed. |
What I meant is that if |
ValueStack will be always not null with struts2 request, so It wouldn't change the behavior of struts2, this pull request is intend to use in non-struts2 context. |
That's the case, can you explain what you mean by non-struts2 context? |
simplest case import java.util.Locale;
public class Main {
public static void main(String[] args) {
System.out.println(LocalizedTextUtil.findText(Main.class, "key",Locale.getDefault(), "defaultMessage", null, null));
}
} Main.properties
it will print "this is a key" |
So you can use a public class StubValueStack implements ValueStack {
Map<String, Object> ctx = new HashMap<String, Object>();
CompoundRoot root = new CompoundRoot();
public Map<String, Object> getContext() {
return ctx;
}
public void setDefaultType(Class defaultType) {
}
public void setExprOverrides(Map<Object, Object> overrides) {
}
public Map<Object, Object> getExprOverrides() {
return null;
}
public CompoundRoot getRoot() {
return root;
}
public void setValue(String expr, Object value) {
ctx.put(expr, value);
}
public void setParameter(String expr, Object value) {
throw new UnsupportedOperationException("not implemented");
}
public void setValue(String expr, Object value, boolean throwExceptionOnFailure) {
ctx.put(expr, value);
}
public String findString(String expr) {
return (String) ctx.get(expr);
}
public String findString(String expr, boolean throwExceptionOnFailure) {
return findString(expr, false);
}
public Object findValue(String expr) {
return findValue(expr, false);
}
public Object findValue(String expr, boolean throwExceptionOnFailure) {
return ctx.get(expr);
}
public Object findValue(String expr, Class asType) {
return findValue(expr, asType, false);
}
public Object findValue(String expr, Class asType, boolean throwExceptionOnFailure) {
return ctx.get(expr);
}
public Object peek() {
return root.peek();
}
public Object pop() {
return root.pop();
}
public void push(Object o) {
root.push(o);
}
public void set(String key, Object o) {
ctx.put(key, o);
}
public int size() {
return root.size();
}
} |
It's just a static util class, It should be light and simple, your suggestion is opposite. |
But you cannot pass |
I hope so, please add a method drop last two arguments, thanks. |
Please re-open this PR against |
defend NPE if not ValueStack presented, for example non-struts request or backend thread.