简体   繁体   English

从子类中的重写方法访问超类方法中的局部变量

[英]Access a local variable in the superclass' method from the overridden method in the subclass

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? 在Java中,是否可以通过反射或其他方式从子类中对应的重写方法访问超类的方法中声明的局部变量?

Specifically, I am working with Spring Security's DefaultLdapAuthoritiesPopulator . 具体来说,我正在使用Spring Security的DefaultLdapAuthoritiesPopulator This class has a method named getAdditionalRoles which the documentation says subclasses can override to return extra roles for the user. 此类具有一个名为getAdditionalRoles的方法,该文档称该方法可以重写子类以为用户返回额外的角色。

The class also implements the getGrantedAuthorities method, which actually calls the getAdditionalRoles method. 该类还实现了getGrantedAuthorities方法,该方法实际上调用了getAdditionalRoles方法。 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 . 此方法调用getGroupMembershipRoles ,该方法getGroupMembershipRoles为此用户定义的组进行LDAP搜索,并将其存储在名为roles的本地变量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. 现在,在实现getAdditionalRoles ,我还需要访问为此用户在LDAP中定义的组,因此可以推断出该用户的其他角色。 For business reasons, I cannot define these additional roles directly in LDAP. 出于业务原因,我无法直接在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. 我可以自己继续实施LdapAuthoritiesPopulator ,但我想知道是否还有其他方法,因为我真正需要的是访问父类方法中的roles局部变量,以避免我不得不进行第二次LDAP搜索。

  1. you cann't access variables in other method, for variables in method is removed after the method is returned. 您无法访问其他方法中的变量,因为方法返回后,方法中的变量将被删除。 because variables is in stack. 因为变量在堆栈中。
  2. if possible, you can override getGroupMembershipRoles and get groups stored as property, and access it at other method. 如果可能,您可以覆盖getGroupMembershipRoles并获取存储为属性的groups ,并以其他方法访问它。

Possibly you could leverage AOP and put After Retuning Advice on getGroupMembershipRoles(userDn, username); 可能您可以利用AOP并将“调整建议后”放到getGroupMembershipRoles(userDn, username); and modify the returned Roles. 并修改返回的角色。

I took Zutty's advice and implemented it this way: 我接受了Zutty的建议并以这种方式实施了它:

@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. 我不认为您可以通过任何其他远程类中存在的任何方法(谁的value方法不返回)从局部变量访问数据。 Even reflections wont help in this case. 在这种情况下,即使反思也无济于事。 Please correct me if I am wrong or missed anything. 如果我错了或错过了什么,请纠正我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM