Creates the set method for the given field definition

@param cw
@param classDef
@param fieldDef
194214
protected void buildSetMethod(ClassVisitor cw,
                                  ClassDefinition classDef,
                                  FieldDefinition fieldDef) {
        MethodVisitor mv;
        // set method
        {
            mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
                    fieldDef.getWriteMethod(),
                    Type.getMethodDescriptor( Type.VOID_TYPE,
                            new Type[]{Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) )} ),
                    null,
                    null );
            mv.visitCode();
            Label l0 = null;
            if ( this.debug ) {
                l0 = new Label();
                mv.visitLabel( l0 );
            }
            mv.visitVarInsn( Opcodes.ALOAD, 0 );

            if ( classDef.isTraitable() && classDef.isFullTraiting() ) {
                updateTraitableField( mv, classDef, fieldDef );
            } else {
                mv.visitVarInsn( Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) ).getOpcode( Opcodes.ILOAD ), 1 );
            }

            if ( ! fieldDef.hasOverride() ) {
                mv.visitFieldInsn( Opcodes.PUTFIELD,
                                   BuildUtils.getInternalType( classDef.getClassName() ),
                                   fieldDef.getName(),
                                   BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) );
            } else {
                mv.visitMethodInsn( INVOKESPECIAL,
                                    BuildUtils.getInternalType( classDef.getSuperClass() ),
                                    BuildUtils.setterName( fieldDef.getName(), fieldDef.getOverriding() ),
                                    Type.getMethodDescriptor( Type.VOID_TYPE,
                                                              new Type[]{Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getOverriding()) )} ),
                                    false );
            }

            if (classDef.isReactive()) {
                mv.visitVarInsn( ALOAD, 0 );
                mv.visitMethodInsn( INVOKEVIRTUAL, BuildUtils.getInternalType( classDef.getClassName() ), "notifyModification", "()V", false );
            }

            mv.visitInsn( Opcodes.RETURN );
            Label l1 = null;
            if ( this.debug ) {
                l1 = new Label();
                mv.visitLabel( l1 );
                mv.visitLocalVariable( "this",
                        BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                        null,
                        l0,
                        l1,
                        0 );
            }
            mv.visitMaxs( 0, 0 );
            mv.visitEnd();
        }
    }
853945
public static FieldAnnotation fromBCELField(JavaClass jClass, Field field) {
        return new FieldAnnotation(jClass.getClassName(), field.getName(), field.getSignature(), field.isStatic());
    }
1044677
private JMethod getMethodsMap(MethodType type, JFieldVar field, ClassOutline co) {
        String getterBody = "return " + field.name() + ";";
        for (JMethod method : co.implClass.methods()) {
            String name = method.name();
            if (method.type().isPrimitive()) {
                if (MethodType.GETTER == type && (name.startsWith("is") || name.startsWith("get"))) {
                    JStatement o = (JStatement) method.body().getContents().get(0);
                    String s = getterBody(o);
                    if (s.trim().equals(getterBody)) {
                        return method;
                    }
                } else if (MethodType.SETTER == type && name.startsWith("set")) {
                    JStatement o = (JStatement) method.body().getContents().get(0);
                    String s = setterBody(o);
                    if (s.startsWith("this." + field.name() + " =")) {
                        return method;
                    }
                }
            }

        }
        throw new RuntimeException("Failed to find " + type + " for " + field.name() + ", disable XReplacePrimitives and report a bug");
    }
913984
public static Map<String, Field> getDeepDeclaredFieldMap(Class c)
    {
        Map<String, Field> fieldMap = new HashMap<>();
        Collection<Field> fields = getDeepDeclaredFields(c);
        for (Field field : fields)
        {
            String fieldName = field.getName();
            if (fieldMap.containsKey(fieldName))
            {   // Can happen when parent and child class both have private field with same name
                fieldMap.put(field.getDeclaringClass().getName() + '.' + fieldName, field);
            }
            else
            {
                fieldMap.put(fieldName, field);
            }
        }

        return fieldMap;
    }
507137
public void addFieldDefinition(FieldDefinition fieldDef) {
        // Assure field name is unique and add the definition to the map.
        Utils.require(!Utils.isEmpty(fieldDef.getName()), "Field name is required");
        Utils.require(!m_fieldDefMap.containsKey(fieldDef.getName()),
                      "Field names must be unique: " + fieldDef.getName());
        m_fieldDefMap.put(fieldDef.getName(), fieldDef);
        fieldDef.setTableDefinition(this);

        // Recurse to nested fields, if any.
        if (fieldDef.isGroupField()) {
            for (FieldDefinition nestedFieldDef : fieldDef.getNestedFields()) {
                addFieldDefinition(nestedFieldDef);
            }
        }
    }
