Proguard Maven使用Lambda表达式屏蔽错误

时间:2017-01-20 10:31:47

标签: java maven lambda proguard

我使用Proguard和Maven来掩盖我的Java代码。

我将所有函数从匿名声明更改为Lambda expression0但在编译和运行后我得到错误

2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] - Exception in thread "AWT-EventQueue-0" 
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] - java.lang.BootstrapMethodError: call site initialization exception
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.lang.invoke.CallSite.makeSite(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.lang.invoke.MethodHandleNatives.linkCallSite(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at com.goodsoft.stockbox.chart.f.c.h.a(ChartWindowPanel.java:50208)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at com.goodsoft.stockbox.commons.f.h.a.b.n(BaseWindowController.java:269)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at com.goodsoft.stockbox.commons.f.h.a.b.a(BaseWindowController.java:30)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at com.goodsoft.stockbox.commons.f.h.a.b$1.m(BaseWindowController.java:164)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at com.goodsoft.stockbox.commons.b.c.d$3.c(InstrumentController.java:324)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.event.InvocationEvent.dispatch(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventQueue.access$500(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventQueue$3.run(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventQueue$3.run(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.security.AccessController.doPrivileged(Native Method)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventQueue.dispatchEvent(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
2017-sty-20 08:53:46,289 ERROR [AWT-EventQueue-0] -     at java.awt.EventDispatchThread.run(Unknown Source)
2017-sty-20 08:53:46,305 ERROR [AWT-EventQueue-0] - Caused by: java.lang.invoke.LambdaConversionException: Type mismatch for lambda argument 1: class com.goodsoft.stockbox.commons.model.c is not convertible to int
2017-sty-20 08:53:46,305 ERROR [AWT-EventQueue-0] -     at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(Unknown Source)

有趣的错误日志是:

Caused by: java.lang.invoke.LambdaConversionException: Type mismatch for lambda argument 1: class com.goodsoft.stockbox.commons.model.c is not convertible to int

...所以也许Lambda没有问题,但使用lambda的掩码对象?

方法 BEFORE 更改为lambda看起来像(工作正常):

bidAskDiffChangeObserver = new BidAskDiffChangeObserver(instrument,
    new IDiffValueChangeDelegate() {
        @Override
        public void valueChanged(int newValue, ValueChangeType type) {
            mainRangeAxis.updateSpread(newValue);
        }
    });

...和 AFTER 更改为Lambda表达式(不起作用):

bidAskDiffChangeObserver = new BidAskDiffChangeObserver(instrument,
                    (newValue, type) -> mainRangeAxis.updateSpread(newValue));

它被称为:

int newValue = computeBidAskDifference();
ValueChangeType type = previousValue > newValue ? ValueChangeType.DROP :
        previousValue < newValue ? ValueChangeType.GROWTH : ValueChangeType.EQUAL;

if (delegate != null)
    delegate.valueChanged(newValue, type);

我应该使用哪个选项来添加lambda条件?

没有屏蔽,一切正常(使用选项-dontoptimize)。

我在pom.xml中的proguard选项现在看起来像这样:

<plugin>
    <groupId>com.github.wvengen</groupId>
    <artifactId>proguard-maven-plugin</artifactId>
    <version>2.0.10</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>proguard</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <injar>StockBox.jar</injar>
        <outjar>StockBox.jar</outjar>
        <options>
            <option>-allowaccessmodification</option>
            <option>-keepdirectories</option>
            <option>-keep public class com.goodsoft.stockbox.Main { *; }</option>
            <option>-keep public class * implements
                com.goodsoft.stockbox.base.model.config.IConvertibleConfiguration
                { *;}
            </option>
            <option>-dontskipnonpubliclibraryclasses</option>
            <option>-dontskipnonpubliclibraryclassmembers</option>
            <option>-keepattributes RuntimeVisibleAnnotations</option>
            <option>-keep @javax.persistence.* class * { *;}</option>
            <option>-keepclassmembers enum * {
                public static **[] values();
                public static ** valueOf(java.lang.String);
                }
            </option>
            <option>-keep @org.springframework.context.annotation.Configuration class *</option>
            <option>-keep @org.springframework.beans.factory.annotation.Service class *</option>
            <option>-keep @org.springframework.stereotype.Component class *</option>
            <option>-keepclassmembers class * {
                @org.springframework.beans.factory.annotation.Autowired *;
                @org.springframework.beans.factory.annotation.Value *;
                @org.springframework.context.annotation.Bean *;
                }
            </option>
            <option>-adaptresourcefilecontents **.properties</option>
            <option>-keepattributes
                Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
            </option>
        </options>
        <maxMemory>512m</maxMemory>
        <libs>
            <lib>${java.home}/lib/rt.jar</lib>
            <lib>${java.home}/lib/jce.jar</lib>
        </libs>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>net.sf.proguard</groupId>
            <artifactId>proguard-base</artifactId>
            <version>${proguard.version}</version>
        </dependency>
    </dependencies>
</plugin>

修改

我尝试将maven选项添加为-optimizations !method/removal/parameter-optimizations !method/marking/static,但没有帮助。

1 个答案:

答案 0 :(得分:1)

<强>解决

优化将枚举更改为整数存在问题。

只需关闭此优化即可。

<option>-optimizations !class/unboxing/enum</option>