首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JAXB程序优化后的空域

JAXB程序优化后的空域
EN

Stack Overflow用户
提问于 2015-11-14 16:35:45
回答 1查看 774关注 0票数 1

我正在尝试使用proguard来混淆使用JAXB的Java应用程序。代码(它使用公共可用的库)是

代码语言:javascript
复制
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);
            }
        }
    }

}

前保护配置文件是

代码语言:javascript
复制
-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

在混淆之前,输出与预期的相同。在混淆之后,我不会得到任何异常,但是预期的数据字段为null。例如,输出

文件:/ SASentry /paul/ SASentry /sample/ZZ非2D格式/canSAS/cs_coragen.xml SASentry元素:1 SASentry标题:干鸡胶原蛋白,d= 673 A,6531 eV,X6B Run: 1 Run@name: Run:1994年9月19日01:41:02上午 SASdata: 1 SASdata@name: 点数: 125

混淆后变成:

文件:/ SASentry /paul/ SASentry /sample data/ZZ非2D格式/canSAS/cs_coragen.xml SASentry元素:1 SASentry标题: null 运行:0 SASdata: 0

注意,按照这个列表中的其他建议,我在配置文件中同时包含了签名和*注释*。这防止了我之前获得的类型强制转换异常,但是xml文件仍然没有被正确读取。如有任何建议请见谅!

EN

回答 1

Stack Overflow用户

发布于 2016-08-23 21:59:01

代码语言:javascript
复制
-keep public class your.package.path.to.jaxb.classes.** {
  public protected private *;
}

这会有帮助的。我想你已经找到解决办法了。诚挚的问候。拉尔夫。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33710698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档