Skip to content
Open
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
1 change: 1 addition & 0 deletions core/src/test/org/jnode/test/CoreTestSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
@SuiteClasses({
NumberUtilsTest.class,
VersionTest.class,
ViewMethodTest.class,
}
)
public class CoreTestSuite {
Expand Down
182 changes: 166 additions & 16 deletions core/src/test/org/jnode/test/ViewMethodTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,182 @@
* along with this library; If not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.jnode.test;

import org.jnode.vm.classmgr.VmMethod;
import org.jnode.vm.classmgr.VmType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertArrayEquals;

/**
* @author Ewout Prangsma (epr@users.sourceforge.net)
* JUnit4 test for java.lang.reflect.Method API.
* Tests Method.getName, getReturnType, getParameterTypes, getModifiers, isBridge, isVarArgs, invoke
*/
public class ViewMethodTest {

public static void main(String[] args) throws ClassNotFoundException {
final String className = args[0];
final String mname = (args.length > 1) ? args[1] : null;
// Test class with various method signatures
public static class TestClass {
public String publicMethod(int a, String b) {
return "public";
}

private void privateMethod() {
}

protected int protectedMethod(long l) {
return 42;
}

static boolean staticMethod(double d) {
return true;
}

public void varArgsMethod(String... args) {
}

ClassLoader cl = Thread.currentThread().getContextClassLoader();
final VmType cls = VmType.fromClass(cl.loadClass(className));
@Deprecated
public String deprecatedMethod() {
return "deprecated";
}

final int cnt = cls.getNoDeclaredMethods();
for (int i = 0; i < cnt; i++) {
final VmMethod method = cls.getDeclaredMethod(i);
if ((mname == null) || method.getName().equals(mname)) {
System.out.println("OptL: " + method.getNativeCodeOptLevel());
System.out.println("Code: " + method.getDefaultCompiledCode());
}
public String bridgeMethod() {
return "bridge";
}
}

@Test
public void testGetName() throws Exception {
Method method = TestClass.class.getMethod("publicMethod", int.class, String.class);
assertEquals("publicMethod", method.getName());

Method privateMethod = TestClass.class.getDeclaredMethod("privateMethod");
assertEquals("privateMethod", privateMethod.getName());
}

@Test
public void testGetReturnType() throws Exception {
Method method = TestClass.class.getMethod("publicMethod", int.class, String.class);
assertEquals(String.class, method.getReturnType());

Method voidMethod = TestClass.class.getDeclaredMethod("privateMethod");
assertEquals(void.class, voidMethod.getReturnType());

Method intMethod = TestClass.class.getDeclaredMethod("protectedMethod", long.class);
assertEquals(int.class, intMethod.getReturnType());
}

@Test
public void testGetParameterTypes() throws Exception {
Method method = TestClass.class.getMethod("publicMethod", int.class, String.class);
Class<?>[] params = method.getParameterTypes();
assertEquals(2, params.length);
assertEquals(int.class, params[0]);
assertEquals(String.class, params[1]);

Method noParamMethod = TestClass.class.getDeclaredMethod("privateMethod");
assertEquals(0, noParamMethod.getParameterTypes().length);

Method varArgsMethod = TestClass.class.getMethod("varArgsMethod", String[].class);
Class<?>[] varArgsParams = varArgsMethod.getParameterTypes();
assertEquals(1, varArgsParams.length);
assertEquals(String[].class, varArgsParams[0]);
}

@Test
public void testGetModifiers() throws Exception {
Method publicMethod = TestClass.class.getMethod("publicMethod", int.class, String.class);
int modifiers = publicMethod.getModifiers();
assertTrue(Modifier.isPublic(modifiers));
assertFalse(Modifier.isPrivate(modifiers));
assertFalse(Modifier.isProtected(modifiers));
assertFalse(Modifier.isStatic(modifiers));

Method privateMethod = TestClass.class.getDeclaredMethod("privateMethod");
int privateModifiers = privateMethod.getModifiers();
assertTrue(Modifier.isPrivate(privateModifiers));
assertFalse(Modifier.isPublic(privateModifiers));

Method protectedMethod = TestClass.class.getDeclaredMethod("protectedMethod", long.class);
int protectedModifiers = protectedMethod.getModifiers();
assertTrue(Modifier.isProtected(protectedModifiers));

Method staticMethod = TestClass.class.getDeclaredMethod("staticMethod", double.class);
int staticModifiers = staticMethod.getModifiers();
assertTrue(Modifier.isStatic(staticModifiers));
}

@Test
public void testIsBridge() throws Exception {
Method method = TestClass.class.getMethod("publicMethod", int.class, String.class);
assertFalse(method.isBridge());

Method bridgeMethod = TestClass.class.getMethod("bridgeMethod");
// bridgeMethod is not actually a bridge method in this simple test class
assertFalse(bridgeMethod.isBridge());
}

@Test
public void testIsVarArgs() throws Exception {
Method regularMethod = TestClass.class.getMethod("publicMethod", int.class, String.class);
assertFalse(regularMethod.isVarArgs());

Method varArgsMethod = TestClass.class.getMethod("varArgsMethod", String[].class);
assertTrue(varArgsMethod.isVarArgs());
}

@Test
public void testInvoke() throws Exception {
TestClass instance = new TestClass();

// Test public method invoke
Method publicMethod = TestClass.class.getMethod("publicMethod", int.class, String.class);
Object result = publicMethod.invoke(instance, 42, "test");
assertEquals("public", result);

// Test static method invoke
Method staticMethod = TestClass.class.getDeclaredMethod("staticMethod", double.class);
staticMethod.setAccessible(true);
Object staticResult = staticMethod.invoke(null, 3.14);
assertEquals(Boolean.TRUE, staticResult);

// Test protected method invoke
Method protectedMethod = TestClass.class.getDeclaredMethod("protectedMethod", long.class);
protectedMethod.setAccessible(true);
Object protectedResult = protectedMethod.invoke(instance, 100L);
assertEquals(Integer.valueOf(42), protectedResult);

// Test method with no return value
Method voidMethod = TestClass.class.getDeclaredMethod("privateMethod");
voidMethod.setAccessible(true);
Object voidResult = voidMethod.invoke(instance);
assertEquals(null, voidResult); // invoke returns null for void methods
}

@Test
public void testInvokeWithVarArgs() throws Exception {
TestClass instance = new TestClass();
Method varArgsMethod = TestClass.class.getMethod("varArgsMethod", String[].class);

// Invoke with varargs
varArgsMethod.invoke(instance, (Object) new String[]{"a", "b", "c"});
varArgsMethod.invoke(instance, (Object) new String[]{});
varArgsMethod.invoke(instance, (Object) null);
}

@Test
public void testDeprecatedMethod() throws Exception {
Method deprecatedMethod = TestClass.class.getMethod("deprecatedMethod");
assertTrue(deprecatedMethod.isAnnotationPresent(Deprecated.class));

TestClass instance = new TestClass();
Object result = deprecatedMethod.invoke(instance);
assertEquals("deprecated", result);
}
}
Loading