Reputation: 1076
Is it possible to find a object instance using a public static method which expects a field object as parameter?
Thats what I search:
public class Foo {
private Bar myObject;
public static Foo get(Bar bar) {
// return an instance of Foo which has stored a
// reference to bar inside myObject or null if not found
}
}
I hope, you understand my concern. Google did not help, I just guess that there could be a solution using Java Reflection. Thanks a lot for help.
Upvotes: 1
Views: 148
Reputation: 61158
Not sure this is to do with reflection, you just need to keep track of you Foo
s, so you need keep a static map of all Foos created.
public class Foo {
private static final Map<Bar, Foo> BAR_FOO_MAP = new HashMap<>();
private final Bar bar;
private Foo(final Bar bar) {
this.bar = bar;
}
public static Foo newInstance(final Bar bar) {
final Foo foo = new Foo(bar);
BAR_FOO_MAP.put(bar, foo);
return foo;
}
public static Foo findFoo(final Bar bar) {
return BAR_FOO_MAP.get(bar);
}
}
As pointed out by others, unless you have unqiue mappings then this won't work - you may have to use a Map<Bar, Collection<Foo>>
or even a guava Multimap
.
Upvotes: 0
Reputation: 79
No I don't think, but if you have a list of known objects (for example using a static list and adding every new istance), you can create a search metod that compare bar to the fiel object.. or you can simply create new instance in this method :)
Upvotes: 0
Reputation: 72294
Not easily, and my gut reaction is unless there's a very special case, you're probably approaching the problem the wrong way.
If you really do need to do this, you could use instrumentation to register the constructors of the classes that you're interested in which might reference Foo, and then keep a reference to the created objects somewhere.
You could then step through all these references when you like, and use reflection to check if any contain the field type that you're after.
It's not pretty, not efficient, and in most cases would be completely impractical, but purely answering the question from a technical viewpoint that's the only way I can think to do it.
Upvotes: 0
Reputation: 1500775
No, there's no way of knowing this.
A given Bar
object could be referred to by any number of instances of Foo
, or indeed none. Unless you explicitly have a reference back from Bar
to Foo
, there's no way of finding them.
(Of course, if you know about all the Foo
instances in the system, you could search that way...)
Upvotes: 2
Reputation: 1074455
No, you can't backtrack a field that way. The bar
reference that the get
method receives is a copy of the reference passed in. There could be dozens of references to that same Bar
object, or none. If you had a list of all of the Foo
objects in the system, you could find out (via reflection, since myObject
is private) which Foo
objects had that same reference, but it could easily be that there was more than one, or none, as there's no way to know that the bar
you received is referenced by any Foo
instance, nor is there any reason it could only be referenced by one of them.
Upvotes: 0