@Retention(value=CLASS) @Target(value=METHOD) public @interface ConstructorDelegate
@PatchFor
implementation type
as its first parameter. The rest of parameter types will be used to generate
a constructor in the original API class, with the same thrown exceptions and
access modifiers.
The generated constructor will first call
the default constructor of the API class, then delegate the
initialization work to the static method passing this
typed as
the @PatchFor
class as the first parameter. Formally, at compile-time, the API class does
not derive from the @PatchFor
supertype. Passing the new
instance typed as the API class would require type casting to the
@PatchFor
supertype in order to access the data for the injected
code.
The annotation has only effect if the method's declaring class was annotated
using @PatchFor
.
Take, for example, the following code from openide.filesystems
module:
@PatchFor(JarFileSystem.class)
public abstract class JarFileSystemCompat extends AbstractFileSystem {
@ConstructorDelegate
public static void createJarFileSystemCompat(JarFileSystemCompat jfs, FileSystemCapability cap) throws IOException {
...
}
}
Will cause generate, at runtime (during class loading), a new constructor in the
JarFileSystem
class:
JarFileSystem(FileSystemCapability c) throws IOException {
this();
JarFileSystemCompat.createJarFileSystemCompat(this, c);
}
In the case it is necessary to invoke a non-default constructor, the
ConstructorDelegate.delegateParams()
attribute enumerates which parameters of the static
annotated method are also passed to the non-default constructor. Parameter positions
are zero-based (0 = the object type itself) and must be listed in the same order
and have the same types as in the invoked constructor declaration.
@PatchFor(JarFileSystem.class)
public abstract class JarFileSystemCompat extends AbstractFileSystem {
@ConstructorDelegate(delegateParams = {1, 2} )
public static void createJarFileSystemCompat(JarFileSystemCompat jfs, FileSystemCapability cap, File f) throws IOException {
...
}
}
will produce an equivalent of
JarFileSystem(FileSystemCapability cap, File f) throws IOException {
this(f);
JarFileSystemCompat.createJarFileSystemCompat(this, cap, f);
}
PatchFor
Modifier and Type | Optional Element and Description |
---|---|
int[] |
delegateParams
Specifies the position of parameters passed to the real class' constructor.
|
public abstract int[] delegateParams