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

This is the initial version of an 'java like' method matching for OGNL. #19

Merged
merged 1 commit into from
Apr 21, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>javassist</groupId>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.11.0.GA</version>
<version>3.20.0-GA</version>
</dependency>
</dependencies>

Expand Down
5 changes: 5 additions & 0 deletions src/java/ognl/ASTCtor.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ void setClassName(String className)
this.className = className;
}

Class getCreatedClass(OgnlContext context) throws ClassNotFoundException {
return OgnlRuntime.classForName(context, className);
}


void setArray(boolean value)
{
isArray = value;
Expand Down
75 changes: 71 additions & 4 deletions src/java/ognl/ASTMethod.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import ognl.enhance.UnsupportedCompilationException;

import java.lang.reflect.Method;
import java.util.List;

/**
* @author Luke Blanshard (blanshlu@netscape.net)
Expand Down Expand Up @@ -154,13 +155,13 @@ public String toGetSourceString(OgnlContext context, Object target)
Method m = null;

try {

m = OgnlRuntime.getMethod(context, context.getCurrentType() != null ? context.getCurrentType() : target.getClass(), _methodName, _children, false);
Class[] argumentClasses = getChildrenClasses(context, _children);
if (m == null)
m = OgnlRuntime.getReadMethod(target.getClass(), _methodName, _children != null ? _children.length : -1);
m = OgnlRuntime.getReadMethod(target.getClass(), _methodName, argumentClasses);

if (m == null) {
m = OgnlRuntime.getWriteMethod(target.getClass(), _methodName, _children != null ? _children.length : -1);
m = OgnlRuntime.getWriteMethod(target.getClass(), _methodName, argumentClasses);

if (m != null) {

Expand Down Expand Up @@ -327,7 +328,7 @@ public String toSetSourceString(OgnlContext context, Object target)
+ " last child? " + lastChild(context));*/
Method m = OgnlRuntime.getWriteMethod(context.getCurrentType() != null ?
context.getCurrentType() : target.getClass(),
_methodName, _children != null ? _children.length : -1);
_methodName, getChildrenClasses(context, _children));
if (m == null)
{
throw new UnsupportedCompilationException("Unable to determine setter method generation for " + _methodName);
Expand Down Expand Up @@ -486,4 +487,70 @@ public String toSetSourceString(OgnlContext context, Object target)

return result + ")" + post;
}

private static Class getClassMatchingAllChildren(OgnlContext context, Node[] _children) {
Class[] cc = getChildrenClasses(context, _children);
Class componentType = null;
for (int j = 0; j < cc.length; j++) {
Class ic = cc[j];
if (ic == null) {
componentType = Object.class; // fall back to object...
break;
} else {
if (componentType == null) {
componentType = ic;
} else {
if (!componentType.isAssignableFrom(ic)) {
if (ic.isAssignableFrom(componentType)) {
componentType = ic; // just swap... ic is more generic...
} else {
Class pc;
while ((pc = componentType.getSuperclass()) != null) { // TODO hmm - it could also be that an interface matches...
if (pc.isAssignableFrom(ic)) {
componentType = pc; // use this matching parent class
break;
}
}
if (!componentType.isAssignableFrom(ic)) {
// parents didn't match. the types might be primitives. Fall back to object.
componentType = Object.class;
break;
}
}
}
}
}
}
if (componentType == null)
componentType = Object.class;
return componentType;
}

private static Class[] getChildrenClasses(OgnlContext context, Node[] _children) {
if (_children == null)
return null;
Class[] argumentClasses = new Class[_children.length];
for (int i = 0; i < _children.length; i++) {
Node child = _children[i];
if (child instanceof ASTList) { // special handling for ASTList - it creates a List
//Class componentType = getClassMatchingAllChildren(context, ((ASTList)child)._children);
//argumentClasses[i] = Array.newInstance(componentType, 0).getClass();
argumentClasses[i] = List.class;
} else if (child instanceof NodeType) {
argumentClasses[i] = ((NodeType)child).getGetterClass();
} else if (child instanceof ASTCtor) {
try {
argumentClasses[i] = ((ASTCtor)child).getCreatedClass(context);
} catch (ClassNotFoundException nfe) {
throw OgnlOps.castToRuntime(nfe);
}
} else if (child instanceof ASTTest) {
argumentClasses[i] = getClassMatchingAllChildren(context, ((ASTTest)child)._children);
} else {
throw new UnsupportedOperationException("Don't know how to handle child: "+child);
}
}
return argumentClasses;
}

}
Loading