在JAXB的proguard优化之后的空字段

时间:2015-11-14 16:35:46

标签: java xml jaxb proguard

我正在尝试使用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);
            }
        }
    }

}

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

在混淆之前,输出是预期的。混淆后,我没有任何异常,但预期的数据字段为空。例如,输出

  

文件:/ Users / paul / Documents / sample data / ZZ Non-2D   formats / canSAS / cs_collagen.xml SASentry元素:1 SASentry   标题:干鸡胶原蛋白,d = 673 A,6531 eV,X6B

     

运行:1运行@ name:运行:Sep 19 1994 01:41:02 am

     

SASdata:1个SASdata @ name:

     

分:125

混淆后变为:

  

文件:/ Users / paul / Documents / sample data / ZZ Non-2D   formats / canSAS / cs_collagen.xml SASentry元素:1 SASentry   标题:null

     

运行:0

     

SASdata:0

请注意,遵循此列表中的其他建议,我确实在配置文件中包含了Signature和* Annotation *。这可以防止我之前遇到的类型转换异常,但仍未正确读取xml文件。任何建议表示赞赏!

1 个答案:

答案 0 :(得分:0)

.ProjectTitle {
    background-color: green;
    position: absolute;
    width: 100%;
    height: 20%;
    bottom: 0;
}

.flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: "Taviraj", serif;
    color: #000;
    letter-spacing: 0.11em;
    line-height: 20%;
    height: 100%;
}

.flex-container p {
    color: #ffffff;
    vertical-align: middle;
}

这应该有所帮助。我想你已经找到了解决方案。最好的祝福。拉尔夫。