Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.operator.aggregation.state;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.byteCode.Access.FINAL;
 import static com.facebook.presto.byteCode.Access.PRIVATE;
 import static com.facebook.presto.byteCode.Access.PUBLIC;
 import static com.facebook.presto.byteCode.Access.STATIC;
 import static com.facebook.presto.byteCode.Access.a;
 import static com.facebook.presto.byteCode.NamedParameterDefinition.arg;
 import static com.facebook.presto.byteCode.ParameterizedType.type;
 import static com.facebook.presto.spi.type.BigintType.BIGINT;
 import static com.facebook.presto.spi.type.BooleanType.BOOLEAN;
 import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
 import static com.facebook.presto.spi.type.VarcharType.VARCHAR;
 import static com.facebook.presto.sql.gen.Bootstrap.BOOTSTRAP_METHOD;
 import static com.facebook.presto.sql.gen.CompilerUtils.defineClass;
 import static com.facebook.presto.sql.gen.CompilerUtils.makeClassName;
 import static com.facebook.presto.sql.gen.SqlTypeByteCodeExpression.constantType;
 import static com.google.common.base.CaseFormat.LOWER_CAMEL;
 import static com.google.common.base.CaseFormat.UPPER_CAMEL;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class StateCompiler
 {
     private static Class<?> getBigArrayType(Class<?> type)
     {
         if (type.equals(long.class)) {
             return LongBigArray.class;
         }
         if (type.equals(byte.class)) {
             return ByteBigArray.class;
         }
         if (type.equals(double.class)) {
             return DoubleBigArray.class;
         }
         if (type.equals(boolean.class)) {
             return BooleanBigArray.class;
         }
         if (type.equals(Slice.class)) {
             return SliceBigArray.class;
         }
         // TODO: support more reference types
         throw new IllegalArgumentException("Unsupported type: " + type.getName());
     }
 
     public <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazz)
     {
         return generateStateSerializer(clazznew DynamicClassLoader(clazz.getClassLoader()));
     }
 
     public <T> AccumulatorStateSerializer<T> generateStateSerializer(Class<T> clazzDynamicClassLoader classLoader)
     {
        AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
        if (metadata != null && metadata.stateSerializerClass() != void.class) {
            try {
                return (AccumulatorStateSerializer<T>) metadata.stateSerializerClass().getConstructor().newInstance();
            }
                throw Throwables.propagate(e);
            }
        }
        ClassDefinition definition = new ClassDefinition(
                a(),
                makeClassName(clazz.getSimpleName() + "Serializer"),
                type(Object.class),
                type(AccumulatorStateSerializer.class));
        CallSiteBinder callSiteBinder = new CallSiteBinder();
        // Generate constructor
        definition.declareDefaultConstructor(a());
        List<StateFieldfields = enumerateFields(clazz);
        generateGetSerializedType(definitionfieldscallSiteBinder);
        generateSerialize(definitionclazzfields);
        generateDeserialize(definitionclazzfields);
        Class<? extends AccumulatorStateSerializerserializerClass = defineClass(definitionAccumulatorStateSerializer.classcallSiteBinder.getBindings(), classLoader);
        try {
            return (AccumulatorStateSerializer<T>) serializerClass.newInstance();
        }
        catch (InstantiationException | IllegalAccessException e) {
            throw Throwables.propagate(e);
        }
    }
    private static void generateGetSerializedType(ClassDefinition definitionList<StateFieldfieldsCallSiteBinder callSiteBinder)
    {
        CompilerContext compilerContext = new CompilerContext();
        Block body = definition.declareMethod(compilerContexta(), "getSerializedType"type(Type.class)).getBody();
        Type type;
        if (fields.size() > 1) {
            type = ;
        }
        else {
            Class<?> stackType = fields.get(0).getType();
            if (stackType == long.class) {
                type = ;
            }
            else if (stackType == double.class) {
                type = ;
            }
            else if (stackType == boolean.class) {
                type = ;
            }
            else if (stackType == byte.class) {
                type = ;
            }
            else if (stackType == Slice.class) {
                type = ;
            }
            else {
                throw new IllegalArgumentException("Unsupported type: " + stackType);
            }
        }
        body.comment("return %s"type.getTypeSignature())
                .append(constantType(new CompilerContext(), callSiteBindertype))
                .retObject();
    }
    private static <T> AccumulatorStateMetadata getMetadataAnnotation(Class<T> clazz)
    {
        AccumulatorStateMetadata metadata = clazz.getAnnotation(AccumulatorStateMetadata.class);
        if (metadata != null) {
            return metadata;
        }
        // If the annotation wasn't found, then search the super classes
        for (Class<?> superInterface : clazz.getInterfaces()) {
            metadata = superInterface.getAnnotation(AccumulatorStateMetadata.class);
            if (metadata != null) {
                return metadata;
            }
        }
        return null;
    }
    private static <T> void generateDeserialize(ClassDefinition definitionClass<T> clazzList<StateFieldfields)
    {
        CompilerContext compilerContext = new CompilerContext();
        Block deserializerBody = definition.declareMethod(compilerContexta(), "deserialize"type(void.class), arg("block"com.facebook.presto.spi.block.Block.class), arg("index"int.class), arg("state"Object.class)).getBody();
        if (fields.size() == 1) {
            generatePrimitiveDeserializer(deserializerBodygetSetter(clazzfields.get(0)));
        }
        else {
            Variable slice = compilerContext.declareVariable(Slice.class"slice");
            deserializerBody.comment("Slice slice = block.getSlice(index, 0, block.getLength(index));")
                    .getVariable("block")
                    .getVariable("index")
                    .push(0)
                    .getVariable("block")
                    .getVariable("index")
                    .invokeInterface(com.facebook.presto.spi.block.Block.class"getLength"int.classint.class)
                    .invokeInterface(com.facebook.presto.spi.block.Block.class"getSlice"Slice.classint.classint.classint.class)
                    .putVariable(slice);
            for (StateField field : fields) {
                generateDeserializeFromSlice(deserializerBodyslicegetSetter(clazzfield), offsetOfField(fieldfields));
            }
        }
        deserializerBody.ret();
    }
    private static <T> void generateSerialize(ClassDefinition definitionClass<T> clazzList<StateFieldfields)
    {
        CompilerContext compilerContext = new CompilerContext();
        Block serializerBody = definition.declareMethod(compilerContexta(), "serialize"type(void.class), arg("state"Object.class), arg("out"BlockBuilder.class)).getBody();
        if (fields.size() == 1) {
            generatePrimitiveSerializer(serializerBodygetGetter(clazzfields.get(0)));
        }
        else {
            Variable slice = compilerContext.declareVariable(Slice.class"slice");
            int size = serializedSizeOf(clazz);
            serializerBody.comment("Slice slice = Slices.allocate(%d);"size)
                    .push(size)
                    .invokeStatic(Slices.class"allocate"Slice.classint.class)
                    .putVariable(slice);
            for (StateField field : fields) {
                generateSerializeFieldToSlice(serializerBodyslicegetGetter(clazzfield), offsetOfField(fieldfields));
            }
            serializerBody.comment("out.appendSlice(slice);")
                    .getVariable("out")
                    .getVariable(slice)
                    .push(0)
                    .push(size)
                    .invokeInterface(BlockBuilder.class"writeBytes"BlockBuilder.classSlice.classint.classint.class)
                    .invokeInterface(BlockBuilder.class"closeEntry"BlockBuilder.class)
                    .pop();
        }
        serializerBody.ret();
    }
    private static void generateSerializeFieldToSlice(Block bodyVariable sliceMethod getterint offset)
    {
        Method sliceSetterMethod = StateCompilerUtils.getSliceSetter(getter.getReturnType());
        body.comment("slice.%s(offset, state.%s())"sliceSetterMethod.getName(), getter.getName())
                .getVariable(slice)
                .push(offset)
                .getVariable("state")
                .invokeInterface(getter)
                .invokeStatic(sliceSetterMethod);
    }

    
