Java 如何抑制FindBugs警告(对绝对路径名的硬编码引用)?
我正在为开源软件Geonetwork测试一些东西,现在我想将一个形状文件从我的postGIS数据库导出到我的计算机,但我无法测试它,因为我总是收到findbugs警告: 对org.fao.geonet.services.resources.Download.exec(元素,ServiceContext)中绝对路径名的硬编码引用 有人知道如何抑制或避免此警告吗?或者另一个解决方案如何测试导出是否有效 代码如下:Java 如何抑制FindBugs警告(对绝对路径名的硬编码引用)?,java,findbugs,absolute-path,suppress-warnings,hardcoded,Java,Findbugs,Absolute Path,Suppress Warnings,Hardcoded,我正在为开源软件Geonetwork测试一些东西,现在我想将一个形状文件从我的postGIS数据库导出到我的计算机,但我无法测试它,因为我总是收到findbugs警告: 对org.fao.geonet.services.resources.Download.exec(元素,ServiceContext)中绝对路径名的硬编码引用 有人知道如何抑制或避免此警告吗?或者另一个解决方案如何测试导出是否有效 代码如下: ` Map parameter1= new HashMap(); para
` 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注释之后
我希望有人能帮我解决这个问题。
谢谢大家! 您可以使用筛选器文件来描述要忽略的bug
制作一个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);
通过这种方式,您可以将硬编码文件的配置放在可配置属性文件中。您如何使用findbugs?(即直接调用、使用maven report等)?findbugs警告如何阻止您运行代码?我在findbugs_.xml中这样做过:谢谢您的回答!你是说bundle.getString而不是bundle.get吗?@PatrickGiersch当然,我会纠正的。我现在试过了,但出了点问题!?Exc:java.util.MissingResourceException:找不到基名称shpprop、locale de_de------我的代码:ResourceBundle bundle=ResourceBundle.getBundle(“shpprop”);String danoshpath=bundle.getString(“danoShp”);文件sfile=新文件(danoShpPath);----------使用与我的Java文件位于同一个包中的shpprop.properties:------danoShp=C:/Users/Tinis/Downloads/dano.shp,我做错了什么?getBundle中的路径是绝对路径,需要包名作为路径。和一个文件
“shpprop.properties”
。也区分大小写getBundle(“org.sioux.myapp.shprop”)
虽然/
通常用于.properties。谢谢,现在就开始工作。不能放弃你的投票权,因为你需要15个声誉。无论如何,非常感谢!:-)