0

I want to write a method that takes as a parameter either result of Lambdaj.on() call or a string.

public static <T> void method(T arg) {
}

T can be String here. Is it possible to determine if arg was created using Lambdaj.on() method?

alkedr
  • 754
  • 6
  • 17
  • Does `Lambdaj.on` ever return String? If not, and `!(arg instanceof String)`, then it came from `Lambdaj.on` (or is null). – Jeffrey Bosboom Oct 18 '14 at 18:34
  • Yes, `Lambdaj.on` can return any type. This is really cool because you can write `on(SomeClass.class).getX().getY()` – alkedr Oct 18 '14 at 18:40
  • Then you can't, because if you receive a String it might be a String the user passed in directly or a String returned by `getY()`, and any information about the source has been lost. (On the other hand, if `getY` actually returns a type that subclasses String and adds some interfaces, you can test for the presence of those interfaces. That seems impossible without a modified JVM, however, as String is final.) – Jeffrey Bosboom Oct 18 '14 at 18:43
  • If I recall correctly, Lambdaj remembers returned values internally. If it uses identity comparison for strings I could check if `arg` is stored in Lambdaj internals (most likely by trying to pass `arg` to some Lambdaj method). But I don't know if it's safe or correct or defined behavior. – alkedr Oct 18 '14 at 18:54

1 Answers1

0

After reading Lambdaj source code (ArgumentsFactory.java for the most part) I can conclude that this is impossible.

Lambdaj stores data in a WeakHashMap<Object, Argument<?>>, where key is what return things like on(SomeCLass.classs).getX(). WeakHashMap compares objects using .hashCode() and .equals().

alkedr
  • 754
  • 6
  • 17