我有一个使用XML和反射的Object类将s 返回到另一个类.
通常这些对象是外部对象的子字段,但偶尔它是我想要动态生成的东西.我尝试过类似的东西,但无济于事.我相信这是因为Java不允许您访问private反射方法.
Element node = outerNode.item(0);
String methodName = node.getAttribute("method");
String objectName = node.getAttribute("object");
if ("SomeObject".equals(objectName))
object = someObject;
else
object = this;
method = object.getClass().getMethod(methodName, (Class[]) null);
Run Code Online (Sandbox Code Playgroud)
如果提供的方法是private,它失败了NoSuchMethodException.我可以通过制作方法public或使另一个类从中派生它来解决它.
长话短说,我只是想知道是否有办法private通过反射访问方法.
我喜欢在其他语言的方法/函数名称末尾使用问号.Java不允许我这样做.作为一种解决方法,我如何在Java中命名布尔返回方法?使用is,has,should,can在方法的前声好一些情况.有没有更好的方法来命名这样的方法?
例如createFreshSnapshot?
所以我想说我正试图从一个类中获取一个方法Method m = plugin.getClass().getDeclaredMethod("getFile");.
但是那个plugin类正在扩展另一个类,即该getFile方法的类.我不太确定是否会使它抛出NoSuchMethodException异常.
我知道plugin扩展的类具有getFile方法.对不起,如果我听起来很混乱,有点累.
TL; DR:
如何减少冗余(任何有效的方法)?
if (personModification.firstName != null) {person.firstName = personModification.firstName}
if (personModification.lastName != null) {person.lastName = personModification.lastName}
if (personModification.job != null) {person.job = personModification.job}
Run Code Online (Sandbox Code Playgroud)
长版:我有一个简单的问题.我有一节课Person:
class Person (val firstName: String?,
val lastName: String?,
val job: String?)
Run Code Online (Sandbox Code Playgroud)
我有一个叫做的课PersonModification:
class PersonModification(val firstName: String?,
val lastName: String?,
val job: String?)
Run Code Online (Sandbox Code Playgroud)
任务是Person用PersonModification值覆盖任何属性值,如果PersonModification属性不是null.如果你关心,这背后的业务逻辑是一个API端点,它修改Person并接受一个PersonModification参数(但可以更改所有或任何属性,因此我们不希望用空值覆盖有效的旧值).对此的解决方案看起来像这样.
if (personModification.firstName != null) {person.firstName = personModification.firstName}
if (personModification.lastName != null) {person.lastName = personModification.lastName}
if …Run Code Online (Sandbox Code Playgroud) 所以我在几个类中有数百个字段,我想在它们上面编写一些方法,它们会自动显示println每个字段及其对应的值
目前我有这个:
inner class Version(val profile: Profile) {
@JvmField val MINOR_VERSION = glGetInteger(GL_MINOR_VERSION)
fun write(file: File? = null) {
//file.printWriter().use { out -> out.pri }
this::class.java.fields.forEach {
println(it.isAccessible)
println(it.getInt(it)) }
}
}
Run Code Online (Sandbox Code Playgroud)
但这就是我得到的:
false
Exception in thread "main" java.lang.IllegalArgumentException: Can not set final int field uno.caps.Caps$Version.MINOR_VERSION to java.lang.reflect.Field
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeQualifiedIntegerFieldAccessorImpl.getInt(UnsafeQualifiedIntegerFieldAccessorImpl.java:58)
Run Code Online (Sandbox Code Playgroud)
任何的想法?
我的实体类:
class User : ActiveRecord<User>() {
var name by Column(String.javaClass);
var id by Column(Int.javaClass);
}
Run Code Online (Sandbox Code Playgroud)
现在我想通过refelection设置名称值:
var clazz = User().javaClass
var record = clazz.newInstance()
var field = record.getDeclaredField(it + "$" + "delegate")
field.set(record, "aa")
Run Code Online (Sandbox Code Playgroud)
那么错误:
entity.Column字段ActiveRecord4k.User.name $委托给java.lang.String
这该怎么做?
是否可以使用反射来访问对象的私有字段并在此字段上调用公共方法?
即
class Hello {
private World word
}
class World {
public BlaBlaBla foo()
}
Hello h = new Hello()
World world = reflect on the h
// And then
world.foo()
Run Code Online (Sandbox Code Playgroud) java ×4
kotlin ×4
reflection ×4
android ×1
delegates ×1
dry ×1
methods ×1
optimization ×1
private ×1