复制类但覆盖现有的类导入

huangapple go评论57阅读模式
英文:

Copy class but overwrite the existing class imports

问题

我有一个带有现有导入的类:

import org.packageA.Peeler

public class Potato {

    Peeler peeler = new Peeler();

}

我想能够复制这个类或者创建一个对象,但将 "import org.packageA.Peeler" 更改为 "import org.packZ.Peeler"。
是否可以动态地实现这个?

英文:

I have a class with an existing import:

import org.packageA.Peeler

public class Potato {

    Peeler peeler = new Peeler();

}

I'd like to be able to copy this class or create an object with it but change "import org.packageA.Peeler" to "import org.packZ.Peeler".

Is this possible to do dynamically?

答案1

得分: 1

不。

import语句有点误导性。import com.foo.A; 的意思是:在这个源文件中,每当我只写A,就相当于我写了com.foo.A,并且仅限于此。它不表示:初始化这个类或解析这个源文件或任何其他类似的操作(这通常是在其他环境中import的含义)。

因此,你所问的是:我是否可以获取class Potato { org.packA.Peeler peeler = new org.packA.Peeler(); },并以某种方式动态创建一个不同的Potato类,就好像我写了class Potato { org.packA.Peeler peeler = new org.packA.Peeler(); }一样 - 答案是否定的;org.packA.Peelerorg.packZ.Peeler之间的关系就像枪支和祖母一样(即它们有相同的名称是无关紧要的)。

即使你可以,那又意味着什么呢?Java是静态和显式类型的,你不可能编写引用这个重写的potato类的代码,而不使用反射或动态代码生成,在Java中,这几乎总是错误的答案。

你可以尝试一些奇特的选项:

  1. 使用正则表达式或构建脚本插件创建一个应用了一些重命名的源文件,并将其包含在构建和编译过程中(不是一个好主意,但我猜你可以这样做)。
  2. 使用ASM、BCEL或其他一些类文件工具,以字节数组形式创建一个带有重命名的新类文件,使用自定义类加载器动态加载它,然后生成使用它的字节码,或者使用反射访问。这非常复杂,需要大量的代码,几乎没有实际用途。

也许退一步,解释一下让你觉得需要动态重写这个类来使用不同包的问题是什么?但我不知道怎么做,所以我要问SO(Stack Overflow) - 除非你正在问错误的问题,请询问你最初的问题。

英文:

No.

import statements are a bit of a misnomer. import com.foo.A; means: Anytime I write just A in this source file, imagine I wrote com.foo.A and that is all it means. It does not mean: Initialize this class or parse this source file or any other such thing (which is usually what import means in other environments).

Thus, what you're asking is: Can I take class Potato { org.packA.Peeler peeler = new org.packA.Peeler(); } and somehow dynamically create a different Potato class that is as if I wrote class Potato { org.packA.Peeler peeler = new org.packA.Peeler(); } - to which the answer is no; org.packA.Peeler is as related to org.packZ.Peeler as guns and grandmas (i.e.: That they have the same name is immaterial).

Even if you could, what would that mean? Java is statically and nominally typed, it wouldn't be possible to write code that refers to this rewritten potato class without using reflection or dynamic code generation which, in java at any rate, are almost always the wrong answer.

Some exotic options you do have:

  1. Use a regexp or build script plugin to make a source file with some renames applied and include it in the build and compilation process (bad idea, but I guess you can do that)
  2. Use ASM, BCEL or some other classfile tool to create a new class file in byte array form with renames applied, have a custom classloader that dynamically loads this, and then generate bytecode that uses this, or use reflective access. This is extremely convoluted, requires a ton of code, and is almost useless.

Perhaps take a step back and explain the problem you have that made you think: I know! I'll dynamically rewrite this class to use a different package! But I don't know how so I'll ask SO - except you're asking the wrong question, ask about your original problem.

huangapple
  • 本文由 发表于 2020年10月14日 01:50:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64340534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定