【问题标题】:How to suppress FindBugs warning (hard coded reference to an absolute pathname)? [duplicate]如何抑制 FindBugs 警告(对绝对路径名的硬编码引用)? [复制]
【发布时间】:2014-10-13 19:22:03
【问题描述】:

我只是在为开源软件 Geonetwork 测试一些东西,现在我想将一个形状文件从我的 postGIS 数据库导出到我的计算机,但我无法测试它,因为我总是收到一个 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(); `

给出 findbugs 警告的行就在 // create shapefile 注释之后

我希望有人可以帮助我解决这个问题。 谢谢!

【问题讨论】:

  • 你是如何使用 findbugs 的? (即直接调用,使用maven报告等)?
  • findbugs 警告如何阻止您运行代码?

标签: java findbugs absolute-path suppress-warnings hardcoded


【解决方案1】:

您可以使用过滤器文件来描述要忽略的错误。

http://findbugs.sourceforge.net/manual/filter.html

【讨论】:

  • 我已经在 findbugs_excludes.xml 中这样做了: 然后我运行Eclipse 中的包为“Maven 安装” 安装时出现同样的错误,之后我在 findbugs_excludes.xml 中所做的更改被撤消。奇怪,为什么会发生这种情况?
【解决方案2】:

制作一个带有条目的 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);            

通过这种方式,您可以将硬编码文件的配置放入可配置的属性文件中。

【讨论】:

  • 感谢您的回答!你的意思是 bundle.getString 而不是 bundle.get?
  • @PatrickGiersch 当然,我会更正。
  • 我现在试过了,但出了点问题!? Exc : java.util.MissingResourceException: Can't find bundle for base name shpprop, locale de_DE ------- 我的代码: ResourceBundle bundle = ResourceBundle.getBundle("shpprop");字符串 danoShpPath = bundle.getString("dan​​oShp");文件 sfile = 新文件(danoShpPath); -------- shpprop.properties 与我的 Java 文件位于同一个包中: ------ danoShp = C:/Users/Tinis/Downloads/dano.shp 我做错了什么?跨度>
  • getBundle 中的路径是绝对路径,需要包名作为路径。还有一个文件"shpprop.properties"。也区分大小写。 getBundle("org.sioux.myapp.shprop") 虽然 / 经常用于 .properties。
  • 谢谢你,现在工作。不能给你投票,因为需要 15 声望。总之非常感谢! :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 2013-01-08
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多