如何抑制FindBugs警告(硬编码的绝对路径引用)?

8
我正在测试开源软件Geonetwork的一些功能,现在我想从我的postGIS数据库将一个shape文件导出到我的电脑上,但我无法进行测试,因为我总是收到一个findbugs警告: 在org.fao.geonet.services.resources.Download.exec(Element, ServiceContext)中存在对绝对路径名的硬编码引用。
有没有人知道如何消除或避免这个警告?或者是否有其他解决方案来测试导出是否有效?
以下是代码:
  ` Map parameter1= new HashMap();
    parameter1.put("dbtype", "postgis");        
    parameter1.put("host", "localhost");        
    parameter1.put("port", "5433");  
    parameter1.put("database", "geonetwork");
    parameter1.put("schema", "mydata");
    parameter1.put("user", "postgres");        
    parameter1.put("passwd", "dominik1");
    parameter1.put("Expose primary keys", false);
    parameter1.put(PostgisNGDataStoreFactory.VALIDATECONN, true);
    parameter1.put(PostgisNGDataStoreFactory.MAX_OPEN_PREPARED_STATEMENTS, 100);
    parameter1.put(PostgisNGDataStoreFactory.LOOSEBBOX, true);
    parameter1.put(PostgisNGDataStoreFactory.PREPARED_STATEMENTS, true); 
    System.out.println("parameter1: " + parameter1);

    DataStore pds = new PostgisNGDataStoreFactory().createDataStore(parameter1);
    FeatureSource fs = pds.getFeatureSource("dano");

    SimpleFeatureCollection fc = (SimpleFeatureCollection) fs.getFeatures();
    SimpleFeature f = (SimpleFeature) fc.toArray()[0];

    // create shapefile

    File sfile = new File("C:/Users/Tinis/Downloads/dano.zip");            
    parameter1 = new HashMap();
    parameter1.put("url", sfile.toURI().toURL());
    parameter1.put("create spatial index", Boolean.FALSE);


    DirectoryDataStore dds = new DirectoryDataStore(new File(sfile.getAbsolutePath()), (FileStoreFactory) new ShapefileDataStoreFactory.ShpFileStoreFactory(new ShapefileDataStoreFactory(), parameter1));    

    dds.createSchema(fc.getSchema());
    String typeName = dds.getTypeNames()[0];
    SimpleFeatureSource featureSource = dds.getFeatureSource(typeName);
    SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

    // write data to shapefile
    Transaction t = new DefaultTransaction("addTransaction");
    featureStore.setTransaction(t);
    featureStore.addFeatures(fc);
    t.commit();
    t.close(); `

在“// 创建shapefile”注释后面的行会出现FindBugs警告。

希望有人能帮我解决这个问题。 谢谢!


你是如何使用FindBugs的?(例如直接调用,使用Maven报告等)? - Brett Okken
一个FindBugs警告如何阻止你运行代码? - Joeri Hendrickx
2个回答

8
创建一个 xxx.properties 文件(ResourceBundle),并添加以下内容:
danoZip = C:/Users/Tinis/Downloads/dano.zip

并且以间接的方式实现:

ResourceBundle bundle = ResourceBundle.getBundle("xxx");

String danoZipPath = bundle.getString("danoZip");

File sfile = new File(danoZipPath);            

通过将硬编码文件的配置放置在可配置的属性文件中,您可以实现此目的。

@PatrickGiersch 当然,我会纠正。 - Joop Eggen
getBundle中的路径是绝对路径,需要将包名作为路径。还有一个文件“shpprop.properties”。大小写敏感。虽然“/”经常用于.properties,但应使用getBundle(“org.sioux.myapp.shprop”)。 - Joop Eggen
1
谢谢,现在可以了。因为需要15个声望值,所以无法给你点赞。不管怎样,非常感谢! :-) - Patrick Giersch
只是一个打字错误:应该是 ResourceBundle.getBundle("xxx"); - aProgger
@aProgger 谢谢,已经更正。 - Joop Eggen
显示剩余2条评论

1

我在findbugs_excludes.xml中这样做了:<Match> <Class name=""org.fao.geonet.services.resources.Download" /> </Match> 然后我在Eclipse中运行包,选择"Maven Install" 在安装过程中,我收到了同样的错误,并且在此之后我所做的更改在findbugs_excludes.xml中被撤销了。 奇怪,为什么会发生这种情况? - Patrick Giersch

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接