Computes the byte offset to store this field at, when serializing it to a Slice
    private static int offsetOfField(StateField targetFieldList<StateFieldfields)
    {
        int offset = 0;
        for (StateField field : fields) {
            if (targetField.getName().equals(field.getName())) {
                break;
            }
            offset += field.sizeOfType();
        }
        return offset;
    }

    
Computes the size in bytes that this state will occupy, when serialized as a Slice
    private static int serializedSizeOf(Class<?> stateClass)
    {
        List<StateFieldfields = enumerateFields(stateClass);
        int size = 0;
        for (StateField field : fields) {
            size += field.sizeOfType();
        }
        return size;
    }
    private static Method getSetter(Class<?> clazzStateField field)
    {
        try {
            return clazz.getMethod(field.getSetterName(), field.getType());
        }
        catch (NoSuchMethodException e) {
            throw Throwables.propagate(e);
        }
    }
    private static Method getGetter(Class<?> clazzStateField field)
    {
        try {
            return clazz.getMethod(field.getGetterName());
        }
        catch (NoSuchMethodException e) {
            throw Throwables.propagate(e);
        }
    }
    private static void generatePrimitiveSerializer(Block bodyMethod getter)
    {
        Method method = StateCompilerUtils.getBlockBuilderAppend(getter.getReturnType());
        body.comment("out.%s(state.%s());"method.getName(), getter.getName())
                .getVariable("out")
                .getVariable("state")
                .invokeInterface(getter)
                .invokeStatic(method);
    }
    private static void generatePrimitiveDeserializer(Block bodyMethod setter)
    {
        Method method = StateCompilerUtils.getBlockGetter(setter.getParameterTypes()[0]);
        body.comment("state.%s(block.%s));"setter.getName(), method.getName())
                .getVariable("state")
                .getVariable("block")
                .getVariable("index")
                .invokeStatic(method)
                .invokeInterface(setter);
    }
    private static void generateDeserializeFromSlice(Block bodyVariable sliceMethod setterint offset)
    {
        Method sliceGetterMethod = StateCompilerUtils.getSliceGetter(setter.getParameterTypes()[0]);
        body.comment("state.%s(slice.%s(%d))"setter.getName(), sliceGetterMethod.getName(), offset)
                .getVariable("state")
                .getVariable(slice)
                .push(offset)
                .invokeStatic(sliceGetterMethod)
                .invokeInterface(setter);
    }
    public <T> AccumulatorStateFactory<T> generateStateFactory(Class<T> clazz)
    {
        return generateStateFactory(clazznew DynamicClassLoader(clazz.getClassLoader()));
    }
    public <T> AccumulatorStateFactory<T> generateStateFactory(Class<T> clazzDynamicClassLoader classLoader)
    {
        AccumulatorStateMetadata metadata = getMetadataAnnotation(clazz);
        if (metadata != null && metadata.stateFactoryClass() != void.class) {
            try {
                return (AccumulatorStateFactory<T>) metadata.stateFactoryClass().getConstructor().newInstance();
            }
                throw Throwables.propagate(e);
            }
        }
        Class<? extends T> singleStateClass = generateSingleStateClass(clazzclassLoader);
        Class<? extends T> groupedStateClass = generateGroupedStateClass(clazzclassLoader);
        ClassDefinition definition = new ClassDefinition(
                a(),
                makeClassName(clazz.getSimpleName() + "Factory"),
                type(Object.class),
                type(AccumulatorStateFactory.class));
        // Generate constructor
        definition.declareDefaultConstructor(a());
        // Generate single state creation method
        definition.declareMethod(a(), "createSingleState"type(Object.class))
                .getBody()
                .newObject(singleStateClass)
                .dup()
                .invokeConstructor(singleStateClass)
                .retObject();
        // Generate grouped state creation method
        definition.declareMethod(a(), "createGroupedState"type(Object.class))
                .getBody()
                .newObject(groupedStateClass)
                .dup()
                .invokeConstructor(groupedStateClass)
                .retObject();
        // Generate getters for state class
        definition.declareMethod(a(), "getSingleStateClass"type(Class.classsingleStateClass))
                .getBody()
                .push(singleStateClass)
                .retObject();
        definition.declareMethod(a(), "getGroupedStateClass"type(Class.classgroupedStateClass))
                .getBody()
                .push(groupedStateClass)
                .retObject();
        Class<? extends AccumulatorStateFactoryfactoryClass = defineClass(definitionAccumulatorStateFactory.classclassLoader);
        try {
            return (AccumulatorStateFactory<T>) factoryClass.newInstance();
        }
        catch (InstantiationException | IllegalAccessException e) {
            throw Throwables.propagate(e);
        }
    }
    private static <T> Class<? extends T> generateSingleStateClass(Class<T> clazzDynamicClassLoader classLoader)
    {
        ClassDefinition definition = new ClassDefinition(
                a(),
                makeClassName("Single" + clazz.getSimpleName()),
                type(Object.class),
                type(clazz));
        // Store class size in static field
        FieldDefinition classSize = definition.declareField(a(), "CLASS_SIZE"long.class);
        definition.getClassInitializer()
                .getBody()
                .comment("CLASS_SIZE = ClassLayout.parseClass(%s.class).instanceSize()"definition.getName())
                .push(definition.getType())
                .invokeStatic(ClassLayout.class"parseClass"ClassLayout.classClass.class)
                .invokeVirtual(ClassLayout.class"instanceSize"int.class)
                .intToLong()
                .putStaticField(classSize);
        // Add getter for class size
        definition.declareMethod(new CompilerContext(null), a(), "getEstimatedSize"type(long.class))
                .getBody()
                .getStaticField(classSize)
                .retLong();
        // Generate constructor
        Block constructor = definition.declareConstructor(a())
                .getBody()
                .pushThis()
                .invokeConstructor(Object.class);
        // Generate fields
        List<StateFieldfields = enumerateFields(clazz);
        for (StateField field : fields) {
            generateField(definitionconstructorfield);
        }
        constructor.ret();
        return defineClass(definitionclazzclassLoader);
    }
    private static <T> Class<? extends T> generateGroupedStateClass(Class<T> clazzDynamicClassLoader classLoader)
    {
        ClassDefinition definition = new ClassDefinition(
                a(),
                makeClassName("Grouped" + clazz.getSimpleName()),
                type(AbstractGroupedAccumulatorState.class),
                type(clazz),
                type(GroupedAccumulator.class));
        List<StateFieldfields = enumerateFields(clazz);
        // Create constructor
        Block constructor = definition.declareConstructor(
                a())
                .getBody()
                .pushThis()
                .invokeConstructor(AbstractGroupedAccumulatorState.class);
        // Create ensureCapacity
        Block ensureCapacity = definition.declareMethod(a(), "ensureCapacity"type(void.class), arg("size"long.class)).getBody();
        // Generate fields, constructor, and ensureCapacity
        List<FieldDefinitionfieldDefinitions = new ArrayList<>();
        for (StateField field : fields) {
            fieldDefinitions.add(generateGroupedField(definitionconstructorensureCapacityfield));
        }
        constructor.ret();
        ensureCapacity.ret();
        // Generate getEstimatedSize
        Block getEstimatedSize = definition.declareMethod(a(), "getEstimatedSize"type(long.class))
                .getBody()
                .comment("long size = 0;")
                .push(0L);
        for (FieldDefinition field : fieldDefinitions) {
            getEstimatedSize
                    .comment("size += %s.sizeOf();"field.getName())
                    .pushThis()
                    .getField(field)
                    .invokeVirtual(field.getType(), "sizeOf"type(long.class))
                    .longAdd();
        }
        getEstimatedSize.comment("return size;");
        getEstimatedSize.retLong();
        return defineClass(definitionclazzclassLoader);
    }
    private static void generateField(ClassDefinition definitionBlock constructorStateField stateField)
    {
        FieldDefinition field = definition.declareField(a(), .to(stateField.getName()) + "Value"stateField.getType());
        // Generate getter
        definition.declareMethod(a(), stateField.getGetterName(), type(stateField.getType()))
                .getBody()
                .pushThis()
                .getField(field)
                .ret(stateField.getType());
        // Generate setter
        definition.declareMethod(a(), stateField.getSetterName(), type(void.class), arg("value"stateField.getType()))
                .getBody()
                .pushThis()
                .getVariable("value")
                .putField(field)
                .ret();
        constructor.pushThis();
        pushInitialValue(constructorstateField);
        constructor.putField(field);
    }
    private static FieldDefinition generateGroupedField(ClassDefinition definitionBlock constructorBlock ensureCapacityStateField stateField)
    {
        Class<?> bigArrayType = getBigArrayType(stateField.getType());
        FieldDefinition field = definition.declareField(a(), .to(stateField.getName()) + "Values"bigArrayType);
        // Generate getter
        definition.declareMethod(a(), stateField.getGetterName(), type(stateField.getType()))
                .getBody()
                .comment("return field.get(getGroupId());")
                .pushThis()
                .getField(field)
                .pushThis()
                .invokeVirtual(AbstractGroupedAccumulatorState.class"getGroupId"long.class)
                .invokeVirtual(bigArrayType"get"stateField.getType(), long.class)
                .ret(stateField.getType());
        // Generate setter
        definition.declareMethod(a(), stateField.getSetterName(), type(void.class), arg("value"stateField.getType()))
                .getBody()
                .comment("return field.set(getGroupId(), value);")
                .pushThis()
                .getField(field)
                .pushThis()
                .invokeVirtual(AbstractGroupedAccumulatorState.class"getGroupId"long.class)
                .getVariable("value")
                .invokeVirtual(bigArrayType"set"void.classlong.classstateField.getType())
                .ret();
        ensureCapacity.pushThis()
                .getField(field)
                .getVariable("size")
                .invokeVirtual(field.getType(), "ensureCapacity"type(void.class), type(long.class));
        // Initialize field in constructor
        constructor.pushThis()
                .newObject(field.getType())
                .dup();
        pushInitialValue(constructorstateField);
        constructor.invokeConstructor(field.getType(), type(stateField.getType()));
        constructor.putField(field);
        return field;
    }
    private static void pushInitialValue(Block blockStateField stateField)
    {
        Object initialValue = stateField.getInitialValue();
        if (initialValue != null) {
            if (initialValue instanceof Number) {
                block.push((NumberinitialValue);
            }
            else if (initialValue instanceof Boolean) {
                block.push((booleaninitialValue);
            }
            else {
                throw new IllegalArgumentException("Unsupported initial value type: " + initialValue.getClass());
            }
        }
        else {
            block.pushJavaDefault(stateField.getType());
        }
    }

    
Enumerates all the fields in this state interface.

Parameters:
clazz a subclass of AccumulatorState
Returns:
list of state fields. Ordering is guaranteed to be stable, and have all primitive fields at the beginning.
    private static List<StateFieldenumerateFields(Class<?> clazz)
    {
        ImmutableList.Builder<StateFieldbuilder = ImmutableList.builder();
        final Set<Class<?>> primitiveClasses = ImmutableSet.<Class<?>>of(byte.classboolean.classlong.classdouble.class);
        Set<Class<?>> supportedClasses = ImmutableSet.<Class<?>>of(byte.classboolean.classlong.classdouble.classSlice.class);
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals("getEstimatedSize")) {
                continue;
            }
            if (method.getName().startsWith("get")) {
                Class<?> type = method.getReturnType();
                checkArgument(supportedClasses.contains(type), type.getName() + " is not supported");
                String name = method.getName().substring(3);
                builder.add(new StateField(nametypegetInitialValue(method)));
            }
            if (method.getName().startsWith("is")) {
                Class<?> type = method.getReturnType();
                checkArgument(type == boolean.class"Only boolean is support for 'is' methods");
                String name = method.getName().substring(2);
                builder.add(new StateField(nametypegetInitialValue(method), method.getName()));
            }
        }
        // We need this ordering because the serializer and deserializer are on different machines, and so the ordering of fields must be stable
        Ordering<StateFieldordering = new Ordering<StateField>()
        {
            @Override
            public int compare(StateField leftStateField right)
            {
                if (primitiveClasses.contains(left.getType()) && !primitiveClasses.contains(right.getType())) {
                    return -1;
                }
                if (primitiveClasses.contains(right.getType()) && !primitiveClasses.contains(left.getType())) {
                    return 1;
                }
                // If they're the category, just sort by name
                return left.getName().compareTo(right.getName());
            }
        };
        List<StateFieldfields = ordering.sortedCopy(builder.build());
        checkInterface(clazzfields);
        return fields;
    }
    private static Object getInitialValue(Method method)
    {
        Object value = null;
        for (Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof InitialLongValue) {
                checkArgument(value == null"%s has multiple initialValue annotations"method.getName());
                checkArgument(method.getReturnType() == long.class"%s does not return a long, but is annotated with @InitialLongValue"method.getName());
                value = ((InitialLongValueannotation).value();
            }
            else if (annotation instanceof InitialDoubleValue) {
                checkArgument(value == null"%s has multiple initialValue annotations"method.getName());
                checkArgument(method.getReturnType() == double.class"%s does not return a double, but is annotated with @InitialDoubleValue"method.getName());
                value = ((InitialDoubleValueannotation).value();
            }
            else if (annotation instanceof InitialBooleanValue) {
                checkArgument(value == null"%s has multiple initialValue annotations"method.getName());
                checkArgument(method.getReturnType() == boolean.class"%s does not return a boolean, but is annotated with @InitialBooleanValue"method.getName());
                value = ((InitialBooleanValueannotation).value();
            }
        }
        return value;
    }
    private static void checkInterface(Class<?> clazzList<StateFieldfields)
    {
        checkArgument(clazz.isInterface(), clazz.getName() + " is not an interface");
        Set<Stringsetters = new HashSet<>();
        Set<Stringgetters = new HashSet<>();
        Set<StringisGetters = new HashSet<>();
        Map<StringClass<?>> fieldTypes = new HashMap<>();
        for (StateField field : fields) {
            fieldTypes.put(field.getName(), field.getType());
        }
        for (Method method : clazz.getMethods()) {
            if (method.getName().equals("getEstimatedSize")) {
                checkArgument(method.getReturnType().equals(long.class), "getEstimatedSize must return long");
                checkArgument(method.getParameterTypes().length == 0, "getEstimatedSize may not have parameters");
                continue;
            }
            if (method.getName().startsWith("get")) {
                String name = method.getName().substring(3);
                checkArgument(fieldTypes.get(name).equals(method.getReturnType()),
                        "Expected %s to return type %s, but found %s"method.getName(), fieldTypes.get(name), method.getReturnType());
                checkArgument(method.getParameterTypes().length == 0, "Expected %s to have zero parameters"method.getName());
                getters.add(name);
            }
            else if (method.getName().startsWith("is")) {
                String name = method.getName().substring(2);
                checkArgument(fieldTypes.get(name) == boolean.class,
                        "Expected %s to have type boolean, but found %s"namefieldTypes.get(name));
                checkArgument(method.getParameterTypes().length == 0, "Expected %s to have zero parameters"method.getName());
                checkArgument(method.getReturnType() == boolean.class"Expected %s to return boolean"method.getName());
                isGetters.add(name);
            }
            else if (method.getName().startsWith("set")) {
                String name = method.getName().substring(3);
                checkArgument(method.getParameterTypes().length == 1, "Expected setter to have one parameter");
                checkArgument(fieldTypes.get(name).equals(method.getParameterTypes()[0]),
                        "Expected %s to accept type %s, but found %s"method.getName(), fieldTypes.get(name), method.getParameterTypes()[0]);
                checkArgument(getInitialValue(method) == null"initial value annotation not allowed on setter");
                checkArgument(method.getReturnType().equals(void.class), "%s may not return a value"method.getName());
                setters.add(name);
            }
            else {
                throw new IllegalArgumentException("Cannot generate implementation for method: " + method.getName());
            }
        }
        checkArgument(getters.size() + isGetters.size() == setters.size() && setters.size() == fields.size(), "Wrong number of getters/setters");
    }
    private static final class StateField
    {
        private final String name;
        private final String getterName;
        private final Class<?> type;
        private final Object initialValue;
        private StateField(String nameClass<?> typeObject initialValue)
        {
            this(nametypeinitialValue"get" + name);
        }
        private StateField(String nameClass<?> typeObject initialValueString getterName)
        {
            this. = checkNotNull(name"name is null");
            checkArgument(!name.isEmpty(), "name is empty");
            this. = checkNotNull(type"type is null");
            this. = checkNotNull(getterName"getterName is null");
            this. = initialValue;
        }
        public String getGetterName()
        {
            return ;
        }
        public String getSetterName()
        {
            return "set" + getName();
        }
        public String getName()
        {
            return ;
        }
        public Class<?> getType()
        {
            return ;
        }
        public int sizeOfType()
        {
            if (getType() == long.class) {
                return .;
            }
            else if (getType() == double.class) {
                return .;
            }
            else if (getType() == boolean.class || getType() == byte.class) {
                return .;
            }
            else {
                throw new IllegalArgumentException("Unsupported type: " + getType());
            }
        }
        public Object getInitialValue()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X