Skip to main content
deleted 199 characters in body
Source Link
Andrew
  • 49.8k
  • 15
  • 99
  • 145

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // exception handling omitted for... brevity
}
  catch (NoSuchMethodException e) {
  // exception handling omitted for... brevity
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodNamemethodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { // exception handling omitted for... brevity
}
  catch (IllegalAccessException e) { // exception handling omitted for... brevity
}
  catch (InvocationTargetException e) { // exception handling omitted for... brevity}

Again, leave out the arguments in .invoke.invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // exception handling omitted for brevity
} catch (NoSuchMethodException e) {
  // exception handling omitted for brevity
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { // exception handling omitted for brevity
} catch (IllegalAccessException e) { // exception handling omitted for brevity
} catch (InvocationTargetException e) { // exception handling omitted for brevity

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) { ... }
  catch (NoSuchMethodException e) { ... }

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { ... }
  catch (IllegalAccessException e) { ... }
  catch (InvocationTargetException e) { ... }

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

added 196 characters in body
Source Link
user177800
user177800

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...exception handling omitted for brevity
} catch (NoSuchMethodException e) {
  // ...exception handling omitted for brevity
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { // exception handling omitted for brevity
} catch (IllegalAccessException e) { // exception handling omitted for brevity
} catch (InvocationTargetException e) { // exception handling omitted for brevity

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // exception handling omitted for brevity
} catch (NoSuchMethodException e) {
  // exception handling omitted for brevity
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) { // exception handling omitted for brevity
} catch (IllegalAccessException e) { // exception handling omitted for brevity
} catch (InvocationTargetException e) { // exception handling omitted for brevity

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

added 18 characters in body
Source Link
yegor256
  • 105.8k
  • 131
  • 465
  • 629

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Coding from the hip, it would be something like:

java.lang.reflect.Method method;
try {
  method = obj.getClass().getMethod(methodName, param1.class, param2.class, ..);
} catch (SecurityException e) {
  // ...
} catch (NoSuchMethodException e) {
  // ...
}

The parameters identify the very specific method you need (if there are several overloaded available, if the method has no arguments, only give methodName).

Then you invoke that method by calling

try {
  method.invoke(obj, arg1, arg2,...);
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {

Again, leave out the arguments in .invoke, if you don't have any. But yeah. Read about Java Reflection

Source Link
Henrik Paul
  • 68k
  • 32
  • 87
  • 96
Loading