Interface ClassMaker
-
Method Summary
Modifier and TypeMethodDescriptionSwitch this class to be abstract.Add code to the static initializer of this class.default MethodMaker
addConstructor
(MethodType type) Add a constructor to this class.addConstructor
(Object... paramTypes) Add a constructor to this class.Add a field to the class.addInnerClass
(String className) Add an inner class to this class.Add a method to this class.default MethodMaker
addMethod
(String name, MethodType type) Add a method to this class.Indicate that this class is defining an annotation interface.Begin defining another class with the same loader and lookup as this one.arrayType
(int dimensions) Returns a type object which represents the class being made as an array.asRecord()
Convert this class to arecord
, and return a newly added constructor for it.static ClassMaker
begin()
Begin defining a class with an automatically assigned name.static ClassMaker
Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.static ClassMaker
begin
(String className, ClassLoader parentLoader) Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.static ClassMaker
begin
(String className, ClassLoader parentLoader, Object key) Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.static ClassMaker
begin
(String className, MethodHandles.Lookup lookup) Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.static ClassMaker
beginExplicit
(String className, ClassLoader parentLoader, Object key) Begin defining a class with an explicitly specified name.static ClassMaker
beginExplicit
(String className, MethodHandles.Lookup lookup) Begin defining a class with an explicitly specified name.static ClassMaker
beginExternal
(String className) Begin defining a class intended to be loaded from a file.Returns the class loader that the finished class will be loaded into.enum_()
Indicate that this class is defining an enum.Set a class that this class extends.final_()
Switch this class to be final.Class
<?> finish()
Finishes the definition of the new class.byte[]
Finishes the definition of the new class into a byte array.Finishes the definition of a new hidden class, returning a lookup which has full privilege access to the class.Finishes the definition of the new class, returning a lookup which has full privilege access to the class.void
finishTo
(OutputStream out) Finishes the definition of the new class and writes it to a stream.Add an interface that this class or interface implements.boolean
installClass
(Class<?> clazz) Directly install a dependent class into the class loader used by the finished class.Switch this class to be an interface.name()
Returns the tentative name of the class being made.permitSubclass
(Object subclass) Convert this class into a sealed class by permitting a subclass.private_()
Switch this class to be private.Switch this class to be protected.public_()
Switch this class to be public.Define a signature for this member, which is a string for supporting generic types.sourceFile
(String fileName) Set the source file of this class file by adding a source file attribute.static_()
Switch this class to be static.Indicate that this class is synthetic.type()
Returns the type of this class being made.Returns the set of methods which would need to be implemented by this class in order for it to be non-abstract.Methods inherited from interface org.cojen.maker.Maker
addAnnotation, addAttribute, classMaker
-
Method Details
-
begin
Begin defining a class with an automatically assigned name. -
begin
Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.- Parameters:
className
- fully qualified class name; pass null to automatically assign a name
-
begin
Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.- Parameters:
className
- fully qualified class name; pass null to automatically assign a nameparentLoader
- parent class loader; pass null to use default
-
begin
Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.- Parameters:
className
- fully qualified class name; pass null to automatically assign a nameparentLoader
- parent class loader; pass null to use defaultkey
- an opaque key used for creating distinct class loaders; can be null
-
begin
Begin defining a class with the given name, but the actual name will have a suffix applied to ensure uniqueness.- Parameters:
className
- fully qualified class name; pass null to automatically assign a namelookup
- finish loading the class using this lookup object
-
beginExplicit
Begin defining a class with an explicitly specified name.- Parameters:
className
- fully qualified class nameparentLoader
- parent class loader; pass null to use defaultkey
- an opaque key used for creating distinct class loaders; can be null
-
beginExplicit
Begin defining a class with an explicitly specified name.- Parameters:
className
- fully qualified class namelookup
- finish loading the class using this lookup object
-
beginExternal
Begin defining a class intended to be loaded from a file. The class name exactly matches the one given, andsetExact
is unsupported. All classes defined from this maker will also be external.- Parameters:
className
- fully qualified class name- See Also:
-
another
Begin defining another class with the same loader and lookup as this one. The actual class name will have a suffix applied to ensure uniqueness, unless this maker creates explicit or external classes.The returned
ClassMaker
instance isn't attached to this maker, and so it can be acted upon by a different thread.- Parameters:
className
- fully qualified class name; pass null to automatically assign a name (unless explicit or external)- See Also:
-
type
Type type()Returns the type of this class being made. -
name
String name()Returns the tentative name of the class being made. IffinishHidden
is called, then the actual class name will differ. -
public_
ClassMaker public_()Switch this class to be public. Classes are package-private by default. -
private_
ClassMaker private_()Switch this class to be private. Classes are package-private by default. -
protected_
ClassMaker protected_()Switch this class to be protected. Classes are package-private by default.- Specified by:
protected_
in interfaceMaker
- Returns:
- this
-
static_
ClassMaker static_()Switch this class to be static. Classes are non-static by default. -
final_
ClassMaker final_()Switch this class to be final. Classes are non-final by default. -
interface_
ClassMaker interface_()Switch this class to be an interface. Classes are classes by default.- Returns:
- this
-
abstract_
ClassMaker abstract_()Switch this class to be abstract. Classes are non-abstract by default.- Returns:
- this
-
synthetic
ClassMaker synthetic()Indicate that this class is synthetic. Classes are non-synthetic by default. -
enum_
ClassMaker enum_()Indicate that this class is defining an enum. No checks or modifications are performed to ensure that the enum class is defined correctly.- Returns:
- this
-
annotation
-
extend
Set a class that this class extends.- Parameters:
superClass
- non-null type, specified by a Class or a String, etc.- Returns:
- this
- Throws:
IllegalArgumentException
- if the type is unsupportedIllegalStateException
- if already assigned
-
implement
Add an interface that this class or interface implements. Call this method multiple times to implement more interfaces.- Parameters:
iface
- non-null type, specified by a Class or a String, etc.- Returns:
- this
- Throws:
IllegalArgumentException
- if the type is unsupported
-
signature
Define a signature for this member, which is a string for supporting generic types. The components can be strings or types (class, ClassMaker, etc.), which are concatenated into a single string. Consult the JVM specification for the signature syntax. -
permitSubclass
Convert this class into a sealed class by permitting a subclass. Call this method multiple times to permit more subclasses.- Parameters:
subclass
- non-null type, specified by a Class or a String, etc.- Returns:
- this
- Throws:
IllegalArgumentException
- if the type is unsupported
-
addField
Add a field to the class.- Parameters:
type
- a class or name- Throws:
IllegalArgumentException
- if the type is unsupportedIllegalStateException
- if field is already defined
-
addMethod
Add a method to this class.- Parameters:
retType
- a class or name; can be null if method returns voidparamTypes
- classes or names; can be null if method accepts no parameters- Throws:
IllegalArgumentException
- if a type is unsupported
-
addMethod
Add a method to this class.- Parameters:
type
- defines the return type and parameter types
-
addConstructor
Add a constructor to this class.- Parameters:
paramTypes
- classes or names; can be null if constructor accepts no parameters- Throws:
IllegalArgumentException
- if a type is unsupported
-
addConstructor
Add a constructor to this class.- Parameters:
type
- defines the parameter types- Throws:
IllegalArgumentException
- if the return type isn't void
-
addClinit
MethodMaker addClinit()Add code to the static initializer of this class. Multiple initializers can be added, and they're all stitched together when the class definition is finished. Returning from one initializer only breaks out of the local scope. -
asRecord
MethodMaker asRecord()Convert this class to arecord
, and return a newly added constructor for it. Each field which is currently defined in this class is treated as a record component, and each is also represented by a constructor parameter. The constructor shouldn't set the fields directly, since this is performed automatically at the end.Unless already defined, the
equals
,hashCode
, andtoString
methods are automatically added. The same rule applies for the component accessor methods. -
addInnerClass
Add an inner class to this class. The actual class name will have a suitable suffix applied to ensure uniqueness.The returned
ClassMaker
instance isn't attached to this maker, and so it can be acted upon by a different thread.- Parameters:
className
- simple class name; pass null to use default- Throws:
IllegalArgumentException
- if not given a simple class name- See Also:
-
sourceFile
Set the source file of this class file by adding a source file attribute.- Returns:
- this
-
arrayType
Returns a type object which represents the class being made as an array.- Parameters:
dimensions
- must be at least 1- Throws:
IllegalArgumentException
- if the given dimensions is less than 1 or greater than 255
-
classLoader
ClassLoader classLoader()Returns the class loader that the finished class will be loaded into. -
installClass
Directly install a dependent class into the class loader used by the finished class. This is necessary when the given class cannot be found by the normal hierarchical class loading technique.Note: When installed, the given class is held by a weak reference, to facilitate unloading. If the installed class can be unloaded before finishing the new class, a
reachability fence
might be required.- Returns:
- true if installed, or else false if the class was already installed
- Throws:
IllegalStateException
- if this maker was begun with a lookup object, or if the class loader already refers to a different class with the same name
-
unimplementedMethods
-
finish
Class<?> finish()Finishes the definition of the new class.- Throws:
IllegalStateException
- if already finished or if the definition is broken
-
finishLookup
MethodHandles.Lookup finishLookup()Finishes the definition of the new class, returning a lookup which has full privilege access to the class. Calling this method has the side effect of forcing the new class to be initialized.- Returns:
- the lookup for the class; call
lookupClass
to obtain the actual class - Throws:
IllegalStateException
- if already finished or if the definition is broken
-
finishHidden
MethodHandles.Lookup finishHidden()Finishes the definition of a new hidden class, returning a lookup which has full privilege access to the class. Hidden classes are automatically unloaded when no longer referenced, even if the class loader still is. If a lookup object was passed to thebegin
method, the hidden class is defined in the same nest as the lookup class.- Returns:
- the lookup for the class; call
lookupClass
to obtain the actual class - Throws:
IllegalStateException
- if already finished or if the definition is broken
-
finishBytes
byte[] finishBytes()Finishes the definition of the new class into a byte array.- Throws:
IllegalStateException
- if already finished or if the definition is broken
-
finishTo
Finishes the definition of the new class and writes it to a stream.- Throws:
IllegalStateException
- if already finished or if the definition is brokenIOException
-