Có ai từng cố gắng kết hợp việc sử dụng google guice với obfuscation (đặc biệt proguard)? Phiên bản bị xáo trộn của mã của tôi không hoạt động với google guice vì guice phàn nàn về các thông số loại bị thiếu. Thông tin này dường như bị xóa bởi bước chuyển đổi mà proguard làm, ngay cả khi các lớp học liên quan được loại trừ khỏi obfuscation.Tiêm với google guice không hoạt động nữa sau khi obfuscation với proguard
Các vết đống trông như thế này:
com.google.inject.CreationException: Guice creation errors:
1) Cannot inject a Provider that has no type parameter
while locating com.google.inject.Provider
for parameter 0 at de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel.setPasswordPanelProvider(SourceFile:499)
at de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel.setPasswordPanelProvider(SourceFile:499)
while locating de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel
for parameter 0 at de.repower.lvs.client.admin.user.administration.b.k.setParentPanel(SourceFile:65)
at de.repower.lvs.client.admin.user.administration.b.k.setParentPanel(SourceFile:65)
at de.repower.lvs.client.admin.user.administration.o.a(SourceFile:38)
2) Cannot inject a Provider that has no type parameter
while locating com.google.inject.Provider
for parameter 0 at de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel.setWindTurbineAccessGroupProvider(SourceFile:509)
at de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel.setWindTurbineAccessGroupProvider(SourceFile:509)
while locating de.repower.lvs.client.admin.user.administration.AdminUserCommonPanel
for parameter 0 at de.repower.lvs.client.admin.user.administration.b.k.setParentPanel(SourceFile:65)
at de.repower.lvs.client.admin.user.administration.b.k.setParentPanel(SourceFile:65)
at de.repower.lvs.client.admin.user.administration.o.a(SourceFile:38)
2 errors
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:354)
at com.google.inject.InjectorBuilder.initializeStatically(InjectorBuilder.java:152)
at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:105)
at com.google.inject.Guice.createInjector(Guice.java:92)
at com.google.inject.Guice.createInjector(Guice.java:69)
at com.google.inject.Guice.createInjector(Guice.java:59)
tôi đã cố gắng để tạo ra một ví dụ nhỏ (không sử dụng Guice) mà dường như để tạo lại vấn đề:
package de.repower.common;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
class SomeClass<S> {
}
public class ParameterizedTypeTest {
public void someMethod(SomeClass<Integer> param) {
System.out.println("value: " + param);
System.setProperty("my.dummmy.property", "hallo");
}
private static void checkParameterizedMethod(ParameterizedTypeTest testObject) {
System.out.println("checking parameterized method ...");
Method[] methods = testObject.getClass().getMethods();
for (Method method : methods) {
if (method.getName().equals("someMethod")) {
System.out.println("Found method " + method.getName());
Type[] types = method.getGenericParameterTypes();
Type parameterType = types[0];
if (parameterType instanceof ParameterizedType) {
Type parameterizedType = ((ParameterizedType) parameterType).getActualTypeArguments()[0];
System.out.println("Parameter: " + parameterizedType);
System.out.println("Class: " + ((Class) parameterizedType).getName());
} else {
System.out.println("Failed: type ist not instance of ParameterizedType");
}
}
}
}
public static void main(String[] args) {
System.out.println("Starting ...");
try {
ParameterizedTypeTest someInstance = new ParameterizedTypeTest();
checkParameterizedMethod(someInstance);
} catch (SecurityException e) {
e.printStackTrace();
}
}
}
Nếu bạn chạy mã này unsbfuscated , đầu ra trông giống như sau:
Starting ...
checking parameterized method ...
Found method someMethod
Parameter: class java.lang.Integer
Class: java.lang.Integer
Nhưng chạy phiên bản bị xáo trộn với pro sản lượng bảo vệ:
Starting ...
checking parameterized method ...
Found method someMethod
Failed: type ist not instance of ParameterizedType
Đây là những lựa chọn tôi sử dụng cho obfuscation:
-injars classes_eclipse\methodTest.jar
-outjars classes_eclipse\methodTestObfuscated.jar
-libraryjars 'C:\Program Files\Java\jre6\lib\rt.jar'
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontshrink
-printusage classes_eclipse\shrink.txt
-dontoptimize
-dontpreverify
-verbose
-keep class **.ParameterizedTypeTest.class {
<fields>;
<methods>;
}
-keep class ** {
<fields>;
<methods>;
}
# Keep - Applications. Keep all application classes, along with their 'main'
# methods.
-keepclasseswithmembers public class * {
public static void main(java.lang.String[]);
}
# Also keep - Enumerations. Keep the special static methods that are required in
# enumeration classes.
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# Also keep - Database drivers. Keep all implementations of java.sql.Driver.
-keep class * extends java.sql.Driver
# Also keep - Swing UI L&F. Keep all extensions of javax.swing.plaf.ComponentUI,
# along with the special 'createUI' method.
-keep class * extends javax.swing.plaf.ComponentUI {
public static javax.swing.plaf.ComponentUI createUI(javax.swing.JComponent);
}
# Keep names - Native method names. Keep all native class/method names.
-keepclasseswithmembers,allowshrinking class * {
native <methods>;
}
# Keep names - _class method names. Keep all .class method names. This may be
# useful for libraries that will be obfuscated again with different obfuscators.
-keepclassmembers,allowshrinking class * {
java.lang.Class class$(java.lang.String);
java.lang.Class class$(java.lang.String,boolean);
}
Có ai có một ý tưởng về cách giải quyết này (ngoài các workaround rõ ràng để đưa các tập tin có liên quan vào một riêng biệt jar và không obfuscate nó)?
Trân trọng,
Stefan
Ồ, đây là tin khủng khiếp mà bạn phải sử dụng @Provides phương pháp chỉ để ngăn ProGuard loại bỏ các phụ thuộc. Điều này đòi hỏi một công cụ để giúp đỡ. Tôi giả sử nó cũng sẽ làm việc (gần như bằng nhau khó chịu) để dính một loạt các dòng -keep trong cấu hình proguard? –
Có thể vì việc thêm chú thích -keep sẽ làm cho việc tái cấu trúc kém hiệu quả hơn, vì chúng ta sẽ phải đảm bảo trong lọ đầu ra rằng các tên lớp là hợp lệ. Để tâm trí của tôi, hoạt động trên một cấp độ Java thuần túy làm cho mã của chúng tôi tuân thủ nhiều quy tắc tái cấu trúc (nếu có một điều như vậy) – Riduidel