我正在尝试使用 proguard 来混淆使用 JAXB 的 Java 应用程序。代码(使用公开可用的库)是
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.cansas.cansas1d.SASdataType;
import org.cansas.cansas1d.SASentryType;
import org.cansas.cansas1d.SASentryType.Run;
import org.cansas.cansas1d.SASrootType;
public class CansasReader {
private static final String RES_DIR = "/Users/paul/Documents/sample data/ZZ Non-2D formats/canSAS/";
private static final String JAXB_CONTEXT = "org.cansas.cansas1d";
private JAXBContext jc;
private JAXBElement<SASrootType> xmlJavaData;
/**
* Open a cansas1d 1D file
*
* @param xmlFile
* @return SasRootType object (saves a second method call)
* @throws JAXBException
* @throws FileNotFoundException
*/
@SuppressWarnings( "unchecked" )
public SASrootType loadXML(String xmlFile) throws JAXBException, FileNotFoundException {
jc = JAXBContext.newInstance(JAXB_CONTEXT); // reference the namespace:
Unmarshaller unmarshaller = jc.createUnmarshaller();
InputStream in = new FileInputStream(new File(xmlFile));
xmlJavaData = (JAXBElement<SASrootType>) unmarshaller.unmarshal(in);
return xmlJavaData.getValue();
}
/**
* Describe the XML data in more detail than toString() method
* and print to stdout.
*/
public void full_report(SASrootType srt) {
for ( SASentryType entry : srt.getSASentry() ) {
System.out.println("SASentry");
System.out.printf("Title:\t%s\n", entry.getTitle());
List<SASentryType.Run> runs = entry.getRun();
System.out.printf("#Runs:\t%d\n", runs.size());
for ( Run run : runs ) {
System.out.printf("Run@name:\t%s\n", run.getName());
System.out.printf("Run:\t%s\n", run.getValue());
}
List<SASdataType> datasets = entry.getSASdata();
System.out.printf("#SASdata:\t%d\n", entry.getSASdata().size());
for ( SASdataType sdt : datasets ) {
System.out.printf("SASdata@name:\t%s\n", sdt.getName());
System.out.printf("#points:\t%d\n", sdt.getIdata().size());
}
System.out.println();
}
}
/**
* simple representation of data in memory
*/
public String toString(SASrootType sasRoot) {
return "SASentry elements: " + sasRoot.getSASentry().size();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("class: " + CansasReader.class.getCanonicalName());
String[] fileList = {
RES_DIR + "cs_collagen.xml",
RES_DIR + "1998spheres.xml",
"cannot_find_this.xml"
};
for (String xmlFile : fileList) {
System.out.println("\n\nFile: " + xmlFile);
try {
CansasReader rdr = new CansasReader();
SASrootType srt = rdr.loadXML(xmlFile);
System.out.println(rdr.toString(srt));
rdr.full_report(srt);
System.out.println("the end.");
} catch (FileNotFoundException e) {
System.out.println("File not found:" + xmlFile);
} catch (JAXBException e) {
System.out.println("Could not open (unmarshall) XML file" + xmlFile);
}
}
}
}
混淆器配置文件是
-injars CansasReader.jar
-outjars CansasReader_ob.jar
-libraryjars <java.home>/lib/rt.jar
-printmapping CansasReader.map
-renamesourcefileattribute SourceFile
-keepattributes SourceFile,LineNumberTable, *Annotation*, EnclosingMethod, Signature
-keep public class * {
public protected *;
}
-repackageclasses ''
-overloadaggressively
-defaultpackage ''
-allowaccessmodification
-keeppackagenames org.cansas.cansas1d
-keepparameternames
-keep,includedescriptorclasses public class org.cansas.cansas1d.package-info
-keep,includedescriptorclasses public class org.cansas.cansas1d.FloatUnitType
(and the same for every other class in the package)
-keep,includedescriptorclasses public class CansasReader
混淆之前,输出符合预期。混淆后,我没有得到任何异常,但预期的数据字段为空。例如,输出
File: /Users/paul/Documents/sample data/ZZ Non-2D formats/canSAS/cs_collagen.xml SASentry elements: 1 SASentry Title: dry chick collagen, d = 673 A, 6531 eV, X6B
Runs: 1 Run@name: Run: Sep 19 1994 01:41:02 am
SASdata: 1 SASdata@name:
points: 125
混淆后变为:
File: /Users/paul/Documents/sample data/ZZ Non-2D formats/canSAS/cs_collagen.xml SASentry elements: 1 SASentry Title: null
Runs: 0
SASdata: 0
请注意,根据此列表中的其他建议,我确实在配置文件中包含了签名和*注释*。这可以防止我之前遇到的类型转换异常,但 xml 文件仍然无法正确读取。任何建议表示赞赏!
最佳答案
-keep public class your.package.path.to.jaxb.classes.** {
public protected private *;
}
这应该有帮助。我想您已经找到了解决方案。此致。拉尔夫。
关于java - JAXB proguard 优化后的空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710698/