Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright 2010 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
  *      http://www.apache.org/licenses/LICENSE-2.0
  *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.callbackparams.annotation.util;

Deprecated:
This class is currently used by some of the utilities in the package org.callbackparams.sandbox but they will be modified and this package will be removed within the 1.0-release of the framework.
Author(s):
Henrik Kaipe
public class EnumConstantDescriptor {
    private final Field coreEnumField;
    public EnumConstantDescriptor(final Enum<?> enumConstant) {
        for (Field f : enumConstant.getDeclaringClass().getFields()) {
            if (f.isEnumConstant()) {
                if (false == f.isAccessible()) {
                    try {
                        f.setAccessible(true);
                    } catch (final SecurityException x) {
                        /* Never mind */
                    }
                }
                try {
                    if (enumConstant == f.get(null)) {
                        this. = f;
                        return;
                    }
                } catch (Exception x) {
                    throw new Error(x);
                }
            }
        }
        throw new AssertionError("Unable to determine field for enum-constant "
                + enumConstant);
    }

    
Same as an incocation of getAnnotation(java.lang.Class,boolean) with force=false.

    public <A extends Annotation> A getAnnotation(Class<A> a) {
        return getAnnotation(afalse);
    }

    
Tries to find an annotation of the specified type. Primarly the annotations for the enum-constant will be investigated. If the desired annotation cannot be found on the enum-constant then the annotation will be picked from the enum-class.

Returns:
the desired annotation or (in case force=false) null in case it cannot be found
Throws:
java.lang.AssertionError in case the desired annotation can not be found and force=true
    public <A extends Annotation> A getAnnotation(
            final Class<A> afinal boolean force) {
        A annotation = .getAnnotation(a);
        if (null != annotation) {
            return annotation;
        }
        annotation = .getDeclaringClass().getAnnotation(a);
        if (null != annotation) {
            return annotation;
        } else if (false == force) {
            return null;
        } else {
            throw new AssertionError("Neither the enum-constant "
                    + .getName() + " nor its class "
                    + .getDeclaringClass() + " is annotated with "
                    + a);
        }
    }
New to GrepCode? Check out our FAQ X