521641
private static void build3ArgConstructor(final Class< ? > superClazz,
                                             final String className,
                                             final ClassWriter cw) {
        MethodVisitor mv;
        {
            mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
                                 "<init>",
                                 Type.getMethodDescriptor( Type.VOID_TYPE,
                                                           Type.getType( int.class ), Type.getType( Class.class ), Type.getType( ValueType.class ) ),
                                 null,
                                 null );
            mv.visitCode();
            final Label l0 = new Label();
            mv.visitLabel( l0 );
            mv.visitVarInsn( Opcodes.ALOAD,
                             0 );
            mv.visitVarInsn( Opcodes.ILOAD,
                             1 );
            mv.visitVarInsn( Opcodes.ALOAD,
                             2 );
            mv.visitVarInsn( Opcodes.ALOAD,
                             3 );
            mv.visitMethodInsn( Opcodes.INVOKESPECIAL,
                                Type.getInternalName( superClazz ),
                                "<init>",
                                Type.getMethodDescriptor( Type.VOID_TYPE,
                                                          Type.getType( int.class ), Type.getType( Class.class ), Type.getType( ValueType.class ) ) );
            final Label l1 = new Label();
            mv.visitLabel( l1 );
            mv.visitInsn( Opcodes.RETURN );
            final Label l2 = new Label();
            mv.visitLabel( l2 );
            mv.visitLocalVariable( "this",
                                   "L" + className + ";",
                                   null,
                                   l0,
                                   l2,
                                   0 );
            mv.visitLocalVariable( "index",
                                   Type.getDescriptor( int.class ),
                                   null,
                                   l0,
                                   l2,
                                   1 );
            mv.visitLocalVariable( "fieldType",
                                   Type.getDescriptor( Class.class ),
                                   null,
                                   l0,
                                   l2,
                                   2 );
            mv.visitLocalVariable( "valueType",
                                   Type.getDescriptor( ValueType.class ),
                                   null,
                                   l0,
                                   l2,
                                   3 );
            mv.visitMaxs( 0,
                          0 );
            mv.visitEnd();
        }
    }
