Example usage for java.lang.reflect Field isSynthetic

List of usage examples for java.lang.reflect Field isSynthetic

Introduction

In this page you can find the example usage for java.lang.reflect Field isSynthetic.

Prototype

public boolean isSynthetic() 

Source Link

Document

Returns true if this field is a synthetic field; returns false otherwise.

Usage

From source file:Spy.java

public static void main(String... args) throws Exception {
    Class<?> c = Class.forName("Spy");
    Field[] flds = c.getDeclaredFields();
    for (Field f : flds) {
        System.out.println(f.isSynthetic());

    }//ww w .j  ava 2s  .  c om
}

From source file:Spy.java

public static void main(String... args) {
    try {//from  w w w .j  av a 2  s  .co  m
        Class<?> c = Class.forName(args[0]);
        int searchMods = 0x0;
        for (int i = 1; i < args.length; i++) {
            searchMods |= modifierFromString(args[i]);
        }

        Field[] flds = c.getDeclaredFields();
        out.format("Fields in Class '%s' containing modifiers:  %s%n", c.getName(),
                Modifier.toString(searchMods));
        boolean found = false;
        for (Field f : flds) {
            int foundMods = f.getModifiers();
            // Require all of the requested modifiers to be present
            if ((foundMods & searchMods) == searchMods) {
                out.format("%-8s [ synthetic=%-5b enum_constant=%-5b ]%n", f.getName(), f.isSynthetic(),
                        f.isEnumConstant());
                found = true;
            }
        }

        if (!found) {
            out.format("No matching fields%n");
        }

        // production code should handle this exception more gracefully
    } catch (ClassNotFoundException x) {
        x.printStackTrace();
    }
}

From source file:Main.java

public static List<Field> getRealFields(Class objectClass) {
    List<Field> result = new LinkedList<Field>();
    for (Field item : objectClass.getDeclaredFields()) {
        if (!item.isSynthetic())
            result.add(item);//from   ww w  .  j  av  a  2 s  .c  o  m
    }
    return result;
}

From source file:springobjectmapper.ReflectionHelper.java

private static boolean isEntityField(Field field) {
    return !field.isSynthetic() && !Modifier.isStatic(field.getModifiers())
            && !Modifier.isTransient(field.getModifiers());
}

From source file:Main.java

public static boolean fieldIsInvalid(Field field) {
    return (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers()))
            || field.isSynthetic();
}

From source file:com.opencsv.bean.MappingUtils.java

/**
 * Determines which mapping strategy is appropriate for this bean.
 * The algorithm is:<ol>// ww  w.j  av  a2s  .co  m
 * <li>If annotations {@link CsvBindByPosition} or
 * {@link CsvCustomBindByPosition} are present,
 * {@link ColumnPositionMappingStrategy} is chosen.</li>
 * <li>Otherwise, {@link HeaderColumnNameMappingStrategy} is chosen. If
 * annotations are present, they will be used, otherwise the field names
 * will be used as the column names.</li></ol>
 * 
 * @param <T> The type of the bean for which the mapping strategy is sought
 * @param type The class of the bean for which the mapping strategy is sought
 * @return A functional mapping strategy for the bean in question
 */
public static <T> MappingStrategy<T> determineMappingStrategy(Class type) {
    // Check for annotations
    Field[] fields = FieldUtils.getAllFields(type);
    boolean positionAnnotationsPresent = false;
    for (Field field : fields) {
        if (field.isAnnotationPresent(CsvBindByPosition.class)
                || field.isAnnotationPresent(CsvCustomBindByPosition.class)) {
            positionAnnotationsPresent = true;
            break;
        }
        if (positionAnnotationsPresent) {
            break;
        }
    }

    // Set the mapping strategy according to what we've found.
    MappingStrategy<T> mappingStrategy;
    if (positionAnnotationsPresent) {
        ColumnPositionMappingStrategy<T> ms = new ColumnPositionMappingStrategy<T>();
        ms.setType(type);
        mappingStrategy = ms;
    } else {
        HeaderColumnNameMappingStrategy<T> ms = new HeaderColumnNameMappingStrategy<T>();
        ms.setType(type);

        // Ugly hack, but I have to get the field names into the stupid
        // strategy somehow.
        if (!ms.isAnnotationDriven()) {
            SortedSet<String> sortedFields = new TreeSet<String>();
            for (Field f : fields) {
                if (!f.isSynthetic()) { // Otherwise JaCoCo breaks tests
                    sortedFields.add(f.getName());
                }
            }
            String header = StringUtils.join(sortedFields, ',').concat("\n");
            try {
                ms.captureHeader(new CSVReader(new StringReader(header)));
                ms.findDescriptor(0);
            } catch (IOException e) {
                // Can't happen. It's a StringReader with a defined string.
            } catch (IntrospectionException e) {
                CsvBeanIntrospectionException csve = new CsvBeanIntrospectionException("");
                csve.initCause(e);
                throw csve;
            }
        }

        mappingStrategy = ms;
    }
    return mappingStrategy;
}

From source file:bigbluej.ReflectionUtils.java

public static SortedMap<String, Object> getFieldsAndValuesInSortedMap(Object object)
        throws IllegalAccessException {
    Validate.notNull(object);/*from  ww w.j  a  v a  2s . c  om*/
    SortedMap<String, Object> fieldAndValues = new TreeMap<>();
    for (Field field : object.getClass().getDeclaredFields()) {
        field.setAccessible(true);
        String name = field.getName();
        Object value = field.get(object);
        if (!field.isSynthetic() && value != null) {
            fieldAndValues.put(name, value);
        }
    }
    return fieldAndValues;
}

From source file:org.jspresso.framework.application.AbstractController.java

private static void appendToInternalActionState(Class<?> clazz, IAction action, Map<String, Object> state)
        throws IllegalAccessException {
    for (Field field : clazz.getDeclaredFields()) {
        if (!field.isSynthetic()) {
            field.setAccessible(true);//from   w w  w .j a  va2s.  com
            Object fieldValue = field.get(action);
            if (fieldValue instanceof IAction) {
                fieldValue = extractInternalActionState((IAction) fieldValue);
            }
            state.put(clazz.getName() + "." + field.getName(), fieldValue);
        }
    }
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz != null && !Object.class.equals(superClazz)) {
        appendToInternalActionState(superClazz, action, state);
    }
}

From source file:Main.java

public static int hashCode(Object target) {
    if (target == null)
        return 0;
    final int prime = 31;
    int result = 1;

    Class<?> current = target.getClass();
    do {/*from   w  w w  . j a  va  2 s  .c  o m*/
        for (Field f : current.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                    || f.isSynthetic()) {
                continue;
            }

            Object self;
            try {
                f.setAccessible(true);
                self = f.get(target);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
            if (self == null) {
                result = prime * result + 0;
            } else if (self.getClass().isArray()) {
                if (self.getClass().equals(boolean[].class)) {
                    result = prime * result + Arrays.hashCode((boolean[]) self);
                } else if (self.getClass().equals(char[].class)) {
                    result = prime * result + Arrays.hashCode((char[]) self);
                } else if (self.getClass().equals(byte[].class)) {
                    result = prime * result + Arrays.hashCode((byte[]) self);
                } else if (self.getClass().equals(short[].class)) {
                    result = prime * result + Arrays.hashCode((short[]) self);
                } else if (self.getClass().equals(int[].class)) {
                    result = prime * result + Arrays.hashCode((int[]) self);
                } else if (self.getClass().equals(long[].class)) {
                    result = prime * result + Arrays.hashCode((long[]) self);
                } else if (self.getClass().equals(float[].class)) {
                    result = prime * result + Arrays.hashCode((float[]) self);
                } else if (self.getClass().equals(double[].class)) {
                    result = prime * result + Arrays.hashCode((double[]) self);
                } else {
                    result = prime * result + Arrays.hashCode((Object[]) self);
                }
            } else {
                result = prime * result + self.hashCode();
            }

            System.out.println(f.getName() + ": " + result);
        }
        current = current.getSuperclass();
    } while (!Object.class.equals(current));

    return result;
}

From source file:Main.java

public static boolean equals(Object target, Object o) {
    if (target == o)
        return true;
    if (target == null || o == null)
        return false;
    if (!target.getClass().equals(o.getClass()))
        return false;

    Class<?> current = target.getClass();
    do {/*  w ww .ja  va  2  s  . co  m*/
        for (Field f : current.getDeclaredFields()) {
            if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())
                    || f.isSynthetic()) {
                continue;
            }

            Object self;
            Object other;
            try {
                f.setAccessible(true);
                self = f.get(target);
                other = f.get(o);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException(e);
            }
            if (self == null) {
                if (other != null)
                    return false;
            } else if (self.getClass().isArray()) {
                if (self.getClass().equals(boolean[].class)) {
                    if (!Arrays.equals((boolean[]) self, (boolean[]) other))
                        return false;
                } else if (self.getClass().equals(char[].class)) {
                    if (!Arrays.equals((char[]) self, (char[]) other))
                        return false;
                } else if (self.getClass().equals(byte[].class)) {
                    if (!Arrays.equals((byte[]) self, (byte[]) other))
                        return false;
                } else if (self.getClass().equals(short[].class)) {
                    if (!Arrays.equals((short[]) self, (short[]) other))
                        return false;
                } else if (self.getClass().equals(int[].class)) {
                    if (!Arrays.equals((int[]) self, (int[]) other))
                        return false;
                } else if (self.getClass().equals(long[].class)) {
                    if (!Arrays.equals((long[]) self, (long[]) other))
                        return false;
                } else if (self.getClass().equals(float[].class)) {
                    if (!Arrays.equals((float[]) self, (float[]) other))
                        return false;
                } else if (self.getClass().equals(double[].class)) {
                    if (!Arrays.equals((double[]) self, (double[]) other))
                        return false;
                } else {
                    if (!Arrays.equals((Object[]) self, (Object[]) other))
                        return false;
                }
            } else if (!self.equals(other)) {
                return false;
            }
        }
        current = current.getSuperclass();
    } while (!Object.class.equals(current));

    return true;
}