Class Gizmo


  • public final class Gizmo
    extends java.lang.Object
    • Constructor Detail

      • Gizmo

        private Gizmo()
    • Method Detail

      • toString

        public static ResultHandle toString​(BytecodeCreator target,
                                            ResultHandle obj)
        Generates the bytecode that calls Object.toString().
        Parameters:
        target -
        obj -
        Returns:
        the result
      • systemOutPrintln

        public static void systemOutPrintln​(BytecodeCreator target,
                                            ResultHandle obj)
        Generates the bytecode that calls System.out.println(obj).
        Parameters:
        target -
        obj -
      • systemErrPrintln

        public static void systemErrPrintln​(BytecodeCreator target,
                                            ResultHandle obj)
        Generates the bytecode that calls System.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 a StringBuilder generator that helps to generate a chain of append calls and a final toString call.
         StringBuilderGenerator str = Gizmo.newStringBuilder(bytecode);
         str.append("constant");
         str.append(someResultHandle);
         ResultHandle result = str.callToString();
         
        The append method mimics the regular StringBuilder.append, so it accepts ResultHandles of all types for which StringBuilder has an overload:
        • primitive types
        • char[]
        • java.lang.String
        • java.lang.Object
        Notably, arrays except of char[] are appended using Object.toString and if Arrays.toString should be used, it must be generated manually.

        Methods for appending only a part of char[] or CharSequence are not provided. Other StringBuilder methods are not provided either. This is just a simple utility for generating code that concatenates strings, e.g. for implementing the toString method.

        Parameters:
        target -
        Returns:
        the generator
      • newStringBuilder

        public static Gizmo.StringBuilderGenerator newStringBuilder​(BytecodeCreator target,
                                                                    int capacity)
        Creates a StringBuilder generator that helps to generate a chain of append calls and a final toString call.
         StringBuilderGenerator str = Gizmo.newStringBuilder(bytecode, capacity);
         str.append("constant");
         str.append(someResultHandle);
         ResultHandle result = str.callToString();
         
        The append method mimics the regular StringBuilder.append, so it accepts ResultHandles of all types for which StringBuilder has an overload:
        • primitive types
        • char[]
        • java.lang.String
        • java.lang.Object
        Notably, arrays except of char[] are appended using Object.toString and if Arrays.toString should be used, it must be generated manually.

        Methods for appending only a part of char[] or CharSequence are not provided. Other StringBuilder methods are not provided either. This is just a simple utility for generating code that concatenates strings, e.g. for implementing the toString method.

        Parameters:
        target -
        capacity -
        Returns:
        the generator
      • generateEquals

        public static void generateEquals​(ClassCreator clazz,
                                          FieldDescriptor... fields)
        Generates a structural equals method in given clazz that compares given fields. The generated code is similar to what IDEs would typically generate from a template:
        1. Reference equality is tested. If this is idential to the other object, true is returned.
        2. Type of the other object is tested using instanceof. If the other object is not an instance of this class, false is returned.
        3. All fields are compared. Primitive types are compared using ==, object types are compared using Objects.equals, single-dimension arrays are compared using Arrays.equals, and multi-dimensional arrays are compared using Arrays.deepEquals. If one of the comparisons fails, false is returned.
        4. Otherwise, true is returned.

        If the class already has an equals method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which equals should be generated
        fields - fields to consider in the equals method
      • generateEquals

        public static void generateEquals​(ClassCreator clazz,
                                          java.util.Collection<FieldDescriptor> fields)
        Generates a structural equals method in given clazz that compares given fields. The generated code is similar to what IDEs would typically generate from a template:
        1. Reference equality is tested. If this is idential to the other object, true is returned.
        2. Type of the other object is tested using instanceof. If the other object is not an instance of this class, false is returned.
        3. All fields are compared. Primitive types are compared using ==, object types are compared using Objects.equals, single-dimension arrays are compared using Arrays.equals, and multi-dimensional arrays are compared using Arrays.deepEquals. If one of the comparisons fails, false is returned.
        4. Otherwise, true is returned.

        If the class already has an equals method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which equals should be generated
        fields - fields to consider in the equals method
      • generateEqualsAndHashCode

        public static void generateEqualsAndHashCode​(ClassCreator clazz,
                                                     FieldDescriptor... fields)
        Generates structural equals and hashCode methods in given clazz, based on given fields. The generated code is similar to what IDEs would typically generate from a template. See generateEquals(ClassCreator, FieldDescriptor...) for description of the generated equals method. The hashCode method is generated like so:
        1. If no field is given, 0 is returned.
        2. Otherwise, a result variable is allocated with initial value of 1.
        3. For each field, a hash code is computed. Hash code for primitive types is computed using Integer.hashCode and equivalent methods, for object types using Objects.hashCode, for single-dimension arrays using Arrays.hashCode, and for multi-dimensional arrays using Arrays.deepHashCode. Then, the result is updated like so: result = 31 * result + fieldHashCode.
        4. At the end, the result is returned.

        If the class already has an equals or hashCode method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which equals and hashCode should be generated
        fields - fields to consider in the equals and hashCode methods
      • generateEqualsAndHashCode

        public static void generateEqualsAndHashCode​(ClassCreator clazz,
                                                     java.util.Collection<FieldDescriptor> fields)
        Generates structural equals and hashCode methods in given clazz, based on given fields. The generated code is similar to what IDEs would typically generate from a template. See generateEquals(ClassCreator, Collection) for description of the generated equals method. The hashCode method is generated like so:
        1. If no field is given, 0 is returned.
        2. Otherwise, a result variable is allocated with initial value of 1.
        3. For each field, a hash code is computed. Hash code for primitive types is computed using Integer.hashCode and equivalent methods, for object types using Objects.hashCode, for single-dimension arrays using Arrays.hashCode, and for multi-dimensional arrays using Arrays.deepHashCode. Then, the result is updated like so: result = 31 * result + fieldHashCode.
        4. At the end, the result is returned.

        If the class already has an equals or hashCode method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which equals and hashCode should be generated
        fields - fields to consider in the equals and hashCode methods
      • generateNaiveToString

        public static void generateNaiveToString​(ClassCreator clazz,
                                                 FieldDescriptor... fields)
        Generates a naive toString methods in given clazz, based on given fields. The generated code is similar to what IDEs would typically generate from a template:
        1. An empty StringBuilder is allocated.
        2. Simple name of the class is appended.
        3. An opening parenthesis '(' is appended.
        4. 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 the StringBuilder directly, Arrays.toString is used for single-dimension arrays, and Arrays.deepToString for multi-dimensional arrays. A comma followed by a space ", " are appended between fields.
        5. A closing parenthesis ')' is appended.
        6. The StringBuilder.toString outcome is returned.

        If the class already has a toString method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which toString should be generated
        fields - fields to consider in the toString methods
      • generateNaiveToString

        public static void generateNaiveToString​(ClassCreator clazz,
                                                 java.util.Collection<FieldDescriptor> fields)
        Generates a naive toString methods in given clazz, based on given fields. The generated code is similar to what IDEs would typically generate from a template:
        1. An empty StringBuilder is allocated.
        2. Simple name of the class is appended.
        3. An opening parenthesis '(' is appended.
        4. 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 the StringBuilder directly, Arrays.toString is used for single-dimension arrays, and Arrays.deepToString for multi-dimensional arrays. A comma followed by a space ", " are appended between fields.
        5. A closing parenthesis ')' is appended.
        6. The StringBuilder.toString outcome is returned.

        If the class already has a toString method, an exception is thrown. If one of the fields doesn't belong to the class, an exception is thrown.

        Parameters:
        clazz - class for which toString should be generated
        fields - fields to consider in the toString methods
      • newHashMap

        public static ResultHandle newHashMap​(BytecodeCreator target)
        Generates the bytecode that calls the no-args HashMap constructor.
        Parameters:
        target -
        Returns:
        the result handle
      • newHashSet

        public static ResultHandle newHashSet​(BytecodeCreator target)
        Generates the bytecode that calls the no-args HashSet constructor.
        Parameters:
        target -
        Returns:
        the result handle
      • newArrayList

        public static ResultHandle newArrayList​(BytecodeCreator target)
        Generates the bytecode that calls the no-args ArrayList constructor.
        Parameters:
        target -
        Returns:
        the result handle
      • newArrayList

        public static ResultHandle newArrayList​(BytecodeCreator target,
                                                int initialCapacity)
        Generates the bytecode that calls the ArrayList constructor with the specified initial capacity.
        Parameters:
        target -
        Returns:
        the result handle