3

Is it possible in Java, to declare a method with a string instead of an identifier?

For example can I do something like the following:

class Car{

     new Method("getFoo", {
          return 1+1;
     });
}

//Use it
Car car = new Car();
car.getFoo();

EDIT: I am adding a Purpose WHY I need this. In order to not hardcode method names when using Jersey and its UriBuilder, which requires a method name: https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/UriBuilder.html

See path() method with signature:

 public abstract UriBuilder path(java.lang.Class resource,
                            java.lang.String method)
                     throws java.lang.IllegalArgumentException

So then I may just use string constants and not worry that the method name will ever be different from the string that I am passing to the path() method.

I hope my question is clear, if not - let me know and I can clarify it.

2
  • What are you ultimately trying to accomplish with the solutions to your 2 recent questions? Commented Jun 11, 2010 at 14:29
  • I believe you could use reflection to achieve that - but why would you want to? Do you want to generate classes dynamically? Commented Jun 11, 2010 at 14:31

4 Answers 4

2

It's not possible in the way you described.

The closest thing is probably the asm library to create java bytecode.

Sign up to request clarification or add additional context in comments.

Comments

1

As per the purpose, why don't you just have a single method and let it act/behave differently depending on the caller and the parameters?

3 Comments

That is what I am currently doing. I have the same method, say "getFoo" in 5 different classes, and then I just pas the string "getFoo" to the UriBuilder. The problem is that if any of the method names change in the future, to say "getBar" - my string "getFoo" will still be passed to UriBuilder which will try to do reflection on it - and come up empty handed.
@drozzy Sounds like you should be declaring getFoo in an interface and having your 5 classes implement it?
Thanks, but I actually came up with a solution - I will only use Path annotations on classes and not methods. That way I will not have to refer to method names ever.
0

It's indeed not great to have the method name as string in the code, but if feel the usage of reflection from your side and from jax-rs side should compensate so that this does not happen.

Let me clarify. I guess you are using UriBuilder because you want to expose a service, or something similar. If you reflect on the class and list the method, then pass their name in UriBuilder, which also reflect on the class, the method is never explicitly mentioned in the source.

I'm not familiar with jax-rs though, and without knowing more about what you exactly try to achieve (your edit does provide a bit more information, but does not explain the end goal you have in mind), I don't know if that makes sense. But it could be a track to follow.

Comments

0

If you can consider using another language on the JVM, Groovy can do that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.