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.dungnv.vfw5.base.utils.StringUtils.java

public static void trimString(Object obj, boolean isLower) {
    String oldData = "";
    String newData = "";
    try {// w w  w.j a v a2 s  . co  m
        if (obj != null) {
            Class escapeClass = obj.getClass();
            Field fields[] = escapeClass.getDeclaredFields();
            Field superFields[] = escapeClass.getSuperclass().getDeclaredFields();
            Field allField[] = new Field[fields.length + superFields.length];
            System.arraycopy(fields, 0, allField, 0, fields.length);
            System.arraycopy(superFields, 0, allField, fields.length, superFields.length);
            for (Field f : allField) {
                f.setAccessible(true);
                if (f.getType().equals(java.lang.String.class) && !Modifier.isFinal(f.getModifiers())) {
                    if (f.get(obj) != null) {
                        oldData = f.get(obj).toString();
                        newData = isLower ? oldData.trim().toLowerCase() : oldData.trim();
                        f.set(obj, newData);
                    }
                } else if (f.getType().isArray()) {
                    if (f.getType().getComponentType().equals(java.lang.String.class)) {
                        String[] tmpArr = (String[]) f.get(obj);
                        if (tmpArr != null) {
                            for (int i = 0; i < tmpArr.length; i++) {
                                tmpArr[i] = isLower ? tmpArr[i].trim().toLowerCase() : tmpArr[i].trim();
                            }
                            f.set(obj, tmpArr);
                        }
                    }
                } else if (f.get(obj) instanceof List) {
                    List<Object> tmpList = (List<Object>) f.get(obj);
                    for (int i = 0; i < tmpList.size(); i++) {
                        if (tmpList.get(i) instanceof java.lang.String) {
                            tmpList.set(i, isLower ? tmpList.get(i).toString().trim().toLowerCase()
                                    : tmpList.get(i).toString().trim());
                        }
                    }
                    f.set(obj, tmpList);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.jboss.dashboard.commons.misc.ReflectionUtils.java

public static Field getField(Object obj, String propertyName) {
    try {//from   ww w  .  j  a va2  s  . co m
        Field field = obj.getClass().getField(propertyName);
        int modifiers = field.getModifiers();
        if (Modifier.isPublic(modifiers) && !Modifier.isFinal(field.getModifiers())) {
            return field;
        }
    } catch (NoSuchFieldException e) {
    }
    return null;
}

From source file:org.regenstrief.hl7.HL7Data.java

private final void initFields() {
    final Class<? extends HL7Data> c = getClass();
    if (fields.get(c) != null) {
        return;// ww w  .  j a  v  a2 s  .c  o m
    }
    final List<Field> all = ReflectUtil.getFieldList(c);
    final String pre = c.getSimpleName() + '.';
    final int istart = pre.length();
    final List<String> fnames = new ArrayList<String>();
    for (final Field f : all) {
        final int m = f.getModifiers();
        if (!Modifier.isFinal(m) || !Modifier.isStatic(m) || !Modifier.isPublic(m)) {
            continue;
        }
        final String fname = f.getName();
        if (!fname.endsWith("_XML") || !f.getType().equals(String.class)) {
            continue;
        }
        final String fval;
        try {
            fval = (String) f.get(null);
        } catch (final Exception e) {
            throw Util.toRuntimeException(e);
        }
        if (fval.startsWith(pre)) {
            final int fend = fval.length();
            if (Util.isAllDigits(fval, istart, fend)) {
                Util.set(fnames, Util.parseInt(fval, istart, fend) - 1,
                        frm(fname.substring(0, fname.length() - 4)));
            }
        }
    }
    final int numFields = fnames.size();
    final List<Field> fields = new ArrayList<Field>(numFields);
    for (final Field f : all) {
        final int m = f.getModifiers();
        // Fields will be private if defined in this class or protected if defined in super class
        if (Modifier.isFinal(m) || Modifier.isStatic(m) || Modifier.isPublic(m)) {
            continue;
        }
        final int i = fnames.indexOf(frm(f.getName()));
        if (i < 0) {
            continue;
        }
        f.setAccessible(true);
        Util.set(fields, i, f);
    }
    for (int i = 0; i < numFields; i++) {
        if (fields.get(i) == null) {
            final String msg = "Error initializing field " + (i + 1) + " (" + fnames.get(i) + ")";
            //throw new IllegalStateException(msg);
            log.warn(msg);
        }
    }
    HL7Data.fields.put(c, fields);
}

From source file:org.apache.cassandra.config.Config.java

public static void log(Config config) {
    Map<String, String> configMap = new TreeMap<>();
    for (Field field : Config.class.getFields()) {
        // ignore the constants
        if (Modifier.isFinal(field.getModifiers()))
            continue;

        String name = field.getName();
        if (SENSITIVE_KEYS.contains(name)) {
            configMap.put(name, "<REDACTED>");
            continue;
        }//www. ja  v a 2 s .  com

        String value;
        try {
            // Field.get() can throw NPE if the value of the field is null
            value = field.get(config).toString();
        } catch (NullPointerException | IllegalAccessException npe) {
            value = "null";
        }
        configMap.put(name, value);
    }

    logger.info("Node configuration:[" + Joiner.on("; ").join(configMap.entrySet()) + "]");
}

From source file:cern.c2mon.shared.common.datatag.address.impl.HardwareAddressImpl.java

/**
 * The two addresses are considered equals if they're of the same type and all their non-static attributes are equal
 *//*www. j  a v  a2 s  . com*/
@Override
public final boolean equals(final Object copy) {

    boolean result = copy != null && copy instanceof HardwareAddress && this.getClass().equals(copy.getClass());

    if (result) {

        Field[] fields = this.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())
                    && !Modifier.isTransient(field.getModifiers())) {
                try {

                    if ((field.get(this) != null && field.get(copy) == null)
                            || (field.get(this) == null && field.get(copy) != null)) {
                        result = false;
                    } else if (field.get(this) != null && field.get(copy) != null) {

                        if (field.getType().isArray()) {

                            if (Object[].class.isAssignableFrom(field.getType())) {
                                result = Arrays.equals((Object[]) field.get(this), (Object[]) field.get(copy));
                            } else {
                                result = ArrayUtils.isEquals(field.get(this), field.get(copy));
                            }

                        } else {
                            result = field.get(this).equals(field.get(copy));
                        }
                    }
                } catch (Exception e) {
                    result = false;
                }
            }

            if (!result) {
                break;
            }
        }
    }

    return result;
}

From source file:org.hibernate.internal.util.ReflectHelper.java

/**
 * Determine is the given class is declared final.
 *
 * @param clazz The class to check.//  ww  w.jav a 2s .  c o  m
 * @return True if the class is final, flase otherwise.
 */
public static boolean isFinalClass(Class clazz) {
    return Modifier.isFinal(clazz.getModifiers());
}

From source file:org.glowroot.agent.weaving.ClassAnalyzer.java

@RequiresNonNull("bridgeTargetAdvisors")
private List<Advice> analyzeMethod(ThinMethod thinMethod) {
    if (Modifier.isFinal(thinMethod.access()) && Modifier.isPublic(thinMethod.access())) {
        ImmutablePublicFinalMethod.Builder builder = ImmutablePublicFinalMethod.builder()
                .name(thinMethod.name());
        List<Type> parameterTypes = Arrays.asList(Type.getArgumentTypes(thinMethod.descriptor()));
        for (Type parameterType : parameterTypes) {
            builder.addParameterTypes(parameterType.getClassName());
        }//  w ww .  j a v a 2 s. c  o m
        analyzedClassBuilder.addPublicFinalMethods(builder.build());
    }
    if (shortCircuitBeforeAnalyzeMethods) {
        return ImmutableList.of();
    }
    List<Type> parameterTypes = Arrays.asList(Type.getArgumentTypes(thinMethod.descriptor()));
    Type returnType = Type.getReturnType(thinMethod.descriptor());
    List<String> methodAnnotations = thinMethod.annotations();
    List<Advice> matchingAdvisors = getMatchingAdvisors(thinMethod, methodAnnotations, parameterTypes,
            returnType);
    boolean intfMethod = intf && !Modifier.isStatic(thinMethod.access());
    if (matchingAdvisors.isEmpty() && !intfMethod) {
        return ImmutableList.of();
    }
    ImmutableAnalyzedMethod.Builder builder = ImmutableAnalyzedMethod.builder();
    builder.name(thinMethod.name());
    for (Type parameterType : parameterTypes) {
        builder.addParameterTypes(parameterType.getClassName());
    }
    builder.returnType(returnType.getClassName()).modifiers(thinMethod.access())
            .signature(thinMethod.signature());
    for (String exception : thinMethod.exceptions()) {
        builder.addExceptions(ClassNames.fromInternalName(exception));
    }
    List<Advice> subTypeRestrictedAdvisors = Lists.newArrayList();
    for (Iterator<Advice> i = matchingAdvisors.iterator(); i.hasNext();) {
        Advice advice = i.next();
        if (!isSubTypeRestrictionMatch(advice, superClassNames)) {
            subTypeRestrictedAdvisors.add(advice);
            i.remove();
        }
    }
    builder.addAllAdvisors(matchingAdvisors).addAllSubTypeRestrictedAdvisors(subTypeRestrictedAdvisors);
    analyzedClassBuilder.addAnalyzedMethods(builder.build());
    return matchingAdvisors;
}

From source file:com.judoscript.jamaica.parser.JamaicaDumpVisitor.java

static void printAccessFlags(int flags) {
    if (Modifier.isPublic(flags))
        out.print("public ");
    else if (Modifier.isProtected(flags))
        out.print("protected ");
    else if (Modifier.isPrivate(flags))
        out.print("private ");

    if (Modifier.isAbstract(flags))
        out.print("abstract ");
    if (Modifier.isFinal(flags))
        out.print("final ");
    if (Modifier.isNative(flags))
        out.print("native ");
    if (Modifier.isStatic(flags))
        out.print("static ");
    if (Modifier.isStrict(flags))
        out.print("strict ");
    if (Modifier.isSynchronized(flags))
        out.print("synchronized ");
    if (Modifier.isTransient(flags))
        out.print("transient ");
    if (Modifier.isVolatile(flags))
        out.print("volative ");
}

From source file:net.java.sip.communicator.impl.protocol.jabber.InfoRetreiver.java

/**
 * Load VCard for the given user./*  w w w. j ava  2s  . c  om*/
 * Using the specified timeout.
 *
 * @param vcard VCard
 * @param connection XMPP connection
 * @param user the user
 * @param timeout timeout in second
 * @throws XMPPException if something went wrong during VCard loading
 */
public void load(VCard vcard, Connection connection, String user, long timeout) throws XMPPException {
    vcard.setTo(user);

    vcard.setType(IQ.Type.GET);
    PacketCollector collector = connection.createPacketCollector(new PacketIDFilter(vcard.getPacketID()));
    connection.sendPacket(vcard);

    VCard result = null;
    try {
        result = (VCard) collector.nextResult(timeout);

        if (result == null) {
            String errorMessage = "Timeout getting VCard information";
            throw new XMPPException(errorMessage,
                    new XMPPError(XMPPError.Condition.request_timeout, errorMessage));
        }

        if (result.getError() != null) {
            throw new XMPPException(result.getError());
        }
    } catch (ClassCastException e) {
        logger.error("No vcard for " + user);
    }

    if (result == null)
        result = new VCard();

    // copy loaded vcard fields in the supplied one.
    Field[] fields = VCard.class.getDeclaredFields();
    for (Field field : fields) {
        if (field.getDeclaringClass() == VCard.class && !Modifier.isFinal(field.getModifiers())) {
            try {
                field.setAccessible(true);
                field.set(vcard, field.get(result));
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Cannot set field:" + field, e);
            }
        }
    }
}

From source file:adalid.core.XS1.java

static Collection<Field> getFields(Class<?> clazz, Class<?> top) throws SecurityException {
    ArrayList<Field> fields = new ArrayList<>();
    if (clazz == null || top == null) {
        return fields;
    }//from   w  w  w  . jav a 2  s. c  om
    int modifiers;
    boolean restricted;
    Class<?> superClazz = clazz.getSuperclass();
    if (superClazz == null) {
        logger.trace(clazz.getName());
    } else {
        logger.trace(clazz.getName() + " extends " + superClazz.getName());
        if (top.isAssignableFrom(superClazz)) {
            //              fields.addAll(getFields(superClazz, top));
            Collection<Field> superFields = getFields(superClazz, top);
            for (Field field : superFields) {
                modifiers = field.getModifiers();
                restricted = Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers);
                if (restricted) {
                    continue;
                }
                fields.add(field);
            }
        }
    }
    if (top.isAssignableFrom(clazz)) {
        logger.trace("adding fields declared at " + clazz.getName());
        //          fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            modifiers = field.getModifiers();
            restricted = Modifier.isStatic(modifiers) || Modifier.isFinal(modifiers);
            if (restricted) {
                continue;
            }
            fields.add(field);
        }
    }
    return getRidOfDupFields(fields);
}