1148870
private String generateSerdesClassAndField(TreeLogger logger, TypeOracle oracle, SourceWriter w,
                                               JClassType type, Json annotation) {
        final String qualifiedSourceName = type.getQualifiedSourceName();
        final String fieldName = getFieldName(type);
        final String listWrapperTypeName = getListWrapperTypeName(type);
        final String setWrapperTypeName = getSetWrapperTypeName(type);
        final String listWrapperFactoryMethodName = factoryFieldName + "." + firstCharToLowerCase(listWrapperTypeName);
        final String setWrapperFactoryMethodName = factoryFieldName + "." + firstCharToLowerCase(setWrapperTypeName);

        final String serdesFieldName = fieldName + "Serdes";
        final String serdesTypeName = getTypeName(type) + "Serdes";

        // serializer field as anonymous class
        w.println("private static class %s extends JsonObjectSerdes<%s> implements HasImpl {", serdesTypeName,
                qualifiedSourceName);

        // static field for impl array
        final String autoBeanInstanceClass = factoryFieldName + "." + fieldName + "().getClass()";
        w.println("    private final Class[] IMPL = new Class[]{ %s };", autoBeanInstanceClass);

        // static field to content-types
        w.println("    private final String[] PATTERNS = new String[]{ %s };", asStringCsv(annotation.value()));
        w.println();

        // constructor
        w.println("    public %s() {", serdesTypeName);
        w.println("        super(%s.class);", qualifiedSourceName);
        w.println("    }");
        w.println();

        // mediaType
        w.println("    @Override");
        w.println("    public Class[] implTypes() {");
        w.println("        return IMPL;");
        w.println("    }");
        w.println();

        // mediaType
        w.println("    @Override");
        w.println("    public String[] mediaType() {");
        w.println("        return PATTERNS;");
        w.println("    }");
        w.println();

        // readJson - used when any of deserialize alternatives succeeded (see JsonObjectSerdes)
        // TODO: improve this by not requiring parsing the json to an js array and latter stringifying it (see below)
        // Here would be no-op
        w.println("    @Override");
        w.println("    public %s readJson(JsonRecordReader r, DeserializationContext ctx) {",
                qualifiedSourceName);
        w.println("        return AutoBeanCodex.decode(%s, %s.class, JsonObjectSerdes.stringify(r)).as();",
                factoryFieldName, qualifiedSourceName);
        w.println("    }");
        w.println();

        // writeJson - not used
        w.println("    @Override");
        w.println("    public void writeJson(%s o, JsonRecordWriter w, SerializationContext ctx) {",
                qualifiedSourceName);
        w.println("        return;");
        w.println("    }");
        w.println();

        // deserialize - deserialize single object using ObjectMapper
        w.println("    @Override");
        w.println("    public %s deserialize(String s, DeserializationContext ctx) {", qualifiedSourceName);
        w.println("        try {");
        w.println("            return AutoBeanCodex.decode(%s, %s.class, s).as();", factoryFieldName,
                qualifiedSourceName);
        w.println("        } catch (java.lang.Exception e) {");
        w.println("            throw new UnableToDeserializeException(\"The auto-generated AutoBean deserializer" +
                " failed to deserialize the response body to \" + ctx.getRequestedType().getName() + \".\", e);");
        w.println("        }");
        w.println("    }");
        w.println();

        // deserialize
        w.println("    @Override");
        w.println("    public <C extends Collection<%s>> C deserialize(Class<C> c, " +
                "String s, DeserializationContext ctx) {", qualifiedSourceName);
        w.println("        try {");
        w.println("            if (c == List.class || c == Collection.class)");
        w.println("                return (C) AutoBeanCodex.decode(%s, %s.class, " +
                "\"{\\\"result\\\":\" + s + \"}\").as().getResult();", factoryFieldName, listWrapperTypeName);
        w.println("            else if (c == Set.class)");
        w.println("                return (C) AutoBeanCodex.decode(%s, %s.class, " +
                "\"{\\\"result\\\":\" + s + \"}\").as().getResult();", factoryFieldName, setWrapperTypeName);
        w.println("            else");
        // TODO: improve this by not requiring parsing the json to an js array and latter stringifying it
        // An alternative would be manually traverse the json array and passing each json object to serialize method
        w.println("                return super.deserialize(c, s, ctx);");
        w.println("        } catch (java.lang.Exception e) {");
        w.println("            throw new UnableToDeserializeException(\"The auto-generated AutoBean deserializer" +
                " failed to deserialize the response body" +
                " to \" + c.getName() + \"<\" + ctx.getRequestedType().getName() + \">.\", e);");
        w.println("        }");
        w.println("    }");

        // serialize
        w.println("    @Override");
        w.println("    public String serialize(%s o, SerializationContext ctx) {", qualifiedSourceName);
        w.println("        try {");
        w.println("            return AutoBeanCodex.encode(AutoBeanUtils.getAutoBean(o)).getPayload();");
        w.println("        } catch (java.lang.Exception e) {");
        w.println("            throw new UnableToSerializeException(\"The auto-generated AutoBean serializer" +
                " failed to serialize the instance of \" + o.getClass().getName() + \" to JSON.\", e);");
        w.println("        }");
        w.println("    }");
        w.println();

        // serialize
        w.println("    @Override");
        w.println("    public String serialize(Collection<%s> c, SerializationContext ctx) {",
                qualifiedSourceName);
        w.println("        try {");
        w.println("            if (c instanceof List) {");
        w.println("                final AutoBean<%s> autoBean = %s();", listWrapperTypeName,
                listWrapperFactoryMethodName);
        w.println("                autoBean.as().setResult((List) c);");
        w.println("                final String json = AutoBeanCodex.encode(autoBean).getPayload();");
        w.println("                return json.substring(10, json.length() -1);");
        w.println("            }");
        w.println("            if (c instanceof Set) {");
        w.println("                final AutoBean<%s> autoBean = %s();", setWrapperTypeName,
                setWrapperFactoryMethodName);
        w.println("                autoBean.as().setResult((Set) c);");
        w.println("                final String json = AutoBeanCodex.encode(autoBean).getPayload();");
        w.println("                return json.substring(10, json.length() -1);");
        w.println("            }");
        w.println("            return super.serialize(c, ctx);");
        w.println("        } catch (java.lang.Exception e) {");
        w.println("            throw new UnableToSerializeException(\"The auto-generated AutoBean serializer" +
                " failed to serialize the instance of \" + c.getClass().getName() + \" to JSON.\", e);");
        w.println("        }");
        w.println("    }");

        // end anonymous class
        w.println("};");
        w.println();

        // serializer field as anonymous class
        w.println("private final %s %s = new %s();", serdesTypeName, serdesFieldName, serdesTypeName);
        w.println();

        return serdesFieldName;
    }
