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

Commit

Permalink
Merge pull request #514 from eclipse/cd_issue513
Browse files Browse the repository at this point in the history
[#513] fix ReflectExtensions.invoke for static methods
  • Loading branch information
cdietrich authored Mar 2, 2023
2 parents db25f39 + 017a18e commit 8b723c9
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 8b723c9

Please sign in to comment.