Skip to content
This repository has been archived by the owner on Apr 21, 2023. It is now read-only.

Commit

Permalink
[#513] fix ReflectExtensions.invoke for static methods
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Dietrich <[email protected]>
  • Loading branch information
cdietrich committed Feb 28, 2023
1 parent db25f39 commit 017a18e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2018, 2022 itemis AG (http://www.itemis.eu) and others.
* Copyright (c) 2018, 2023 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
Expand All @@ -8,6 +8,8 @@
*******************************************************************************/
package org.eclipse.xtext.xbase.tests.lib.util;

import static org.junit.Assert.*;

import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.util.ReflectExtensions;
import org.junit.Assert;
Expand Down Expand Up @@ -57,7 +59,41 @@ public void testInvokeWithPrimitives() throws Exception {
String x = "foo";
Assert.assertEquals("o", ext.invoke(x, "substring", 1, 2));
}


@Test
public void testInvokeWithStaticMethod() throws Exception {
String x = "foo";
ext.invoke(x, "join", "a", new String[]{"b", "c"});
}

@Test
public void testInvokeWithPrivateStaticMethod() throws Exception {
MyClass x = new MyClass();
ext.invoke(x, "doit");
}

@Test
public void testSetWithPrivateStaticField() throws Exception {
MyClass x = new MyClass();
ext.set(x, "x","doit");
}

@Test
public void testGetWithPrivateStaticField() throws Exception {
MyClass x = new MyClass();
assertEquals("old", ext.get(x, "x"));
}

public static class MyClass {
@SuppressWarnings("unused")
private static String doit() {
return "doit";
}
@SuppressWarnings("unused")
private String x = "old";
}

@Test
public void testGet_01() throws Exception {
ReflectExtensionsTest x = new ReflectExtensionsTest();
Expand Down Expand Up @@ -96,4 +132,5 @@ public void testSet_03() throws Exception {
Assert.assertNull(x.privateExt);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -119,8 +120,15 @@ public Object invoke(Object receiver, String methodName, /* @Nullable */ Object.
}
} while(compatible == null && (clazz = clazz.getSuperclass()) != null);
if (compatible != null) {
if (!compatible.canAccess(receiver))
compatible.setAccessible(true);
boolean isStatic = Modifier.isStatic(compatible.getModifiers());
if (isStatic) {
if (!compatible.canAccess(null))
compatible.setAccessible(true);
}
else {
if (!compatible.canAccess(receiver))
compatible.setAccessible(true);
}
return compatible.invoke(receiver, arguments);
}
// not found provoke method not found exception
Expand Down

0 comments on commit 017a18e

Please sign in to comment.