964057
public ClassBuilder<T> withField(String field, Class<?> fieldClass) {
		fields.put(field, fieldClass);
		return this;
	}
418042
public static void setFieldValue(final Class<?> source, final Object target, final String fieldName, final Object value) throws NoSuchFieldException
    {
        try
        {
            AccessController.doPrivileged(new PrivilegedExceptionAction<Void>()
            {
                @Override
                public Void run() throws Exception
                {
                    Field field = source.getDeclaredField(fieldName);
                    if(!field.isAccessible())
                    {
                        field.setAccessible(true);
                    }
                    field.set(target, value);
                    return null;
                }
            });
        }
        // Unwrap
        catch (final PrivilegedActionException pae)
        {
            final Throwable t = pae.getCause();
            // Rethrow
            if (t instanceof NoSuchFieldException)
            {
                throw (NoSuchFieldException) t;
            }
            else
            {
                // No other checked Exception thrown by Class.getConstructor
                try
                {
                    throw (RuntimeException) t;
                }
                // Just in case we've really messed up
                catch (final ClassCastException cce)
                {
                    throw new RuntimeException("Obtained unchecked Exception; this code should never be reached", t);
                }
            }
        }
    }
549927
public boolean isSet(_Fields field) {
    if (field == null) {
      throw new IllegalArgumentException();
    }

    switch (field) {
    case FULL_CLASS_NAME:
      return is_set_full_class_name();
    case ARGS_LIST:
      return is_set_args_list();
    }
    throw new IllegalStateException();
  }
351678
public static Field getDeclaredField(final Class cls, final String fieldName) {
		return getField(cls, fieldName, true);
	}
488104
public static Method getGetterMethod(Class<?> c, String field) {

        try {
            return c.getMethod(getMethodName(field, GET_METHOD_PREFIX), EMPTY_CLASS);
        } catch (NoSuchMethodException e) {
            try {
                return c.getMethod(getMethodName(field, IS_METHOD_PREFIX), EMPTY_CLASS);
            } catch (NoSuchMethodException e1) {
                //log.info("getter method not found : " + field);
                return null;
            }
        }
    }
401753
protected void buildConstructorWithFields(ClassVisitor cw,
                                              ClassDefinition classDef,
                                              Collection<FieldDefinition> fieldDefs) {


        Type[] params = new Type[fieldDefs.size()];
        int index = 0;
        for ( FieldDefinition field : fieldDefs ) {
            params[index++] = Type.getType( BuildUtils.getTypeDescriptor( field.getTypeName() ) );
        }

        MethodVisitor mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
                "<init>",
                Type.getMethodDescriptor( Type.VOID_TYPE,
                        params ),
                null,
                null );
        mv.visitCode();
        Label l0 = null;
        if ( this.debug ) {
            l0 = new Label();
            mv.visitLabel( l0 );
        }

        fieldConstructorStart( mv, classDef, fieldDefs );

        mv.visitInsn( Opcodes.RETURN );
        Label l1 = null;
        if ( this.debug ) {
            l1 = new Label();
            mv.visitLabel( l1 );
            mv.visitLocalVariable( "this",
                    BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                    null,
                    l0,
                    l1,
                    0 );
            for ( FieldDefinition field : classDef.getFieldsDefinitions() ) {
                Label l11 = new Label();
                mv.visitLabel( l11 );
                mv.visitLocalVariable( field.getName(),
                        BuildUtils.getTypeDescriptor( field.getTypeName() ),
                        null,
                        l0,
                        l1,
                        0 );
            }
        }
        mv.visitMaxs( 0,
                0 );
        mv.visitEnd();

    }
430729
protected void buildDefaultConstructor(ClassVisitor cw,
                                           ClassDefinition classDef) {


        MethodVisitor mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
                "<init>",
                Type.getMethodDescriptor( Type.VOID_TYPE,
                        new Type[]{} ),
                null,
                null );
        mv.visitCode();

        Label l0 = null;
        if ( this.debug ) {
            l0 = new Label();
            mv.visitLabel( l0 );
        }

        boolean hasObjects = defaultConstructorStart( mv, classDef );

        mv.visitInsn(Opcodes.RETURN);
        Label l1 = null;
        if ( this.debug ) {
            l1 = new Label();
            mv.visitLabel( l1 );
            mv.visitLocalVariable( "this",
                                   BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                                   null,
                                   l0,
                                   l1,
                                   0 );
        }
        mv.visitMaxs( 0, 0 );
        mv.visitEnd();

    }
