dominic
dominic

Reputation: 143

Check if Field is from specific class

To display keys & values of a dataobject, I'm using a Collection of AccessibleObjects to generate a table. The AccessibleObject'S are collected on a specific time, but the values are read, when the render have to render the table.

The Problem: I'm not only want to hold AccessibleObject's of one specific Class. Is it possible to check a AccessibleObject Class-Origin? e.g. accessibleObject.fromClass(classType);

Upvotes: 0

Views: 88

Answers (2)

Peter Lawrey
Peter Lawrey

Reputation: 533870

Do you mean

Member member = field or method;
Class clazz = member.getDeclaringClass()

to get the class the field appears in.

Note: this is the actual class, not the class you might have used to look it up. e.g. say A has a field x and a subclass B. If you get field x of class B, it will say the declaring class is A. This is because A and B can have a field called x.

Upvotes: 2

Narendra Pathai
Narendra Pathai

Reputation: 42005

Class c = field.getDeclaringClass();

From JavaDoc:

Returns the Class object representing the class or interface that declares the field represented by this Field object.

Upvotes: 0

Related Questions