Example usage for java.lang.reflect Modifier isFinal

List of usage examples for java.lang.reflect Modifier isFinal

Introduction

In this page you can find the example usage for java.lang.reflect Modifier isFinal.

Prototype

public static boolean isFinal(int mod) 

Source Link

Document

Return true if the integer argument includes the final modifier, false otherwise.

Usage

From source file:com.googlecode.ddom.weaver.inject.InjectionPlugin.java

/**
 * Add the given binding. The binding will be configured with a {@link SingletonInjector} or a
 * {@link PrototypeInjector} depending on whether the injected class is a singleton as defined
 * in the Javadoc of the {@link SingletonInjector} class.
 * /*  w  w  w  .j a  va  2s  . c o m*/
 * @param fieldType
 *            the target field type
 * @param injectedClass
 *            the injected class or <code>null</code> to inject a <code>null</code> value
 */
public <T> void addBinding(Class<T> fieldType, Class<? extends T> injectedClass) {
    Injector injector;
    if (injectedClass == null) {
        injector = null;
    } else {
        Field instanceField;
        try {
            Field candidateField = injectedClass.getField("INSTANCE");
            int modifiers = candidateField.getModifiers();
            // Note: getField only returns public fields, so no need to check for that
            // modifier here
            if (!Modifier.isStatic(modifiers) || !Modifier.isFinal(modifiers)) {
                if (log.isWarnEnabled()) {
                    log.warn("Ignoring public INSTANCE field in " + injectedClass.getName()
                            + " that is not static final");
                }
                instanceField = null;
            } else if (!fieldType.isAssignableFrom(candidateField.getType())) {
                if (log.isWarnEnabled()) {
                    log.warn("Ignoring INSTANCE field of incompatible type in " + injectedClass.getName());
                }
                instanceField = null;
            } else {
                instanceField = candidateField;
            }
        } catch (NoSuchFieldException ex) {
            instanceField = null;
        }
        if (instanceField != null) {
            injector = new SingletonInjector(injectedClass.getName(), instanceField.getType().getName());
        } else {
            injector = new PrototypeInjector(injectedClass.getName());
        }
    }
    addBinding(fieldType.getName(), injector);
}

From source file:org.beangle.ems.dictionary.model.BaseCode.java

/**
 * ???/*from ww w .j a  v  a2 s  .c om*/
 * 
 * @return
 */
public boolean hasExtPros() {
    Field[] fields = getClass().getDeclaredFields();
    for (int i = 0; i < fields.length; i++) {
        if (!(Modifier.isFinal(fields[i].getModifiers()) || Modifier.isStatic(fields[i].getModifiers()))) {
            return true;
        }
    }
    return false;
}

From source file:org.springframework.cloud.sleuth.instrument.async.ExecutorBeanPostProcessor.java

@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
    if (bean instanceof Executor && !(bean instanceof ThreadPoolTaskExecutor)) {
        Method execute = ReflectionUtils.findMethod(bean.getClass(), "execute", Runnable.class);
        boolean methodFinal = Modifier.isFinal(execute.getModifiers());
        boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
        boolean cglibProxy = !methodFinal && !classFinal;
        Executor executor = (Executor) bean;
        try {/*w  w  w  . java 2 s  .c  om*/
            return createProxy(bean, cglibProxy, executor);
        } catch (AopConfigException ex) {
            if (cglibProxy) {
                if (log.isDebugEnabled()) {
                    log.debug("Exception occurred while trying to create a proxy, falling back to JDK proxy",
                            ex);
                }
                return createProxy(bean, false, executor);
            }
            throw ex;
        }
    } else if (bean instanceof ThreadPoolTaskExecutor) {
        if (isProxyNeeded(beanName)) {
            boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
            boolean cglibProxy = !classFinal;
            ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) bean;
            return createThreadPoolTaskExecutorProxy(bean, cglibProxy, executor);
        } else {
            log.info("Not instrumenting bean " + beanName);
        }
    }
    return bean;
}

From source file:org.ngrinder.model.BaseEntity.java

/**
 * Merge source entity into current entity.
 *
 * Only not null value is merged./*from w  w w.  j  a va2  s .  c om*/
 *
 * @param source merge source
 * @return merged entity
 */
@SuppressWarnings("unchecked")
public M merge(M source) {
    Field forDisplay = null;
    try {
        Field[] fields = getClass().getDeclaredFields();
        // Iterate over all the attributes
        for (Field each : fields) {
            if (each.isSynthetic()) {
                continue;
            }
            final int modifiers = each.getModifiers();
            if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
                continue;
            }
            forDisplay = each;
            if (!each.isAccessible()) {
                each.setAccessible(true);
            }
            final Object value = each.get(source);
            if (value != null) {
                each.set(this, value);
            }
        }
        return (M) this;
    } catch (Exception e) {
        String displayName = (forDisplay == null) ? "Empty" : forDisplay.getName();
        throw processException(
                displayName + " - Exception occurred while merging an entity from " + source + " to " + this,
                e);
    }
}

From source file:com.l2jfree.config.model.ConfigClassInfo.java

