- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 WifiScanning 类:
public class WifiScanning extends AbstractSetting {
/**
*
*/
private static final long serialVersionUID = 226897434530036069L;
public WifiScanning(Object valueToApply) {
super(valueToApply, WifiScanning.class);
}
/**
* For persistence only
*/
public WifiScanning() {
super(null, WifiScanning.class);
}
如您所见,它有 2 个构造函数。一个用于我的简单持久层并且是一个空的构造函数,因此 newInstance() 可以工作,另一个采用单个参数,这是我的应用程序定义的标准。其他代码假定必须有一个带有单个参数的构造函数,否则它会抛出异常。
/**
*
* @param setting
* @param ctx
* @return
* @throws SettingException
*/
private synchronized static AbstractSetting getOriginalSetting(AbstractSetting setting,
Context ctx) throws SettingException {
Class<? extends AbstractSetting> clazz = setting.getClass();
try {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
Object original = setting.getCurrentSettingValue(ctx);
LOG.debug("Caching original value '"+original+"' for "+clazz.getSimpleName());
return (AbstractSetting) c.newInstance(original);
}
}
/*
* ###################### DEBUG BLOCK ######################
*
* This has been put here to work out why we are getting to this point in the code when
* using WifiScanning.java
*/
LOG.error("There are "+constructors.length+" constructors for "+clazz.getName()+" which we got from "+setting);
for (Constructor<?> c : constructors) {
if(c.getParameterTypes().length == 1) {
LOG.debug("Found the consructor! How the hell can that be?");
}
else {
LOG.error("Unusable constructor: "+c.toGenericString());
LOG.error("From: "+c.getDeclaringClass());
LOG.error("Modifiers:");
LOG.error("private="+Modifier.isPrivate(c.getModifiers()));
LOG.error("protected="+Modifier.isProtected(c.getModifiers()));
LOG.error("public="+Modifier.isPublic(c.getModifiers()));
LOG.error("static="+Modifier.isStatic(c.getModifiers()));
Type[] genericParameterTypes = c.getGenericParameterTypes();
LOG.error("Constructor has "+genericParameterTypes.length+" generic parameter types");
for (Type type : genericParameterTypes) {
LOG.error("Generic parameter type: "+type.getClass().getName());
}
Class<?>[] parameterTypes = c.getParameterTypes();
LOG.error("Constructor has "+parameterTypes.length+" parameters");
for (Class<?> arg1 : parameterTypes) {
LOG.error("Constructor arg: "+arg1.getName());
}
}
}
/*
* ###################### END DEBUG BLOCK ######################
*/
throw new SettingException(clazz+" does not have a constructor with a single argument");
如果您考虑上面的代码,就会添加 DEBUG BLOCK 以尝试理解这里发生的事情。如果你暂时忽略它,你得到的是一段代码,它从代码中获取一组构造函数并迭代它们,寻找具有单个参数的构造函数。如果循环没有找到一个就退出了,就会抛出一个异常。
添加异常 block 后,日志显示:
E/Proference: 10/7 22:28:59.917 e.b[126]: There are 1 constructors for com.domloge.proference.setting.WifiScanning which we got from WifiScanning "Test" [set:true|current:false|priority:1] E/Proference: 10/7 22:28:59.917 e.b[132]: Unusable constructor: public com.domloge.proference.setting.WifiScanning() V/Proference: Purging log file E/Proference: 10/7 22:28:59.918 e.b[133]: From: class com.domloge.proference.setting.WifiScanning E/Proference: 10/7 22:28:59.919 e.b[134]: Modifiers: E/Proference: 10/7 22:28:59.919 e.b[135]: private=false E/Proference: 10/7 22:28:59.920 e.b[136]: protected=false E/Proference: 10/7 22:28:59.920 e.b[137]: public=true E/Proference: 10/7 22:28:59.920 e.b[138]: static=false E/Proference: 10/7 22:28:59.921 e.b[141]: Constructor has 0 generic parameter types E/Proference: 10/7 22:28:59.921 e.b[147]: Constructor has 0 parameters E/Proference: 10/7 22:28:59.922 j.b[270]: Could not apply
如您所见,VM 显示 WifiScanning 类正在提供一个构造函数,而不是 2 个。这怎么可能?
当我在我的个人设备和各种模拟器上运行代码时,这不是问题,数组中有 2 个构造函数。
问题是,当应用程序通过 Google Play 商店分发时,该数组包含一个构造函数。当我的应用程序发布到 Google Play 商店时,我无法对其进行调试,我只能查看日志。
这个 principal 在应用程序中的 10 多个其他类中工作得很好......但是这个正在发挥作用......就好像我遗漏了一个错字,这将是一个前额拍打 d '哦!当有人指出我的愚蠢错误时......
有什么想法吗?
最佳答案
没错,明明就是proguard“michief”。
将以下行放入 proguard-project.txt
-keepclassmembers class * extends full-package-name.AbstractSetting {
public protected <init>(...);
}
一切都会好起来的。
关于java - Android clazz.getConstructors() 返回错误数量的构造函数,仅在实时应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234128/
我试图通过这个例子来理解这种差异: import java.lang.reflect.*; public class ClassDemo { public static void main(St
我把问题写成注释在代码里了,我觉得这样更容易理解。 public class Xpto{ protected AbstractClass x; public void foo(){
import java.util.*; import java.text.*; import java.lang.reflect.*; public class Test { String name;
我正在学习 Java 中的一些反射功能,并且在使用此类测试 getConstructor() 函数时遇到了一个奇怪的问题。 public class IntegerSequence { priva
我正在试验 System.Type。在下面的代码中,我对数组类型使用了 GetConstructors: using System; using System.Reflection; class An
我正在尝试寻找具有特定签名的构造函数。此构造函数在当前类型中不存在,但在其父类型中存在。举例说明: public class Base { public Base() { }
我正在尝试通过反射构造以下类的实例。 public class Abc { private int _a; public Abc(int a) { _a = a
我有一个像下面这样的java类,我想通过使用类名动态创建这个类的实例。 class Demo { public Demo(String... s) { //some initializ
我正在尝试通过反射构造以下类的实例。 public class Abc { private int _a; public Abc(int a) { _a = a
我编写了一个抽象类“Card”,其中包含两个名为“Sticker”和“PlayingCard”的子类。 “PlayingCard”类有一个名为“Color”属性的枚举对象,它基本上说明了它是什么类型的
我有一个类,我在静态 block 中初始化了 HashMap 。通过 key ,我检索了一个类的值。为了为这个类创建对象。我使用构造函数类来获取构造函数并传递参数并创建对象。 我在 HashMap 中
我想使用 Class.getConstructor,因为在运行时我不知道需要什么实例。 我有一个带有构造函数 A(Type1) 的类 A。 Type2 扩展了 Type1。现在,当我调用 A(Type
我有一个名为 ICwsTransactionProcessing_Capture_CWSInvalidOperationFaultFault_FaultMessage 的类,定义如下 public c
由于工厂类存在问题,我传入了一个人类可读的名称,该名称映射到具有单个构造函数和单个参数的类,我收到以下错误: java.lang.NoSuchMethodException: com.satgraf.
我尝试了以下代码: constructor = oneClass.getConstructor(new Class[]{String[].class}); return constructor.new
我是 JAVA 新手我正在努力学习 reflection .我想获得特定的构造函数(选择示例形式 here )来 self 的类(class): public class Example1 {
假设我有类 A 和 B,其中 B 扩展 A。我在 B 中也有一个构造函数,带有一个类型为 A 的参数。我还有一个名为 bObj 的 B 对象。 有没有办法调用 B.class.getConstruct
如果我有一个带有参数化泛型的构造函数的类: public class Foo { public Foo(Map data) { ... } } ...如果我想调用,我该如何
是否可以使用getConstructor获取下面类X的构造函数? public class A { } public class Y { } public class X extends Y {
这个问题在这里已经有了答案: 关闭 12 年前。 Possible Duplicate: Java: How to load a class (and its inner classes) that
我是一名优秀的程序员,十分优秀!