Skip to content

Commit

Permalink
Merge pull request #33 from quaff/master
Browse files Browse the repository at this point in the history
Add access for property which read method is java8 default method
  • Loading branch information
lukaszlenart authored Aug 1, 2017
2 parents 751acc8 + b041eed commit 992c531
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 8 deletions.
46 changes: 38 additions & 8 deletions src/java/ognl/OgnlRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ public static Map getMethods(Class targetClass, boolean staticMethods)
{
toExamined.add(c);

// Including interfaces is needed as from Java 8 intefaces can implement defaul methods
// Including interfaces is needed as from Java 8 intefaces can implement default methods
toExamined.addAll(Arrays.asList(c.getInterfaces()));
}

Expand Down Expand Up @@ -2149,6 +2149,37 @@ public static List getDeclaredMethods(Class targetClass, String propertyName, bo
}
}
}
// Add default methods on interface
for (Class c = targetClass; c != null; c = c.getSuperclass()) {
for(Class intf : c.getInterfaces()){
Method[] methods = intf.getDeclaredMethods();

for (int i = 0; i < methods.length; i++) {
if (Modifier.isAbstract(methods[i].getModifiers()))
continue;

String ms = methods[i].getName();

if (ms.endsWith(baseName)) {
boolean isSet = false, isIs = false;

if ((isSet = ms.startsWith(SET_PREFIX)) || ms.startsWith(GET_PREFIX)
|| (isIs = ms.startsWith(IS_PREFIX))) {
int prefixLength = (isIs ? 2 : 3);

if (isSet == findSets) {
if (baseName.length() == (ms.length() - prefixLength)) {
if (result == null) {
result = new ArrayList();
}
result.add(methods[i]);
}
}
}
}
}
}
}
if (propertyCache == null) {
cache.put(targetClass, propertyCache = new HashMap(101));
}
Expand Down Expand Up @@ -2912,15 +2943,14 @@ public static Method getReadMethod(Class target, String name, Class[] argClasses

name = name.toLowerCase();

BeanInfo info = Introspector.getBeanInfo(target);
MethodDescriptor[] methods = info.getMethodDescriptors();
Method[] methods = target.getMethods();

// exact matches first
ArrayList<Method> candidates = new ArrayList<Method>();

for (int i = 0; i < methods.length; i++)
{
if (!isMethodCallable(methods[i].getMethod()))
if (!isMethodCallable(methods[i]))
continue;

if ((methods[i].getName().equalsIgnoreCase(name)
Expand All @@ -2929,7 +2959,7 @@ public static Method getReadMethod(Class target, String name, Class[] argClasses
|| methods[i].getName().toLowerCase().equals("is" + name))
&& !methods[i].getName().startsWith("set"))
{
candidates.add(methods[i].getMethod());
candidates.add(methods[i]);
}
}
if (!candidates.isEmpty()) {
Expand All @@ -2940,17 +2970,17 @@ public static Method getReadMethod(Class target, String name, Class[] argClasses

for (int i = 0; i < methods.length; i++)
{
if (!isMethodCallable(methods[i].getMethod()))
if (!isMethodCallable(methods[i]))
continue;

if (methods[i].getName().equalsIgnoreCase(name)
&& !methods[i].getName().startsWith("set")
&& !methods[i].getName().startsWith("get")
&& !methods[i].getName().startsWith("is")
&& !methods[i].getName().startsWith("has")
&& methods[i].getMethod().getReturnType() != Void.TYPE) {
&& methods[i].getReturnType() != Void.TYPE) {

Method m = methods[i].getMethod();
Method m = methods[i];
if (!candidates.contains(m))
candidates.add(m);
}
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/ognl/Java8Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ognl;

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

import junit.framework.TestCase;
Expand All @@ -10,18 +12,31 @@ public void testDefaultMethodOnClass() {
/* defaultMethod(); */
List defaultMethod = OgnlRuntime.getMethods(ClassWithDefaults.class, "defaultMethod", false);
assertNotNull(defaultMethod);
Method method = OgnlRuntime.getReadMethod(ClassWithDefaults.class, "defaultMethod");
assertNotNull(method);
}

public void testDefaultMethodOnSubClass() {
/* defaultMethod(); */
List defaultMethod = OgnlRuntime.getMethods(SubClassWithDefaults.class, "defaultMethod", false);
assertNotNull(defaultMethod);
Method method = OgnlRuntime.getReadMethod(SubClassWithDefaults.class, "defaultMethod");
assertNotNull(method);
}

public void testGetDeclaredMethods() throws IntrospectionException, OgnlException{
List defaultMethod = OgnlRuntime.getDeclaredMethods(SubClassWithDefaults.class, "name", false);
assertNotNull(defaultMethod);
defaultMethod = OgnlRuntime.getDeclaredMethods(ClassWithDefaults.class, "name", false);
assertNotNull(defaultMethod);
}

}

class SubClassWithDefaults extends ClassWithDefaults {

public String getName() { return "name"; }

}

class ClassWithDefaults /* implements InterfaceWithDefaults */ {
Expand All @@ -33,5 +48,6 @@ class ClassWithDefaults /* implements InterfaceWithDefaults */ {
*
interface InterfaceWithDefaults {
default public void defaultMethod() { }
default public String getName() { return "name"; }
}
*/

0 comments on commit 992c531

Please sign in to comment.