Java中Enums的静态访问?

问题描述

我有两个枚举AttributesPropertyResourceAttributesMapping互相引用,如下所示:

import org.apache.commons.collections.map.HashedMap;
    
    import test.xsd.Attribute;
    
    public enum AttributesProperty {
        
        ACP_RESOURCETYPE(ResourceAttributesMapping.ACCESSCONTROLPOLICY,"resourceType"),ACP_RESOURCEID(ResourceAttributesMapping.ACCESSCONTROLPOLICY,"resourceID"),.............
    
    private ResourceAttributesMapping resource;
        private String attributeName;
    
        private AttributesProperty(ResourceAttributesMapping resource,String attributeName)
        {
            this.resource = resource;
            this.attributeName = attributeName;
        }
    
    public static Map<ResourceAttributesMapping,List<Attribute>> getResTypeMapForAttrName(List<Attribute> attributesList) {
    
        Map<ResourceAttributesMapping,List<Attribute>> attributeMap = new HashedMap();
        
        List<Attribute> attrList = null;
        List<ResourceAttributesMapping> resTypes = new ArrayList<ResourceAttributesMapping>();
        for (Attribute attr : attributesList) {
            boolean flag = false;
            for (AttributesProperty attrProp : AttributesProperty.values()) {
                if (attr.getName().equals(attrProp.getAttributeName())) {
                    
                        if (attributeMap.get(attrProp.getResourceType()) == null) {
                            
                         .........// Some logic
    }
    
    }

引用的枚举:ResourceAttributesMapping.java

从静态导入AttributesProperty枚举的导入中可以看到该枚举。

import static xsd.enums.AttributesProperty.*;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;


public enum ResourceAttributesMapping
{
    
    ACCESSCONTROLPOLICY(1,Arrays.asList(ACP_RESOURCETYPE,ACP_RESOURCEID,............)),AE(2,Arrays.asList(AE_RESOURCETYPE,AE_RESOURCEID,...........)),....

private int resourceType;
    private List<AttributesProperty> attrList;

    private ResourceAttributesMapping(int resourceType,List<AttributesProperty> attrList) {
        this.resourceType = resourceType;
        this.attrList = attrList;
    }

    public int getM2MResourceTypes() {
        return resourceType;
    }
    
    public  List<AttributesProperty> getAttributeList(){
        return attrList;
    }

......// accessor methods
}

此代码已在JDK 8,WildFly 15上成功运行,但是在迁移到JDK 11 WildFly 20之后,我在NullPointerException的枚举访问器方法中得到了AttributesProperty

public static Map<ResourceAttributesMapping,List<Attribute>> getResTypeMapForAttrName(List<Attribute> attributesList) {

    Map<ResourceAttributesMapping,List<Attribute>> attributeMap = new HashedMap();
    
    List<Attribute> attrList = null;
    List<ResourceAttributesMapping> resTypes = new ArrayList<ResourceAttributesMapping>();
    for (Attribute attr : attributesList) {
        boolean flag = false;
        for (AttributesProperty attrProp : AttributesProperty.values()) {
            if (attr.getName().equals(attrProp.getAttributeName())) {
                
                    if (attributeMap.get(attrProp.getResourceType()) == null) {
                        
                     .........// Some logic

attrProp.getResourceType()在给null

测试场景如下:在第一次运行枚举中的accesor方法的迭代中,它成功运行,但是在第二次迭代中,我得到了NullPointerException

从JDK 8到11的枚举访问是否有所变化?

或者导致问题的第二枚举的静态访问。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...