Is there a way in Java, either via reflection or other means, to access a local variable declared in a superclass' method from its corresponding overridden method in the subclass?
Specifically, I am working with Spring Security's DefaultLdapAuthoritiesPopulator
. This class has a method named getAdditionalRoles
which the documentation says subclasses can override to return extra roles for the user.
The class also implements the getGrantedAuthorities
method, which actually calls the getAdditionalRoles
method. The source code looks something like this:
public final GrantedAuthority[] getGrantedAuthorities(DirContextOperations user, String username) {
...
Set roles = getGroupMembershipRoles(userDn, username);
Set extraRoles = getAdditionalRoles(user, username);
...
}
This method calls getGroupMembershipRoles
, which does an LDAP search for groups defined for this user and stores it in a local variable named roles
. Now in my implementation of getAdditionalRoles
, I also need access to the groups defined for this user in LDAP, so I can infer additional roles for this user. For business reasons, I cannot define these additional roles directly in LDAP.
I could simply go ahead and implement LdapAuthoritiesPopulator
myself, but I was wondering if there was some other way around, as all I really need is access to the roles
local variable in the parent class' method to avoid me having to do a second LDAP search.
getGroupMembershipRoles
and get groups
stored as property, and access it at other method. Possibly you could leverage AOP and put After Retuning Advice on getGroupMembershipRoles(userDn, username);
and modify the returned Roles.
I took Zutty's advice and implemented it this way:
@Override
public Set<GrantedAuthority> getGroupMembershipRoles(String userDn,
String username) {
Set<GrantedAuthority> authorities = super.getGroupMembershipRoles(userDn, username);
// My app's logic by inspecting the authorities Set
return authorities;
}
I dont think you can access data from local variables from any method(who's value method does not return) which is present in some other remote class. Even reflections wont help in this case. Please correct me if I am wrong or missed anything.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.