Class Gizmo
- java.lang.Object
-
- io.quarkus.gizmo.Gizmo
-
public final class Gizmo extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classGizmo.CustomInvocationGeneratorHelper class to build a bytecode generator for repetitive tasks.private static classGizmo.EqualsHashCodeToStringGeneratorstatic classGizmo.InstanceInvocationGeneratorAn abstract base for all instance invokers.static classGizmo.JdkCollectionBytecode generator for static methods.static classGizmo.JdkIterableBytecode generator for static methods.static classGizmo.JdkIteratorBytecode generator for static methods.static classGizmo.JdkListBytecode generator for static methods.static classGizmo.JdkMapstatic classGizmo.JdkOptionalBytecode generator for static methods.static classGizmo.JdkSetstatic classGizmo.StaticInvocationGeneratorAn abstract base for all generators.static classGizmo.StringBuilderGenerator
-
Field Summary
Fields Modifier and Type Field Description static intASM_API_VERSIONstatic MethodDescriptorEQUALSstatic MethodDescriptorPRINTLNstatic FieldDescriptorSYSTEM_ERRstatic FieldDescriptorSYSTEM_OUTstatic MethodDescriptorTO_STRING
-
Constructor Summary
Constructors Modifier Constructor Description privateGizmo()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Gizmo.JdkCollectioncollectionOperations(BytecodeCreator target)ResultHandle size = Gizmo.collectionOperations(methodBytecode).instance(myList).size();static ResultHandleequals(BytecodeCreator target, ResultHandle obj1, ResultHandle obj2)Generates the bytecode that callsObject.equals(Object).static voidgenerateEquals(ClassCreator clazz, FieldDescriptor... fields)Generates a structuralequalsmethod in givenclazzthat compares givenfields.static voidgenerateEquals(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)Generates a structuralequalsmethod in givenclazzthat compares givenfields.static voidgenerateEqualsAndHashCode(ClassCreator clazz, FieldDescriptor... fields)Generates structuralequalsandhashCodemethods in givenclazz, based on givenfields.static voidgenerateEqualsAndHashCode(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)Generates structuralequalsandhashCodemethods in givenclazz, based on givenfields.static voidgenerateNaiveToString(ClassCreator clazz, FieldDescriptor... fields)Generates a naivetoStringmethods in givenclazz, based on givenfields.static voidgenerateNaiveToString(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)Generates a naivetoStringmethods in givenclazz, based on givenfields.static Gizmo.JdkIterableiterableOperations(BytecodeCreator target)ResultHandle iterator = Gizmo.iterableOperations(methodBytecode).instance(myCollection).iterator();static Gizmo.JdkIteratoriteratorOperations(BytecodeCreator target)ResultHandle hasNext = Gizmo.iteratorOperations(methodBytecode).instance(myIterator).hasNext();static Gizmo.JdkListlistOperations(BytecodeCreator target)ResultHandle firstElement = Gizmo.listOperations(methodBytecode).instance(myList).get(1);static Gizmo.JdkMapmapOperations(BytecodeCreator target)ResultHandle mapping = Gizmo.mapOperations(methodBytecode).instance(myMap).get(myKey);static ResultHandlenewArrayList(BytecodeCreator target)Generates the bytecode that calls the no-argsArrayListconstructor.static ResultHandlenewArrayList(BytecodeCreator target, int initialCapacity)Generates the bytecode that calls theArrayListconstructor with the specified initial capacity.static ResultHandlenewHashMap(BytecodeCreator target)Generates the bytecode that calls the no-argsHashMapconstructor.static ResultHandlenewHashSet(BytecodeCreator target)Generates the bytecode that calls the no-argsHashSetconstructor.static Gizmo.StringBuilderGeneratornewStringBuilder(BytecodeCreator target)Creates aStringBuildergenerator that helps to generate a chain ofappendcalls and a finaltoStringcall.static Gizmo.StringBuilderGeneratornewStringBuilder(BytecodeCreator target, int capacity)Creates aStringBuildergenerator that helps to generate a chain ofappendcalls and a finaltoStringcall.static Gizmo.JdkOptionaloptionalOperations(BytecodeCreator target)ResultHandle optionalFoo = Gizmo.optionalOperations(methodBytecode).ofNullable(foo);static Gizmo.JdkSetsetOperations(BytecodeCreator target)ResultHandle set = Gizmo.setOperations(methodBytecode).of(element);static voidsystemErrPrintln(BytecodeCreator target, ResultHandle obj)Generates the bytecode that callsSystem.err.println(obj).static voidsystemOutPrintln(BytecodeCreator target, ResultHandle obj)Generates the bytecode that callsSystem.out.println(obj).static ResultHandletoString(BytecodeCreator target, ResultHandle obj)Generates the bytecode that callsObject.toString().
-
-
-
Field Detail
-
ASM_API_VERSION
public static final int ASM_API_VERSION
- See Also:
- Constant Field Values
-
TO_STRING
public static final MethodDescriptor TO_STRING
-
EQUALS
public static final MethodDescriptor EQUALS
-
PRINTLN
public static final MethodDescriptor PRINTLN
-
SYSTEM_OUT
public static final FieldDescriptor SYSTEM_OUT
-
SYSTEM_ERR
public static final FieldDescriptor SYSTEM_ERR
-
-
Method Detail
-
toString
public static ResultHandle toString(BytecodeCreator target, ResultHandle obj)
Generates the bytecode that callsObject.toString().- Parameters:
target-obj-- Returns:
- the result
-
equals
public static ResultHandle equals(BytecodeCreator target, ResultHandle obj1, ResultHandle obj2)
Generates the bytecode that callsObject.equals(Object).- Parameters:
target-obj1-obj2-- Returns:
- the result
-
systemOutPrintln
public static void systemOutPrintln(BytecodeCreator target, ResultHandle obj)
Generates the bytecode that callsSystem.out.println(obj).- Parameters:
target-obj-
-
systemErrPrintln
public static void systemErrPrintln(BytecodeCreator target, ResultHandle obj)
Generates the bytecode that callsSystem.err.println(obj).- Parameters:
target-obj-
-
listOperations
public static Gizmo.JdkList listOperations(BytecodeCreator target)
ResultHandle firstElement = Gizmo.listOperations(methodBytecode).instance(myList).get(1);
- Parameters:
target-- Returns:
- the generator
-
collectionOperations
public static Gizmo.JdkCollection collectionOperations(BytecodeCreator target)
ResultHandle size = Gizmo.collectionOperations(methodBytecode).instance(myList).size();
- Parameters:
target-- Returns:
- the generator
-
setOperations
public static Gizmo.JdkSet setOperations(BytecodeCreator target)
ResultHandle set = Gizmo.setOperations(methodBytecode).of(element);
- Parameters:
target-- Returns:
- the generator
-
optionalOperations
public static Gizmo.JdkOptional optionalOperations(BytecodeCreator target)
ResultHandle optionalFoo = Gizmo.optionalOperations(methodBytecode).ofNullable(foo);
- Parameters:
target-- Returns:
- the generator
-
iterableOperations
public static Gizmo.JdkIterable iterableOperations(BytecodeCreator target)
ResultHandle iterator = Gizmo.iterableOperations(methodBytecode).instance(myCollection).iterator();
- Parameters:
target-- Returns:
- the generator
-
iteratorOperations
public static Gizmo.JdkIterator iteratorOperations(BytecodeCreator target)
ResultHandle hasNext = Gizmo.iteratorOperations(methodBytecode).instance(myIterator).hasNext();
- Parameters:
target-- Returns:
- the generator
-
mapOperations
public static Gizmo.JdkMap mapOperations(BytecodeCreator target)
ResultHandle mapping = Gizmo.mapOperations(methodBytecode).instance(myMap).get(myKey);
- Parameters:
target-- Returns:
- the generator
-
newStringBuilder
public static Gizmo.StringBuilderGenerator newStringBuilder(BytecodeCreator target)
Creates aStringBuildergenerator that helps to generate a chain ofappendcalls and a finaltoStringcall.StringBuilderGenerator str = Gizmo.newStringBuilder(bytecode); str.append("constant"); str.append(someResultHandle); ResultHandle result = str.callToString();Theappendmethod mimics the regularStringBuilder.append, so it acceptsResultHandles of all types for whichStringBuilderhas an overload:- primitive types
char[]java.lang.Stringjava.lang.Object
char[]are appended usingObject.toStringand ifArrays.toStringshould be used, it must be generated manually.Methods for appending only a part of
char[]orCharSequenceare not provided. OtherStringBuildermethods are not provided either. This is just a simple utility for generating code that concatenates strings, e.g. for implementing thetoStringmethod.- Parameters:
target-- Returns:
- the generator
-
newStringBuilder
public static Gizmo.StringBuilderGenerator newStringBuilder(BytecodeCreator target, int capacity)
Creates aStringBuildergenerator that helps to generate a chain ofappendcalls and a finaltoStringcall.StringBuilderGenerator str = Gizmo.newStringBuilder(bytecode, capacity); str.append("constant"); str.append(someResultHandle); ResultHandle result = str.callToString();Theappendmethod mimics the regularStringBuilder.append, so it acceptsResultHandles of all types for whichStringBuilderhas an overload:- primitive types
char[]java.lang.Stringjava.lang.Object
char[]are appended usingObject.toStringand ifArrays.toStringshould be used, it must be generated manually.Methods for appending only a part of
char[]orCharSequenceare not provided. OtherStringBuildermethods are not provided either. This is just a simple utility for generating code that concatenates strings, e.g. for implementing thetoStringmethod.- Parameters:
target-capacity-- Returns:
- the generator
-
generateEquals
public static void generateEquals(ClassCreator clazz, FieldDescriptor... fields)
Generates a structuralequalsmethod in givenclazzthat compares givenfields. The generated code is similar to what IDEs would typically generate from a template:- Reference equality is tested. If
thisis idential to the other object,trueis returned. - Type of the other object is tested using
instanceof. If the other object is not an instance of this class,falseis returned. - All fields are compared. Primitive types are compared using
==, object types are compared usingObjects.equals, single-dimension arrays are compared usingArrays.equals, and multi-dimensional arrays are compared usingArrays.deepEquals. If one of the comparisons fails,falseis returned. - Otherwise,
trueis returned.
If the class already has an
equalsmethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichequalsshould be generatedfields- fields to consider in theequalsmethod
- Reference equality is tested. If
-
generateEquals
public static void generateEquals(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)
Generates a structuralequalsmethod in givenclazzthat compares givenfields. The generated code is similar to what IDEs would typically generate from a template:- Reference equality is tested. If
thisis idential to the other object,trueis returned. - Type of the other object is tested using
instanceof. If the other object is not an instance of this class,falseis returned. - All fields are compared. Primitive types are compared using
==, object types are compared usingObjects.equals, single-dimension arrays are compared usingArrays.equals, and multi-dimensional arrays are compared usingArrays.deepEquals. If one of the comparisons fails,falseis returned. - Otherwise,
trueis returned.
If the class already has an
equalsmethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichequalsshould be generatedfields- fields to consider in theequalsmethod
- Reference equality is tested. If
-
generateEqualsAndHashCode
public static void generateEqualsAndHashCode(ClassCreator clazz, FieldDescriptor... fields)
Generates structuralequalsandhashCodemethods in givenclazz, based on givenfields. The generated code is similar to what IDEs would typically generate from a template. SeegenerateEquals(ClassCreator, FieldDescriptor...)for description of the generatedequalsmethod. ThehashCodemethod is generated like so:- If no field is given, 0 is returned.
- Otherwise, a result variable is allocated with initial value of 1.
- For each field, a hash code is computed. Hash code for primitive types
is computed using
Integer.hashCodeand equivalent methods, for object types usingObjects.hashCode, for single-dimension arrays usingArrays.hashCode, and for multi-dimensional arrays usingArrays.deepHashCode. Then, the result is updated like so:result = 31 * result + fieldHashCode. - At the end, the result is returned.
If the class already has an
equalsorhashCodemethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichequalsandhashCodeshould be generatedfields- fields to consider in theequalsandhashCodemethods
-
generateEqualsAndHashCode
public static void generateEqualsAndHashCode(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)
Generates structuralequalsandhashCodemethods in givenclazz, based on givenfields. The generated code is similar to what IDEs would typically generate from a template. SeegenerateEquals(ClassCreator, Collection)for description of the generatedequalsmethod. ThehashCodemethod is generated like so:- If no field is given, 0 is returned.
- Otherwise, a result variable is allocated with initial value of 1.
- For each field, a hash code is computed. Hash code for primitive types
is computed using
Integer.hashCodeand equivalent methods, for object types usingObjects.hashCode, for single-dimension arrays usingArrays.hashCode, and for multi-dimensional arrays usingArrays.deepHashCode. Then, the result is updated like so:result = 31 * result + fieldHashCode. - At the end, the result is returned.
If the class already has an
equalsorhashCodemethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichequalsandhashCodeshould be generatedfields- fields to consider in theequalsandhashCodemethods
-
generateNaiveToString
public static void generateNaiveToString(ClassCreator clazz, FieldDescriptor... fields)
Generates a naivetoStringmethods in givenclazz, based on givenfields. The generated code is similar to what IDEs would typically generate from a template:- An empty
StringBuilderis allocated. - Simple name of the class is appended.
- An opening parenthesis
'('is appended. - For each field, its name is appended, followed by the equals sign
'=', followed by the field value. Primitive types and object types are appended to theStringBuilderdirectly,Arrays.toStringis used for single-dimension arrays, andArrays.deepToStringfor multi-dimensional arrays. A comma followed by a space", "are appended between fields. - A closing parenthesis
')'is appended. - The
StringBuilder.toStringoutcome is returned.
If the class already has a
toStringmethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichtoStringshould be generatedfields- fields to consider in thetoStringmethods
- An empty
-
generateNaiveToString
public static void generateNaiveToString(ClassCreator clazz, java.util.Collection<FieldDescriptor> fields)
Generates a naivetoStringmethods in givenclazz, based on givenfields. The generated code is similar to what IDEs would typically generate from a template:- An empty
StringBuilderis allocated. - Simple name of the class is appended.
- An opening parenthesis
'('is appended. - For each field, its name is appended, followed by the equals sign
'=', followed by the field value. Primitive types and object types are appended to theStringBuilderdirectly,Arrays.toStringis used for single-dimension arrays, andArrays.deepToStringfor multi-dimensional arrays. A comma followed by a space", "are appended between fields. - A closing parenthesis
')'is appended. - The
StringBuilder.toStringoutcome is returned.
If the class already has a
toStringmethod, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.- Parameters:
clazz- class for whichtoStringshould be generatedfields- fields to consider in thetoStringmethods
- An empty
-
newHashMap
public static ResultHandle newHashMap(BytecodeCreator target)
Generates the bytecode that calls the no-argsHashMapconstructor.- Parameters:
target-- Returns:
- the result handle
-
newHashSet
public static ResultHandle newHashSet(BytecodeCreator target)
Generates the bytecode that calls the no-argsHashSetconstructor.- Parameters:
target-- Returns:
- the result handle
-
newArrayList
public static ResultHandle newArrayList(BytecodeCreator target)
Generates the bytecode that calls the no-argsArrayListconstructor.- Parameters:
target-- Returns:
- the result handle
-
newArrayList
public static ResultHandle newArrayList(BytecodeCreator target, int initialCapacity)
Generates the bytecode that calls theArrayListconstructor with the specified initial capacity.- Parameters:
target-- Returns:
- the result handle
-
-