629728
protected void buildGetMethod(ClassVisitor cw,
                                  ClassDefinition classDef,
                                  FieldDefinition fieldDef) {
        MethodVisitor mv;
        // Get method
        {
            mv = cw.visitMethod( Opcodes.ACC_PUBLIC,
                    fieldDef.getReadMethod(),
                    Type.getMethodDescriptor( Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) ),
                            new Type[]{} ),
                    null,
                    null );
            mv.visitCode();
            Label l0 = null;
            if ( this.debug ) {
                l0 = new Label();
                mv.visitLabel( l0 );
            }
            mv.visitVarInsn( Opcodes.ALOAD,
                    0 );
            if ( ! fieldDef.hasOverride() ) {
                mv.visitFieldInsn( Opcodes.GETFIELD,
                                   BuildUtils.getInternalType( classDef.getClassName() ),
                                   fieldDef.getName(),
                                   BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) );
                mv.visitInsn( Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getTypeName() ) ).getOpcode( Opcodes.IRETURN ) );
            } else {
                mv.visitMethodInsn( INVOKESPECIAL,
                                    BuildUtils.getInternalType( classDef.getSuperClass() ),
                                    BuildUtils.getterName( fieldDef.getName(), fieldDef.getOverriding() ),
                                    Type.getMethodDescriptor( Type.getType( BuildUtils.getTypeDescriptor( fieldDef.getOverriding() ) ), new Type[]{} ),
                                    false );
                mv.visitTypeInsn( CHECKCAST, BuildUtils.getInternalType( fieldDef.getTypeName() ) );
                mv.visitInsn( BuildUtils.returnType( fieldDef.getTypeName() ) );
            }

            Label l1 = null;
            if ( this.debug ) {
                l1 = new Label();
                mv.visitLabel( l1 );
                mv.visitLocalVariable( "this",
                        BuildUtils.getTypeDescriptor( classDef.getClassName() ),
                        null,
                        l0,
                        l1,
                        0 );
            }
            mv.visitMaxs( 0,
                    0 );
            mv.visitEnd();
        }
    }
222271
private void addTableFor(ClassDescriptorDef classDef)
    {
        String                  name     = classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE);
        TableDef                tableDef = getTable(name);
        FieldDescriptorDef      fieldDef;
        ReferenceDescriptorDef  refDef;
        CollectionDescriptorDef collDef;
        ColumnDef               columnDef;
        IndexDef                indexDef;

        if (tableDef == null)
        {
            tableDef = new TableDef(name);
            addTable(tableDef);
        }
        if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION))
        {
            tableDef.setProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION,
                                 classDef.getProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION));
        }
        if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION))
        {
            tableDef.setProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION,
                                 classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION));
        }
        for (Iterator fieldIt = classDef.getFields(); fieldIt.hasNext();)
        {
            fieldDef = (FieldDescriptorDef)fieldIt.next();
            if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false) ||
                fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_VIRTUAL_FIELD, false))
            {
                continue;
            }
            columnDef = addColumnFor(fieldDef, tableDef);
            if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDEXED, false))
            {
                // add the field to the default index
                indexDef = tableDef.getIndex(null);
                if (indexDef == null)
                {
                    indexDef = new IndexDef(null, false);
                    tableDef.addIndex(indexDef);
                }
                indexDef.addColumn(columnDef.getName());
            }
        }
        for (Iterator refIt = classDef.getReferences(); refIt.hasNext();)
        {
            refDef = (ReferenceDescriptorDef)refIt.next();
            if (!refDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false))
            {
                addForeignkeys(refDef, tableDef);
            }
        }
        for (Iterator collIt = classDef.getCollections(); collIt.hasNext();)
        {
            collDef = (CollectionDescriptorDef)collIt.next();
            if (!collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false))
            {
                if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE))
                {
                    addIndirectionTable(collDef);
                }
                else
                {
                    addForeignkeys(collDef, tableDef);
                }
            }
        }
        for (Iterator indexIt = classDef.getIndexDescriptors(); indexIt.hasNext();)
        {
            addIndex((IndexDescriptorDef)indexIt.next(), tableDef);
        }
    }
1051432
private static <T> Method tryGetSetMethod(Class<T> clazz, Field field, String methodName) {

        try {
            return clazz.getDeclaredMethod(methodName, field.getType());
        } catch (Exception e) {

            return null;
        }

    }