private ConfigClassInfo(Class<?> clazz) throws InstantiationException, IllegalAccessException {
    _clazz = clazz;//w w  w .  ja  v  a  2s.  c  om
    _configClass = _clazz.getAnnotation(ConfigClass.class);

    final Map<String, ConfigGroup> activeGroups = new FastMap<String, ConfigGroup>();

    for (Field field : _clazz.getFields()) {
        final ConfigField configField = field.getAnnotation(ConfigField.class);

        if (configField == null)
            continue;

        if (!Modifier.isStatic(field.getModifiers()) || Modifier.isFinal(field.getModifiers())) {
            _log.warn("Invalid modifiers for " + field);
            continue;
        }

        final ConfigFieldInfo info = new ConfigFieldInfo(field);

        _infos.add(info);

        if (info.getConfigGroupBeginning() != null) {
            final ConfigGroup group = new ConfigGroup();

            activeGroups.put(info.getConfigGroupBeginning().name(), group);

            info.setBeginningGroup(group);
        }

        for (ConfigGroup group : activeGroups.values())
            group.add(info);

        if (info.getConfigGroupEnding() != null) {
            final ConfigGroup group = activeGroups.remove(info.getConfigGroupEnding().name());

            info.setEndingGroup(group);
        }
    }

    if (!activeGroups.isEmpty())
        _log.warn("Invalid config grouping!");

    store();

    try {
        // just in case it's missing
        if (!getConfigFile().exists())
            store(getConfigFile(), null);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:uk.org.lidalia.sysoutslf4j.integration.CrossClassLoaderTestUtils.java

@SuppressWarnings("unchecked")
public static <E> E moveToCurrentClassLoader(Class<E> destinationClass, Object target) {
    if (target.getClass().isPrimitive()) {
        return (E) target;
    } else if (destinationClass.isAssignableFrom(target.getClass())) {
        return (E) target;
    } else if (destinationClass.isInterface()) {
        return createProxyInterface(destinationClass, new ReflectionInvocationHandler(target));
    } else if (target instanceof Enum<?>) {
        return (E) getLocalEnumInstance((Enum<?>) target, destinationClass);
    } else if (Modifier.isFinal(destinationClass.getModifiers())) {
        if (target instanceof Serializable) {
            return (E) moveToCurrentClassLoaderViaSerialization((Serializable) target);
        } else {/*www  .  j  av  a  2 s  .  c o  m*/
            return (E) target;
        }
    } else {
        return createProxyClass(destinationClass, new ReflectionInvocationHandler(target));
    }
}

From source file:org.jiemamy.utils.reflect.ModifierUtil.java

/**
 * {@code public},{@code static},{@code final}????{@code true}?????????{@code false}??
 * /*  ww w . java 2 s  .c  o m*/
 * @param modifier ?
 * @return {@code public},{@code static},{@code final}????{@code true}?????????{@code false}
 */
public static boolean isPublicStaticFinal(int modifier) {
    return Modifier.isPublic(modifier) && Modifier.isStatic(modifier) && Modifier.isFinal(modifier);
}

From source file:code.elix_x.excore.utils.nbt.mbt.encoders.NBTClassEncoder.java

@Override
public NBTTagCompound toNBT(MBT mbt, Object t) {
    NBTTagCompound nbt = new NBTTagCompound();
    Class clazz = t.getClass();//from   w  w  w.j a va  2 s. co  m
    if (encodeClass)
        nbt.setString(CLASS, clazz.getName());
    while (clazz != null && clazz != Object.class) {
        if (!clazz.isAnnotationPresent(MBTIgnore.class)) {
            for (Field field : clazz.getDeclaredFields()) {
                if (!field.isAnnotationPresent(MBTIgnore.class)) {
                    field.setAccessible(true);
                    if ((encodeFinal || !Modifier.isFinal(field.getModifiers()))
                            && (encodeStatic || !Modifier.isStatic(field.getModifiers()))) {
                        try {
                            nbt.setTag(field.getName(), mbt.toNBT(field.get(t)));
                        } catch (IllegalArgumentException e) {
                            Throwables.propagate(e);
                        } catch (IllegalAccessException e) {
                            Throwables.propagate(e);
                        }
                    }
                }
            }
        }
        clazz = encodeSuper ? clazz.getSuperclass() : Object.class;
    }
    return nbt;
}

From source file:org.apache.velocity.runtime.parser.node.SetPublicFieldExecutor.java

/**
 * @param clazz//  ww w .ja va 2s  .com
 * @param property
 * @param arg
 */
protected void discover(final Class clazz, final String property, final Object arg) {
    try {
        Field field = introspector.getField(clazz, property);
        if (!Modifier.isFinal(field.getModifiers())) {
            setField(field);
        }
    }
    /**
     * pass through application level runtime exceptions
     */
    catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        String msg = "Exception while looking for public field '" + property;
        log.error(msg, e);
        throw new VelocityException(msg, e);
    }
}

From source file:com.excilys.ebi.utils.spring.log.slf4j.InjectLoggerAnnotationBeanPostProcessor.java

/**
 * Processes a bean's fields for injection if it has a {@link InjectLogger}
 * annotation.// w  w w .j  a  v  a  2s .c  o m
 */
protected void processLogger(final Object bean) {
    final Class<?> clazz = bean.getClass();

    ReflectionUtils.doWithFields(clazz, new FieldCallback() {
        public void doWith(Field field) {
            Annotation annotation = field.getAnnotation(InjectLogger.class);

            if (annotation != null) {
                int modifiers = field.getModifiers();
                Assert.isTrue(!Modifier.isStatic(modifiers),
                        "InjectLogger annotation is not supported on static fields");
                Assert.isTrue(!Modifier.isFinal(modifiers),
                        "InjectLogger annotation is not supported on final fields");

                ReflectionUtils.makeAccessible(field);

                Logger logger = LoggerFactory.getLogger(clazz);

                ReflectionUtils.setField(field, bean, logger);
            }
        }
    });
}