Example usage for java.lang NoSuchFieldException NoSuchFieldException

List of usage examples for java.lang NoSuchFieldException NoSuchFieldException

Introduction

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

Prototype

public NoSuchFieldException() 

Source Link

Document

Constructor.

Usage

From source file:Main.java

private static Field prepareField(Class c, String fieldName) throws NoSuchFieldException {
    while (c != null) {
        try {/*from ww  w  .  ja  va  2 s.  c  o  m*/
            Field f = c.getDeclaredField(fieldName);
            f.setAccessible(true);
            return f;
        } catch (Exception e) {
        } finally {
            c = c.getSuperclass();
        }
    }
    throw new NoSuchFieldException();
}

From source file:Debug.java

public static Debug getDebugLevel(Class cls) throws NoSuchFieldException {
    try {/*from   w  w w .  j  a v a 2s.  com*/
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        return (Debug) fld.get(null);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:Debug.java

public static void setDebugLevel(Class cls, Debug level) throws NoSuchFieldException {
    try {//w  w w. jav  a2  s.  c o m
        Field fld = cls.getField("debug");
        if (fld.getType() != Debug.class || !Modifier.isStatic(fld.getModifiers()))
            throw new NoSuchFieldException();
        fld.set(null, level);
    } catch (IllegalArgumentException e) {
        throw new NoSuchFieldException();
    } catch (IllegalAccessException e) {
        throw new NoSuchFieldException();
    } catch (SecurityException e) {
        throw new NoSuchFieldException();
    }
}

From source file:org.kuali.kra.award.awardhierarchy.sync.helpers.AwardSyncAwardHelper.java

@Override
public void applySyncChange(Award award, AwardSyncChange change) throws NoSuchFieldException {
    if (StringUtils.equals(change.getSyncType(), AwardSyncType.ADD_SYNC.getSyncValue())) {
        if (StringUtils.equalsIgnoreCase(change.getAttrName(), SPONSOR_CODE_ATTR)) {
            award.setSponsorCode((String) change.getXmlExport().getValues().get(change.getAttrName()));
        } else if (StringUtils.equalsIgnoreCase(change.getAttrName(), STATUS_CODE_ATTR)) {
            award.setStatusCode((Integer) change.getXmlExport().getValues().get(change.getAttrName()));
        } else {// w ww . j a  va2s  .c o m
            throw new NoSuchFieldException();
        }
    }
}

From source file:org.kuali.coeus.common.budget.framework.query.operator.RelationalOperator.java

/**Compares this object with the specified object for order.
 *Returns a negative integer, zero, or a positive integer as this object
 *is less than, equal to, or greater than the specified object.
 *///ww w .  java 2 s. com
protected int compare(Object baseBean) {
    int compareValue = 0;

    if (dataClass == null || !dataClass.equals(baseBean.getClass())) {

        dataClass = baseBean.getClass();

        try {
            field = dataClass.getDeclaredField(fieldName);
            if (!field.isAccessible()) {
                throw new NoSuchFieldException();
            }
        } catch (NoSuchFieldException noSuchFieldException) {
            try {
                String methodName = "";

                if (isBoolean) {
                    methodName = "is" + (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1);
                } else {
                    methodName = "get" + (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1);
                }
                method = dataClass.getMethod(methodName, null);
            } catch (NoSuchMethodException noSuchMethodException) {
                LOG.error(noSuchMethodException.getMessage(), noSuchMethodException);
            }
        }
    } //End if field==null && method==null

    try {
        if (field != null && field.isAccessible()) {

            if (!isBoolean) {
                Comparable comparable = (Comparable) field.get(baseBean);
                if (comparable == null && fixedData == null) {
                    compareValue = 0;
                } else if (comparable == null) {
                    throw new UnsupportedOperationException();
                } else {
                    compareValue = comparable.compareTo(fixedData);
                }

            } else {
                if (((Boolean) field.get(baseBean)).booleanValue() == booleanFixedData)
                    compareValue = 0;
                else
                    compareValue = 1;
            }
        } else {
            if (!isBoolean) {
                Comparable comparable = (Comparable) method.invoke(baseBean, null);
                if (comparable == null && fixedData == null) {
                    compareValue = 0;
                } else if (comparable == null) {
                    throw new UnsupportedOperationException();
                } else if (comparable != null && fixedData == null) {
                    compareValue = -1;
                } else {
                    compareValue = comparable.compareTo(fixedData);
                }
            } else {
                Boolean booleanObj = (Boolean) method.invoke(baseBean, null);
                if (booleanObj == null) {
                    compareValue = -1;
                } else {
                    if (booleanObj.booleanValue() == booleanFixedData)
                        compareValue = 0;
                    else
                        compareValue = 1;
                }
            }
        }
    } catch (IllegalAccessException illegalAccessException) {
        LOG.error(illegalAccessException.getMessage(), illegalAccessException);
    } catch (InvocationTargetException invocationTargetException) {
        LOG.error(invocationTargetException.getMessage(), invocationTargetException);
    }
    return compareValue;
}

From source file:u2f.data.DeviceRegistration.java

public X509Certificate getAttestationCertificate() throws CertificateException, NoSuchFieldException {
    if (attestationCert == null) {
        throw new NoSuchFieldException();
    }/*from www . j  a va  2s  . c  o m*/
    return CertificateParser.parseDer(U2fB64Encoding.decode(attestationCert));
}

From source file:com.yubico.u2f.data.DeviceRegistration.java

@JsonIgnore
public X509Certificate getAttestationCertificate()
        throws U2fBadInputException, CertificateException, NoSuchFieldException {
    if (attestationCert == null) {
        throw new NoSuchFieldException();
    }//w w  w  .  j  av a2  s  .  com
    return CertificateParser.parseDer(U2fB64Encoding.decode(attestationCert));
}

From source file:com.cognifide.aet.validation.impl.ValidationResultBuilderImplTest.java

@Test
public void testToString() throws Exception {
    //empty builder
    ValidationResultBuilderImpl validationResultBuilder = new ValidationResultBuilderImpl();
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder), is(StringUtils.EMPTY));

    //single error message
    validationResultBuilder.addErrorMessage("MSG_1");
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("1 errors were found"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_1"));

    //two error messages
    validationResultBuilder.addErrorMessage("MSG_2");
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("2 errors were found"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_1"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_2"));

    //two error messages
    validationResultBuilder.addErrorMessage("MSG_3", new NoSuchFieldException());
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("3 errors were found"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_1"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_2"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Message: MSG_3"));
    assertThat(ValidationUtils.validationResultToString(validationResultBuilder),
            containsString("Caused by: java.lang.NoSuchFieldException"));
}

From source file:org.kuali.coeus.common.budget.framework.query.QueryList.java

/**
 * sorts the QueryList by the fieldName in ascending or descending order.
 * Note: the field Object should be of Comparable type.
 * @return boolean indicating whether the sort is completed successfully or not.
 * @param ignoreCase use only when comparing strings. as default implementation uses case sensitive comparison.
 * @param fieldName field which is used to sort the bean.
 * @param ascending if true sorting is done in ascending order,
 * else sorting is done in descending order.
 *//*from  w w  w  . j  a  v a  2  s  . com*/
@SuppressWarnings("rawtypes")
public boolean sort(String fieldName, boolean ascending, boolean ignoreCase) {
    Object current, next;
    int compareValue = 0;
    Field field = null;
    Method method = null;
    if (this.size() == 0) {
        return false;
    }
    Class dataClass = get(0).getClass();
    String methodName = null;
    try {
        field = dataClass.getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            throw new NoSuchFieldException();
        }
    } catch (NoSuchFieldException noSuchFieldException) {
        //field not available. Use method invokation.
        try {
            methodName = "get" + (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1);
            method = dataClass.getMethod(methodName, null);
        } catch (NoSuchMethodException noSuchMethodException) {
            LOG.error(noSuchMethodException.getMessage(), noSuchMethodException);
            return false;
        }
    }

    for (int index = 0; index < size() - 1; index++) {
        for (int nextIndex = index + 1; nextIndex < size(); nextIndex++) {
            current = get(index);
            next = get(nextIndex);
            //Check if current and next implements Comparable else can't compare.
            //so return without comparing.May be we can have an exception for this purpose.
            try {
                if (field != null && field.isAccessible()) {
                    Comparable thisObj = (Comparable) field.get(current);
                    Comparable otherObj = (Comparable) field.get(next);
                    if (thisObj == null) {
                        compareValue = -1;
                    } else if (otherObj == null) {
                        compareValue = 1;
                    } else {
                        if (thisObj instanceof String && ignoreCase) {
                            compareValue = ((String) thisObj).compareToIgnoreCase((String) otherObj);
                        } else {
                            compareValue = thisObj.compareTo(otherObj);
                        }
                    }
                } else {
                    Comparable thisObj = null;
                    Comparable otherObj = null;
                    if (methodName != null) {
                        Method thisObjMethod = current.getClass().getMethod(methodName, null);
                        Method otherObjMethod = next.getClass().getMethod(methodName, null);
                        thisObj = (Comparable) thisObjMethod.invoke(current, null);
                        otherObj = (Comparable) otherObjMethod.invoke(next, null);
                    } else {
                        thisObj = (Comparable) method.invoke(current, null);
                        otherObj = (Comparable) method.invoke(next, null);
                    }
                    if (thisObj == null) {
                        compareValue = -1;
                    } else if (otherObj == null) {
                        compareValue = 1;
                    } else {
                        if (thisObj instanceof String && ignoreCase) {
                            compareValue = ((String) thisObj).compareToIgnoreCase((String) otherObj);
                        } else {
                            compareValue = thisObj.compareTo(otherObj);
                        }
                    }
                }
            } catch (IllegalAccessException illegalAccessException) {
                LOG.warn(illegalAccessException.getMessage());
                return false;
            } catch (InvocationTargetException invocationTargetException) {
                LOG.warn(invocationTargetException.getMessage(), invocationTargetException);
                return false;
            } catch (SecurityException e) {
                LOG.warn(e.getMessage(), e);
                return false;
            } catch (NoSuchMethodException e) {
                LOG.warn(e.getMessage(), e);
                return false;
            }

            if (ascending && compareValue > 0) {
                E temp = get(index);
                set(index, get(nextIndex));
                set(nextIndex, temp);
            } else if (!ascending && compareValue < 0) {
                E temp = get(index);
                set(index, get(nextIndex));
                set(nextIndex, temp);
            }

        }
    }
    return true;
}

From source file:org.kuali.kra.budget.calculator.QueryList.java

/**
 * sorts the QueryList by the fieldName in ascending or descending order.
 * Note: the field Object should be of Comparable type.
 * @return boolean indicating whether the sort is completed successfully or not.
 * @param ignoreCase use only when comparing strings. as default implementation uses case sensitive comparison.
 * @param fieldName field which is used to sort the bean.
 * @param ascending if true sorting is done in ascending order,
 * else sorting is done in descending order.
 *///from  ww w.  ja va 2  s .c  om
@SuppressWarnings("rawtypes")
public boolean sort(String fieldName, boolean ascending, boolean ignoreCase) {
    Object current, next;
    int compareValue = 0;
    Field field = null;
    Method method = null;
    if (this.size() == 0) {
        return false;
    }
    Class dataClass = get(0).getClass();
    String methodName = null;
    try {
        field = dataClass.getDeclaredField(fieldName);
        if (!field.isAccessible()) {
            throw new NoSuchFieldException();
        }
    } catch (NoSuchFieldException noSuchFieldException) {
        //field not available. Use method invokation.
        try {
            methodName = "get" + (fieldName.charAt(0) + "").toUpperCase() + fieldName.substring(1);
            method = dataClass.getMethod(methodName, null);
        } catch (NoSuchMethodException noSuchMethodException) {
            noSuchMethodException.printStackTrace();
            return false;
        }
    }

    for (int index = 0; index < size() - 1; index++) {
        for (int nextIndex = index + 1; nextIndex < size(); nextIndex++) {
            current = get(index);
            next = get(nextIndex);
            //Check if current and next implements Comparable else can't compare.
            //so return without comparing.May be we can have an exception for this purpose.
            try {
                if (field != null && field.isAccessible()) {
                    Comparable thisObj = (Comparable) field.get(current);
                    Comparable otherObj = (Comparable) field.get(next);
                    if (thisObj == null) {
                        compareValue = -1;
                    } else if (otherObj == null) {
                        compareValue = 1;
                    } else {
                        if (thisObj instanceof String && ignoreCase) {
                            compareValue = ((String) thisObj).compareToIgnoreCase((String) otherObj);
                        } else {
                            compareValue = thisObj.compareTo(otherObj);
                        }
                    }
                } else {
                    Comparable thisObj = null;
                    Comparable otherObj = null;
                    if (methodName != null) {
                        Method thisObjMethod = current.getClass().getMethod(methodName, null);
                        Method otherObjMethod = next.getClass().getMethod(methodName, null);
                        thisObj = (Comparable) thisObjMethod.invoke(current, null);
                        otherObj = (Comparable) otherObjMethod.invoke(next, null);
                    } else {
                        thisObj = (Comparable) method.invoke(current, null);
                        otherObj = (Comparable) method.invoke(next, null);
                    }
                    if (thisObj == null) {
                        compareValue = -1;
                    } else if (otherObj == null) {
                        compareValue = 1;
                    } else {
                        if (thisObj instanceof String && ignoreCase) {
                            compareValue = ((String) thisObj).compareToIgnoreCase((String) otherObj);
                        } else {
                            compareValue = thisObj.compareTo(otherObj);
                        }
                    }
                }
            } catch (IllegalAccessException illegalAccessException) {
                LOG.warn(illegalAccessException.getMessage());
                return false;
            } catch (InvocationTargetException invocationTargetException) {
                LOG.warn(invocationTargetException.getMessage(), invocationTargetException);
                return false;
            } catch (SecurityException e) {
                LOG.warn(e.getMessage(), e);
                return false;
            } catch (NoSuchMethodException e) {
                LOG.warn(e.getMessage(), e);
                return false;
            }

            if (ascending && compareValue > 0) {
                E temp = get(index);
                set(index, get(nextIndex));
                set(nextIndex, temp);
            } else if (!ascending && compareValue < 0) {
                E temp = get(index);
                set(index, get(nextIndex));
                set(nextIndex, temp);
            }

        }
    }
    return true;
}