What you want is not possible, because the Java compiler needs to know the types as well as the names of variables and methods at bytecode generation time. Because Java bytecode is typed and some Java-source-level conversions are not known to the JVM (e.g., unboxing conversion is purely a Java compiler convention), the compiler cannot get away with emitting bytecode that says "load field f
" or "call method foo
"; the compiler must say "load field f
of type double
" or "call method f
of type int(double, Object)
".
Consider reading an unknown field of an object and assigning it to a double
local variable:
double d = o.f;
If the type of f
is double
, the compiler will simply use a getfield
followed by a dstore
into the local variable slot for d
. If f
is of type int
, the compiler will emit an i2d
instruction between the getfield
and dstore
to convert the int
to a double
. If f
is of type Double
, the compiler will emit a call to doubleValue()
to unbox the loaded value before storing it. If f
is of type Object
, the assignment is illegal in Java without a cast (which would be a checkcast
instruction if it were legal). If the class of x
doesn't have a field named x
, there's no possible translation for this assignment.
Similar considerations apply when calling a method with unknown signature, which may require conversions for each of its arguments and/or creating an array for a varargs call. Further, at the JVM level, overloading on return type is allowed, so even if there's only one list of argument types, the return type must also be specified.
The best you can do is provide dummy declarations of the unknown classes so the compiler knows what to do, as suggested by paulsm4's answer.