Java Classloaders - Invoking a static method inside a private class

user1301318

I would like to invoke a static method inside of a private class in Java using classloaders.

This is a short version of my classloader that I have been using.

URL[] jarURLArray = { server.lan.serverJAR().toURL() };
URLClassLoader serverClassLoader = new URLClassLoader(jarURLArray,  this.getClass().getClassLoader());
Class mainClass = Class.forName("com.packagename.someclass", true, serverClassLoader);
Class sampleArgClass[] = { (new String[1]).getClass() };
Method mainMethod = mainClass.getDeclaredMethod("getSimplifiedName", sampleArgClass);
Object mainMethodInstance = mainClass.newInstance();
Object serverArgConverted[] = { args };
Object result = mainMethod.invoke(mainMethodInstance, serverArgConverted);

This code loads classes from a jar file and I am able to invoke classes under normal circumstances.

When I have a class, such as this one:

public final class someClass
{
private static Server server;

/**
 * Static class cannot be initialized.
 */
private someClass()
{
}

public static int someValue()
{
    return someValue;
}

I am unable to reach the someValue() method because of how the classloader creates new instances of the class, which, is not possible because it has a private constructor.

How can I reach the someValue method using a classloader?

Jon Skeet

The classloader isn't creating a new instance: you're telling the VM to create a new instance here:

Object mainMethodInstance = mainClass.newInstance();

Don't do that. Just pass in null as the target of the static method call:

Object result = mainMethod.invoke(null, serverArgConverted);

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Access method of private class inside static inner class from main()

分類Dev

Private Static Members inside a Static Class... Good idea?

分類Dev

Invoking static method dynamically in powershell

分類Dev

unit test for private static method in non-static class

分類Dev

Accessing a method of the Outer Class inside a static Inner Class

分類Dev

Access The Type of the Containing Class of a Static Method in Java

分類Dev

Generic static method inside usual class; uses array of basic type

分類Dev

Invoking a singleton class in a abstract factory method

分類Dev

Invoking a toString method from another class

分類Dev

Invoking a non-static method via the scope resolution operator

分類Dev

How to access private class's property from public static method in PHP

分類Dev

Extending private class in Java

分類Dev

Static class method in go language

分類Dev

Static class method in go language

分類Dev

Run method inside a method in java

分類Dev

use Winform Objects inside an Static Method

分類Dev

Enforcing a static method compilation inside a subclass with Kotlin?

分類Dev

define a decorator as method inside class

分類Dev

Cost of invoking a method on Android

分類Dev

Using Mockito: Matching multiple arguments in a private static method?

分類Dev

Java - local variables in static method

分類Dev

Typescript access to private static fields within same class

分類Dev

Generate method that calls class private field method by just forwarding parameters?

分類Dev

iOS - Not able to access Static method of swift class

分類Dev

How to spyOn a static class method with Jasmine

分類Dev

Add static method to current custom class

分類Dev

Haxe macro to call static method of a class

分類Dev

Call static method in another class library

Related 関連記事

  1. 1

    Access method of private class inside static inner class from main()

  2. 2

    Access method of private class inside static inner class from main()

  3. 3

    Private Static Members inside a Static Class... Good idea?

  4. 4

    Invoking static method dynamically in powershell

  5. 5

    unit test for private static method in non-static class

  6. 6

    Accessing a method of the Outer Class inside a static Inner Class

  7. 7

    Access The Type of the Containing Class of a Static Method in Java

  8. 8

    Generic static method inside usual class; uses array of basic type

  9. 9

    Invoking a singleton class in a abstract factory method

  10. 10

    Invoking a toString method from another class

  11. 11

    Invoking a non-static method via the scope resolution operator

  12. 12

    How to access private class's property from public static method in PHP

  13. 13

    Extending private class in Java

  14. 14

    Static class method in go language

  15. 15

    Static class method in go language

  16. 16

    Run method inside a method in java

  17. 17

    use Winform Objects inside an Static Method

  18. 18

    Enforcing a static method compilation inside a subclass with Kotlin?

  19. 19

    define a decorator as method inside class

  20. 20

    Cost of invoking a method on Android

  21. 21

    Using Mockito: Matching multiple arguments in a private static method?

  22. 22

    Java - local variables in static method

  23. 23

    Typescript access to private static fields within same class

  24. 24

    Generate method that calls class private field method by just forwarding parameters?

  25. 25

    iOS - Not able to access Static method of swift class

  26. 26

    How to spyOn a static class method with Jasmine

  27. 27

    Add static method to current custom class

  28. 28

    Haxe macro to call static method of a class

  29. 29

    Call static method in another class library

ホットタグ

アーカイブ