简体   繁体   English

(Java)为通过反射创建的新对象创建方法吗?

[英](Java) Creating methods for new object created via reflection?

I have abstract methods in a class that need to be implemented by a foreign class in a SEPARATE project that uses my project. 我的类中有抽象方法,该类中的抽象方法需要使用我的项目的SEPARATE项目中的外国类来实现。

-- All classes instanceof A are initially generated using reflection -- -A的所有类instanceof最初都是使用反射生成的-

So anyway, say Class A is abstract, and Class B (non-abstract) extends A 因此,无论如何,假设A类是抽象的,而B类(非抽象)扩展了A

B has all the unimplemented methods in Class A because B is in my workspace so I know to add those methods. B在类A中具有所有未实现的方法,因为B在我的工作空间中,所以我知道要添加这些方法。

C also extends A, but C only has a subset of the abstract methods in A. C, however, is not in my workspace. C还扩展了A,但是C在A中仅具有抽象方法的一个子集。但是,C不在我的工作空间中。

Therefore, for each abstract method in C NOT in A, I need to find some way to add the method for A like so: 因此,对于A中C NOT中的每个抽象方法,我需要找到某种方法为A添加方法,如下所示:

(For each method) (每种方法)

public <corresponding return type> <missingMethodName>() { return null; }

Is this possible? 这可能吗?

PS Please assume that I either have to completely rewrite my code to be in sync with the objects I have no control over, or implement a solution like the one I am alluding to above. PS:请假设我要么必须完全重写我的代码才能与我无法控制的对象同步,要么要实现一种我上面提到的解决方案。

No, unless I'm reading you incorrectly, what you're asking for doesn't really make much sense. 不,除非我对您的阅读有误,否则您的要求实际上并没有多大意义。

If you wanted to inject a method 如果您想注入方法

public <corresponding return type> <missingMethodName>() { super.<missingMethodName>(); }

into C, which extends A, which doesn't implement that method, what would it exactly do? 到C中,这扩展了A(未实现该方法),它究竟会做什么?

If you want to provide a default implementation in A, that's fine, and it won't affect C. If you add abstract methods into A, C must implement them, mark itself as abstract, or it won't compile (or throw serialization, or some weird error) if you run with a C compiled with an older A. 如果要在A中提供默认实现,那很好,并且不会影响C。如果将抽象方法添加到A中,则C必须实现它们,将其标记为抽象,否则将无法编译(或引发序列化) ,或一些怪异的错误),如果您使用由旧A编译的C。

You should never need to do this as any instance method which has a super implementation can be called on a sub-class instance. 您永远不需要这样做,因为任何具有super实现的实例方法都可以在子类实例上调用。

You can add these methods using byte code, but the only difference they would make is to change the list of getDefinedMethods(). 您可以使用字节码添加这些方法,但是它们唯一的区别是更改getDefinedMethods()的列表。 However it wouldn't change the behaviour of the objects of the class. 但是,它不会改变类的对象的行为。

its quiet difficult but you can do it with Javassist 它的安静困难,但是您可以使用Javassist做到

Javassist (Java programming assistant) is a Java library providing a means to manipulate the Java bytecode of an application. Javassist(Java编程助手)是一个Java库,提供了一种操作应用程序的Java字节码的方法。 1 In this sense Javassist provides the support for structural reflection, ie the ability to change the implementation of a class at run time. 1从这个意义上讲,Javassist提供了对结构反射的支持,即在运行时更改类的实现的能力。 Bytecode manipulation is performed at load-time through a provided class loader. 字节码操作是在加载时通​​过提供的类加载器执行的。 http://en.wikipedia.org/wiki/Javassist http://en.wikipedia.org/wiki/Javassist

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM