Example usage for java.lang.reflect Field getModifiers

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

Introduction

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

Prototype

public int getModifiers() 

Source Link

Document

Returns the Java language modifiers for the field represented by this Field object, as an integer.

Usage

From source file:org.apache.syncope.client.console.panels.RelationshipTypesPanel.java

@Override
protected List<IColumn<RelationshipTypeTO, String>> getColumns() {

    final List<IColumn<RelationshipTypeTO, String>> columns = new ArrayList<>();

    for (Field field : RelationshipTypeTO.class.getDeclaredFields()) {
        if (field != null && !Modifier.isStatic(field.getModifiers())) {
            final String fieldName = field.getName();
            if (field.getType().isArray() || Collection.class.isAssignableFrom(field.getType())
                    || Map.class.isAssignableFrom(field.getType())) {

                columns.add(new PropertyColumn<>(new ResourceModel(field.getName()), field.getName()));
            } else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
                columns.add(new BooleanPropertyColumn<>(new ResourceModel(field.getName()), field.getName(),
                        field.getName()));
            } else {
                columns.add(new PropertyColumn<RelationshipTypeTO, String>(new ResourceModel(field.getName()),
                        field.getName(), field.getName()) {

                    private static final long serialVersionUID = -6902459669035442212L;

                    @Override//from  w  w w .ja v  a 2 s.co  m
                    public String getCssClass() {
                        String css = super.getCssClass();
                        if ("key".equals(fieldName)) {
                            css = StringUtils.isBlank(css) ? "col-xs-1" : css + " col-xs-1";
                        }
                        return css;
                    }
                });
            }
        }
    }

    return columns;
}

From source file:ClassFigure.java

public ClassFigure(Class cls) {
    setLayoutManager(new ToolbarLayout());
    setBorder(new LineBorder(ColorConstants.black));
    setBackgroundColor(ColorConstants.yellow);
    setOpaque(true);//  ww w.j  a v a2s  .  c  om

    for (int i = 0; i < keys.length; i++)
        registry.put(keys[i], ImageDescriptor.createFromFile(null, "icons/java/" + keys[i] + ".gif"));

    Label title = new Label(cls.getName(), registry.get(KEY_CLASS));
    add(title);
    add(fieldBox);
    add(methodBox);

    // fields.
    Field[] fields = cls.getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        Field field = fields[i];
        Image image = null;
        if (Modifier.isPublic(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PUBLIC);
        } else if (Modifier.isProtected(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PROTECTED);
        } else if (Modifier.isPrivate(field.getModifiers())) {
            image = registry.get(KEY_FIELD_PRIVATE);
        } else {
            image = registry.get(KEY_FIELD_DEFAULT);
        }
        fieldBox.add(new Label(fields[i].getName(), image));
    }

    // fields.
    Method[] methods = cls.getDeclaredMethods();
    for (int i = 0; i < methods.length; i++) {
        Method method = methods[i];
        Image image = null;
        if (Modifier.isPublic(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PUBLIC);
        } else if (Modifier.isProtected(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PROTECTED);
        } else if (Modifier.isPrivate(method.getModifiers())) {
            image = registry.get(KEY_METHOD_PRIVATE);
        } else {
            image = registry.get(KEY_METHOD_DEFAULT);
        }
        methodBox.add(new Label(methods[i].getName(), image));
    }

}

From source file:org.apromore.test.heuristic.JavaBeanHeuristic.java

/**
 * Checks that a Javabean matches a set of rules in terms of its structure and behaviour. Given a class representing a Javabean, run the following
 * checks. <ol> <li>The class has a public no-arg constructor</li> <li>For each field declared in the class, a public getter and setter
 * exists</li> <li>Set the value using the setter, then ensure that the getter returns the same object</li> </ol>
 *
 * @param clazz the class to check./* w w  w  .  j  a  v a  2 s  . c  om*/
 * @param excludes any field names that should be excluded from the check.
 */
public void checkJavaBeanProperties(Class clazz, String... excludes) {
    Field[] fields = clazz.getDeclaredFields();
    Object bean = newInstance(clazz);
    List<String> excludeList = Arrays.asList(excludes);
    for (Field field : fields) {
        if (excludeList.contains(field.getName())) {
            continue;
        }

        int modifiers = field.getModifiers();
        if (!(Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers))) {
            processField(field, bean);
        }
    }
}

From source file:org.apache.zeppelin.service.ShiroAuthenticationServiceTest.java

private void setFinalStatic(Field field, Object newValue) throws NoSuchFieldException, IllegalAccessException {
    field.setAccessible(true);//  w  w  w  .j  a v  a 2s .com
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);
    modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    field.set(null, newValue);
}

From source file:de.taimos.dvalin.interconnect.core.spring.InterconnectBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {//from  w  w  w .j a v a2  s.  c om
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(Interconnect.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException(
                            "@Interconnect annotation is not supported on static fields");
                }
                currElements.add(new InterconnectElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(Interconnect.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@Interconnect annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@Interconnect annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new InterconnectElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}

From source file:org.apache.nifi.controller.MonitorMemoryTest.java

private CapturingLogger wrapAndReturnCapturingLogger() throws Exception {
    Field loggerField = MonitorMemory.class.getDeclaredField("logger");
    Field modifiersField = Field.class.getDeclaredField("modifiers");
    modifiersField.setAccessible(true);/*  ww w.  jav  a  2  s .co  m*/
    modifiersField.setInt(loggerField, loggerField.getModifiers() & ~Modifier.FINAL);

    loggerField.setAccessible(true);
    CapturingLogger capturingLogger = new CapturingLogger((Logger) loggerField.get(null));
    loggerField.set(null, capturingLogger);
    return capturingLogger;
}

From source file:com.ls.http.base.handler.MultipartRequestHandler.java

private void formMultipartEntityObject(Object source) {
    Class<?> currentClass = source.getClass();
    while (!Object.class.equals(currentClass)) {
        Field[] fields = currentClass.getDeclaredFields();
        for (int counter = 0; counter < fields.length; counter++) {
            Field field = fields[counter];
            Expose expose = field.getAnnotation(Expose.class);
            if (expose != null && !expose.deserialize() || Modifier.isTransient(field.getModifiers())) {
                continue;// We don't have to copy ignored fields.
            }/* www.j av a2  s. c o m*/
            field.setAccessible(true);
            Object value;

            String name;
            SerializedName serializableName = field.getAnnotation(SerializedName.class);
            if (serializableName != null) {
                name = serializableName.value();
            } else {
                name = field.getName();
            }
            try {
                value = field.get(source);
                addEntity(name, value);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        currentClass = currentClass.getSuperclass();
    }
    httpentity = entity.build();
}

From source file:eu.stratosphere.api.common.operators.util.UserCodeObjectWrapper.java

public UserCodeObjectWrapper(T userCodeObject) {
    Preconditions.checkArgument(userCodeObject instanceof Serializable,
            "User code object is not serializable: " + userCodeObject.getClass());
    this.userCodeObject = userCodeObject;
    // Remove unserializable objects from the user code object as well as from outer objects
    Object current = userCodeObject;
    try {/*from  w  w w.j  a  v  a 2s . c om*/
        while (null != current) {
            Object newCurrent = null;
            for (Field f : current.getClass().getDeclaredFields()) {
                f.setAccessible(true);

                if (f.getName().contains("$outer")) {
                    newCurrent = f.get(current);

                }

                if (!Modifier.isStatic(f.getModifiers()) && f.get(current) != null
                        && !(f.get(current) instanceof Serializable)) {
                    throw new RuntimeException("User code object " + userCodeObject
                            + " contains non-serializable field " + f.getName() + " = " + f.get(current));
                }
            }
            current = newCurrent;
        }
    } catch (IllegalAccessException e) {
        // this cannot occur since we call setAccessible(true)
        e.printStackTrace();
    }

}

From source file:de.taimos.dvalin.test.jaxrs.TestProxyBeanPostProcessor.java

private InjectionMetadata buildResourceMetadata(Class<?> clazz) {
    LinkedList<InjectionMetadata.InjectedElement> elements = new LinkedList<InjectionMetadata.InjectedElement>();
    Class<?> targetClass = clazz;

    do {/*from   ww  w .j a v  a2s  .  co  m*/
        LinkedList<InjectionMetadata.InjectedElement> currElements = new LinkedList<InjectionMetadata.InjectedElement>();
        for (Field field : targetClass.getDeclaredFields()) {
            if (field.isAnnotationPresent(TestProxy.class)) {
                if (Modifier.isStatic(field.getModifiers())) {
                    throw new IllegalStateException("@TestProxy annotation is not supported on static fields");
                }
                currElements.add(new TestProxyElement(field, null));
            }
        }
        for (Method method : targetClass.getDeclaredMethods()) {
            Method bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
            if (!BridgeMethodResolver.isVisibilityBridgeMethodPair(method, bridgedMethod)) {
                continue;
            }
            if (method.equals(ClassUtils.getMostSpecificMethod(method, clazz))) {
                if (bridgedMethod.isAnnotationPresent(TestProxy.class)) {
                    if (Modifier.isStatic(method.getModifiers())) {
                        throw new IllegalStateException(
                                "@TestProxy annotation is not supported on static methods");
                    }
                    if (method.getParameterTypes().length != 1) {
                        throw new IllegalStateException(
                                "@TestProxy annotation requires a single-arg method: " + method);
                    }
                    PropertyDescriptor pd = BeanUtils.findPropertyForMethod(bridgedMethod, clazz);
                    currElements.add(new TestProxyElement(method, pd));
                }
            }
        }
        elements.addAll(0, currElements);
        targetClass = targetClass.getSuperclass();
    } while ((targetClass != null) && (targetClass != Object.class));

    return new InjectionMetadata(clazz, elements);
}

From source file:com.netflix.bdp.inviso.history.TraceJobHistoryLoader.java

private void handleJobEvent(HistoryEvent event) throws IllegalArgumentException, IllegalAccessException,
        NoSuchMethodException, InvocationTargetException {
    for (Field f : event.getDatum().getClass().getFields()) {
        f.setAccessible(true);// ww w.j a v  a2 s.  com

        if (Modifier.isStatic(f.getModifiers())) {
            continue;
        }

        String name = f.getName();
        Object value = f.get(event.getDatum());

        if (skipElements.contains(name)) {
            continue;
        }

        if (value instanceof CharSequence) {
            value = value.toString();
        }

        if (value == null || value.toString().trim().isEmpty()) {
            continue;
        }

        if (COUNTER_TAGS.containsKey(name)) {
            Method m = event.getClass().getDeclaredMethod(COUNTER_TAGS.get(name), new Class[0]);
            m.setAccessible(true);

            Counters counters = (Counters) m.invoke(event, new Object[0]);
            value = handleCounterEntries(counters);
        }

        job.put(name, value);
    }
}