简体   繁体   English

Proguard优化JAXB之后的空白字段

[英]Empty fields after proguard optimization of JAXB

I am trying to use proguard to obfuscate a Java application that uses JAXB. 我正在尝试使用proguard混淆使用JAXB的Java应用程序。 The code (which uses a publicly available library) is 该代码(使用公共库)是

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);
            }
        }
    }

}

The proguard config file is Proguard配置文件是

-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

Before obfuscating, the output is as expected. 混淆之前,输出是预期的。 After obfuscating, I don't get any exceptions, but expected data fields are null. 混淆后,我没有任何异常,但是期望的数据字段为null。 For example, the output 例如,输出

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 文件:/ Users / paul / Documents / sample data / ZZ非2D格式/canSAS/cs_collagen.xml SASentry元素:1个SASentry标题:干燥的雏鸡胶原蛋白,d = 673 A,6531 eV,X6B

Runs: 1 Run@name: Run: Sep 19 1994 01:41:02 am 运行:1运行@名称:运行:1994年9月19日上午01:41:02

SASdata: 1 SASdata@name: SASdata:1个SASdata @ name:

points: 125 积分:125

After obfuscation becomes: 混淆后变为:

File: /Users/paul/Documents/sample data/ZZ Non-2D formats/canSAS/cs_collagen.xml SASentry elements: 1 SASentry Title: null 文件:/ Users / paul / Documents / sample data / ZZ非2D格式/canSAS/cs_collagen.xml SASentry元素:1个SASentry标题:null

Runs: 0 运行次数:0

SASdata: 0 SAS数据:0

Note that, following other suggestions on this list, I did include both Signature and *Annotation* in the config file. 请注意,按照此列表上的其他建议,我确实在配置文件中同时包含了签名和* Annotation *。 That prevents the type cast exceptions I was getting before, but the xml file is still not being read in properly. 这可以防止我之前遇到的类型转换异常,但是xml文件仍然无法正确读取。 Any suggestions appreciated! 任何建议表示赞赏!

-keep public class your.package.path.to.jaxb.classes.** {
  public protected private *;
}

This should help. 这应该有所帮助。 I guess you've already found your solution. 我想您已经找到了解决方案。 Best regards. 最好的祝福。 Ralph. 拉尔夫

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM