Example usage for java.lang NoSuchFieldError NoSuchFieldError

List of usage examples for java.lang NoSuchFieldError NoSuchFieldError

Introduction

In this page you can find the example usage for java.lang NoSuchFieldError NoSuchFieldError.

Prototype

public NoSuchFieldError(String s) 

Source Link

Document

Constructs a NoSuchFieldError with the specified detail message.

Usage

From source file:Main.java

public static Field findFieldByClassAndTypeAndName(Class<?> targetObject, Class<?> fieldType,
        String fieldName) {//from w  w w.  j  a v a2  s.com
    Class clazz = targetObject;
    do {
        for (Field field : clazz.getDeclaredFields()) {
            if (field.getType() == fieldType && field.getName().equals(fieldName)) {
                field.setAccessible(true);
                return field;
            }
        }
        clazz = clazz.getSuperclass();
    } while (clazz != null);
    throw new NoSuchFieldError("Field of type " + fieldType.getName() + " in class " + targetObject.getName());
}

From source file:Main.java

/**
 * Returns the first field of the given type in a class.
 * Might be useful for Proguard'ed classes to identify fields with unique types.
 * If no matching field was not found, a {@link NoSuchFieldError} will be thrown.
 *///from   w w  w.ja v  a2 s.c  o  m
public static Field findFirstFieldByExactType(Class<?> clazz, Class<?> type) {
    Class<?> clz = clazz;
    do {
        for (Field field : clz.getDeclaredFields()) {
            if (field.getType() == type) {
                field.setAccessible(true);
                return field;
            }
        }
    } while ((clz = clz.getSuperclass()) != null);

    throw new NoSuchFieldError("Field of type " + type.getName() + " in class " + clazz.getName());
}

From source file:Main.java

/**
 * Look up a field in a class and set it to accessible. The result is cached.
 * If the field was not found, a {@link NoSuchFieldError} will be thrown.
 *//*ww w .  j  a  v  a  2s .c om*/
public static Field findField(Class<?> clazz, String fieldName) {
    StringBuilder sb = new StringBuilder(clazz.getName());
    sb.append('#');
    sb.append(fieldName);
    String fullFieldName = sb.toString();

    if (fieldCache.containsKey(fullFieldName)) {
        Field field = fieldCache.get(fullFieldName);
        if (field == null)
            throw new NoSuchFieldError(fullFieldName);
        return field;
    }

    try {
        Field field = findFieldRecursiveImpl(clazz, fieldName);
        field.setAccessible(true);
        fieldCache.put(fullFieldName, field);
        return field;
    } catch (NoSuchFieldException e) {
        fieldCache.put(fullFieldName, null);
        throw new NoSuchFieldError(fullFieldName);
    }
}

From source file:Main.java

static Unsafe getSMU() {
    try {/*www .j a v a2 s.c  om*/
        return sun.misc.Unsafe.getUnsafe();
    } catch (SecurityException tryReflectionInstead) {
        // ignore
    }
    try {
        return java.security.AccessController.doPrivileged((PrivilegedExceptionAction<Unsafe>) () -> {
            Class<sun.misc.Unsafe> k = sun.misc.Unsafe.class;
            for (Field f : k.getDeclaredFields()) {
                f.setAccessible(true);
                Object x = f.get(null);
                if (k.isInstance(x))
                    return k.cast(x);
            }
            throw new NoSuchFieldError("the Unsafe");
        });
    } catch (java.security.PrivilegedActionException e) {
        throw new RuntimeException("Could not initialize intrinsics", e.getCause());
    }
}

From source file:com.google.code.ddom.commons.cl.ClassLoaderLocal.java

private static Map<ClassLoaderLocal<?>, Object> getClassLoaderLocalMap(ClassLoader cl) {
    if (cl == ClassLoaderLocal.class.getClassLoader()) {
        return ClassLoaderLocalMapHolder.locals;
    } else {// w  w w  .  ja  va 2  s  .  c om
        Class<?> holderClass;
        try {
            holderClass = cl.loadClass(holderClassName);
            if (holderClass.getClassLoader() != cl) {
                holderClass = null;
            }
        } catch (ClassNotFoundException ex) {
            holderClass = null;
        }
        if (holderClass == null) {
            try {
                holderClass = (Class<?>) defineClassMethod.invoke(cl, holderClassName, holderClassDef, 0,
                        holderClassDef.length);
            } catch (Exception ex) {
                // TODO: can we get here? maybe if two threads call getClassLoaderLocalMap concurrently?
                throw new Error(ex);
            }
        }
        Field field;
        try {
            field = holderClass.getDeclaredField("locals");
        } catch (NoSuchFieldException ex) {
            throw new NoSuchFieldError(ex.getMessage());
        }
        try {
            return (Map<ClassLoaderLocal<?>, Object>) field.get(0);
        } catch (IllegalAccessException ex) {
            throw new Error(ex);
        }
    }
}

