Creates a new field for a dynamic class

@param field      name of field
@param fieldClass type of field
@return changed AsmFunctionFactory
964057
public ClassBuilder<T> withField(String field, Class<?> fieldClass) {
		fields.put(field, fieldClass);
		return this;
	}
1216202
public static <T> RuntimeTypeAdapterFactory<T> of(Class<T> baseType, String typeFieldName, boolean maintainType) {
    return new RuntimeTypeAdapterFactory<T>(baseType, typeFieldName, maintainType);
  }
599415
public CMAContentType addField(CMAField field) {
    if (fields == null) {
      fields = new ArrayList<CMAField>();
    }

    fields.add(field);
    return this;
  }
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;
    }
212915
public static FieldNode getField(ClassNode classNode, String fieldName) {
        ClassNode node = classNode;
        Set<String> visited = new HashSet<String>();
        while (node != null) {
            FieldNode fn = node.getDeclaredField(fieldName);
            if (fn != null) return fn;
            ClassNode[] interfaces = node.getInterfaces();
            for (ClassNode iNode : interfaces) {
                if (visited.contains(iNode.getName())) continue;
                FieldNode ifn = getField(iNode, fieldName);
                visited.add(iNode.getName());
                if (ifn != null) return ifn;
            }
            node = node.getSuperClass();
        }
        return null;
    }
818328
public static FieldBuilder getInstance(Context context,
            TypeElement typeElement,
            FieldWriter writer) {
        return new FieldBuilder(context, typeElement, writer);
    }
1353176
public FieldType<BeanType<T>> getOrCreateField()
   {
      List<Node> nodeList = childNode.get("field");
      if (nodeList != null &&  nodeList.size() > 0)
      {
         return new FieldTypeImpl<BeanType<T>>(this, "field", childNode, nodeList.get(0));
      }
      return createField();
   }
141777
public static Class[] getGenericTypes(Field field) {
        try {
            ParameterizedType type = (ParameterizedType) field.getGenericType();
            Type[] types = type.getActualTypeArguments();

            Class<?>[] res = new Class<?>[types.length];

            for (int i = 0; i < types.length; i++) {
                res[i] = (Class<?>) types[i];
            }

            return res;

        } catch (ClassCastException e) {
            return new Class<?>[] { (Class<?>) field.getGenericType() };
        }
    }
916271
def field_factory(base_class):
    
    from .fields import TranslationField

    class TranslationFieldField(TranslationField, base_class):
        pass

    TranslationFieldField.__name__ = "Translation%s" % base_class.__name__

    return TranslationFieldField
20004
public CustomField getCustomField(FieldType field)
   {
      CustomField result = m_configMap.get(field);
      if (result == null)
      {
         result = new CustomField(field, this);
         m_configMap.put(field, result);
      }
      return result;
   }
967838
public static <F extends PField> MappedField<F> withColumn(F field) {
        return new MappedField<>(field.getName(), field);
    }
613848
public static Class<?> getParameterTypeOfRepeatedField(Field field) {
    Class<?> componentType = null;
    if (isCollection(field.getType())) {
      validateCollection(field);
      Type parameterType = getParameterType((ParameterizedType) field.getGenericType());
      validateNestedParameterizedType(parameterType);
      componentType = (Class<?>) parameterType;
    } else if (field.getType().isArray()) {
      componentType = field.getType().getComponentType();
      return componentType;
    } else {
      throw new IllegalArgumentException("Unsupported repeated type " + field.getType()
          + " Allowed repeated fields are Collection<T> and arrays");
    }
    validateNestedRepeatedType(componentType, field);
    return componentType;
  }
1301075
public static final <T> Field getField(Class<T> cls, String fieldName) {
        try {
            Field f = cls.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (SecurityException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            if (cls.getSuperclass() != null) {
                return getField(cls.getSuperclass(), fieldName);
            }
            throw new RuntimeException(e);
        }
    }
4362
public static Field getField(Class cls, String fieldname) throws NoSuchFieldException
    {
        while (true)
        {
            try
            {
                return cls.getDeclaredField(fieldname);
            }
            catch (NoSuchFieldException ex)
            {
                cls = cls.getSuperclass();
                if (Object.class.equals(cls))
                {
                    throw ex;
                }
            }
            catch (SecurityException ex)
            {
                throw new IllegalArgumentException(ex);
            }
        }
    }
395332
@Nonnull
    public BugInstance addField(FieldDescriptor fieldDescriptor) {
        FieldAnnotation fieldAnnotation = FieldAnnotation.fromFieldDescriptor(fieldDescriptor);
        add(fieldAnnotation);
        return this;
    }
1146614
public static AnnotationTypeFieldBuilder getInstance(
            Context context, TypeElement typeElement,
            AnnotationTypeFieldWriter writer) {
        return new AnnotationTypeFieldBuilder(context, typeElement,
                    writer, VisibleMemberMap.Kind.ANNOTATION_TYPE_FIELDS);
    }
369923
private Optional<PositionSetter> createField(final Class<?> beanClass, final String fieldName) {
        
        final String positionFieldName = fieldName + "Position";
        
        final Field positionField;
        try {
            positionField = beanClass.getDeclaredField(positionFieldName);
            positionField.setAccessible(true);
            
        } catch (NoSuchFieldException | SecurityException e) {
            return Optional.empty();
        }
        
        if(positionField.getType().equals(CellPosition.class)) {
            
            return Optional.of(new PositionSetter() {
                
                @Override
                public void set(final Object beanObj, final CellPosition position) {
                    ArgUtils.notNull(beanObj, "beanObj");
                    ArgUtils.notNull(position, "position");
                    
                    try {
                        positionField.set(beanObj, position);
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        throw new RuntimeException("fail access position field.", e);
                    }
                }
            });
            
        } else if(positionField.getType().equals(Point.class)) {
            
            return Optional.of(new PositionSetter() {
                
                @Override
                public void set(final Object beanObj, final CellPosition position) {
                    ArgUtils.notNull(beanObj, "beanObj");
                    ArgUtils.notNull(position, "position");
                    
                    try {
                        positionField.set(beanObj, position.toPoint());
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        throw new RuntimeException("fail access position field.", e);
                    }
                }
            });
            
        } else if(positionField.getType().equals(org.apache.poi.ss.util.CellAddress.class)) {
            
            return Optional.of(new PositionSetter() {
                
                @Override
                public void set(final Object beanObj, final CellPosition position) {
                    ArgUtils.notNull(beanObj, "beanObj");
                    ArgUtils.notNull(position, "position");
                    
                    try {
                        positionField.set(beanObj, position.toCellAddress());
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        throw new RuntimeException("fail access position field.", e);
                    }
                }
            });
        
        }
        
        return Optional.empty();
    }