gpt4 book ai didi

java - Android clazz.getConstructors() 返回错误数量的构造函数,仅在实时应用程序中

转载 作者:行者123 更新时间:2023-11-29 05:19:45 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com