From source file:org.ala.repository.Validator.java

/**
 * Validate a DC file (parsed into list of String[])
 *
 * @param lines //from w  w  w .  j  a  v a  2  s . c  o m
 * @throws MalformedURLException
 * @throws IllegalArgumentException
 * @throws NoSuchFieldError
 * @throws Exception
 */
protected void validateDcFile(List<String[]> lines)
        throws MalformedURLException, IllegalArgumentException, NoSuchFieldError, Exception {
    // initialise requiredDcFields
    ArrayList<String> requiredDcFields = new ArrayList<String>();
    requiredDcFields.add(Field.IDENTIFIER.name); // alt value: Predicates.DC_IDENTIFIER.getLocalPart()
    requiredDcFields.add(Field.FORMAT.name);
    requiredDcFields.add(Field.MODIFIED.name);
    //requiredDcFields.add(Field.URI.name);

    for (String[] data : lines) {
        logger.debug("DC entries (" + data.length + ") = " + StringUtils.join(data, "|"));
        // Check for expected number of tab fields
        Assert.isTrue(data.length == FileType.DC.getFieldCount(), "Entry not expected size of "
                + FileType.DC.getFieldCount() + ", got " + data.length + " - " + StringUtils.join(data, "|"));

        if (data[0].endsWith(Field.FORMAT.name)) {
            // Check "format" field
            requiredDcFields.remove(Field.FORMAT.name);
            Assert.isTrue(MimeType.getAllMimeTypes().contains(data[1]),
                    Field.FORMAT.name + " does not contain an accepted value: " + data[1] + " - "
                            + StringUtils.join(MimeType.getAllMimeTypes(), "|"));
        } else if (data[0].endsWith(Field.IDENTIFIER.name)) {
            // Check "identifier" field
            requiredDcFields.remove(Field.IDENTIFIER.name);
            Assert.isTrue(data[1].length() > 0, Field.IDENTIFIER.name + " is empty");
        } else if (data[0].endsWith(Field.MODIFIED.name)) {
            // Check "modified" date field
            requiredDcFields.remove(Field.MODIFIED.name);
            Assert.isTrue(data[1].length() > 0, Field.MODIFIED.name + " date is empty");
            DateValidator validator = DateValidator.getInstance();
            if (!validator.isValid(data[1], "yyyy-MM-dd", true)) {
                throw new IllegalArgumentException(
                        Field.MODIFIED.name + " date is not a valid date: " + data[1]);
            }
        } else if (data[0].endsWith(Field.URI.name)) {
            // Check "URI" field
            requiredDcFields.remove(Field.URI.name);
            new URL(data[1]); // throws MalformedURLException if not valid URL
        }
    }

    if (!requiredDcFields.isEmpty()) {
        throw new NoSuchFieldError("Required fields not found: " + StringUtils.join(requiredDcFields, ", "));
    }
}

From source file:org.ala.repository.Validator.java

/**
 * Validate a DC file (parsed into list of String[])
 *
 * @param lines/*from  www . j a v  a 2s  .co  m*/
 * @throws IllegalArgumentException
 * @throws NoSuchFieldError
 * @throws Exception
 */
protected void validateRdfFile(List<String[]> lines)
        throws IllegalArgumentException, NoSuchFieldError, Exception {
    // initialise requiredRdfFields
    ArrayList<String> requiredRdfFields = new ArrayList<String>();
    requiredRdfFields.add(Field.SCI_NAME.name);

    for (String[] data : lines) {
        logger.debug("RDF entries (" + data.length + ") = " + StringUtils.join(data, "|"));
        Assert.isTrue(data.length == FileType.RDF.getFieldCount(), "RDF Entry not expected size of "
                + FileType.RDF.getFieldCount() + ", got " + data.length + " - " + StringUtils.join(data, "|"));

        if (data[1].endsWith(Field.SCI_NAME.name)) {
            // Check hasScientificName
            requiredRdfFields.remove(Field.SCI_NAME.name);
            Assert.isTrue(data[2].length() > 0, Field.SCI_NAME.name + " is empty");
        }
    }

    if (!requiredRdfFields.isEmpty()) {
        throw new NoSuchFieldError("Required fields not found: " + StringUtils.join(requiredRdfFields, ", "));
    }
}

From source file:LinkedTransferQueue.java

static long objectFieldOffset(sun.misc.Unsafe UNSAFE, String field, Class<?> klazz) {
    try {/*from w w w. j a v a2s  . co m*/
        return UNSAFE.objectFieldOffset(klazz.getDeclaredField(field));
    } catch (NoSuchFieldException e) {
        // Convert Exception to corresponding Error
        NoSuchFieldError error = new NoSuchFieldError(field);
        error.initCause(e);
        throw error;
    }
}