Example usage for java.lang.reflect Member getDeclaringClass

List of usage examples for java.lang.reflect Member getDeclaringClass

Introduction

In this page you can find the example usage for java.lang.reflect Member getDeclaringClass.

Prototype

public Class<?> getDeclaringClass();

Source Link

Document

Returns the Class object representing the class or interface that declares the member or constructor represented by this Member.

Usage

From source file:org.apache.bval.jsr.ClassValidator.java

private <T> Set<ConstraintViolation<T>> validateInvocationParameters(final Member invocable,
        final Object[] parameterValues, final InvocableElementDescriptor constructorDescriptor,
        final Class<?>[] gps, final NodeImpl rootNode, final Object rootBean) {
    final Set<ConstraintViolation<T>> violations = new HashSet<ConstraintViolation<T>>();

    @SuppressWarnings("unchecked")
    final GroupValidationContext<ConstraintValidationListener<?>> parametersContext = createInvocableContext(
            constructorDescriptor.getMetaBean(), rootBean, Class.class.cast(invocable.getDeclaringClass()),
            gps);/*w  w w.  j  a  v  a 2  s  . co m*/

    @SuppressWarnings("unchecked")
    final GroupValidationContext<Object> crossParameterContext = createContext(
            constructorDescriptor.getMetaBean(), rootBean, Class.class.cast(invocable.getDeclaringClass()),
            gps);

    if (rootBean == null) {
        final Constructor<?> m = Constructor.class.cast(invocable);
        parametersContext.setConstructor(m);
        crossParameterContext.setConstructor(m);
    } else { // could be more sexy but that's ok for now
        final Method m = Method.class.cast(invocable);
        parametersContext.setMethod(m);
        crossParameterContext.setMethod(m);
    }

    final Groups groups = parametersContext.getGroups();

    final List<ParameterDescriptor> parameterDescriptors = constructorDescriptor.getParameterDescriptors();
    final ElementDescriptorImpl crossParamDescriptor = ElementDescriptorImpl.class
            .cast(constructorDescriptor.getCrossParameterDescriptor());
    final Set<ConstraintDescriptor<?>> crossParamConstraints = crossParamDescriptor.getConstraintDescriptors();

    crossParameterContext.setBean(parameterValues);
    crossParameterContext.moveDown(rootNode);
    crossParameterContext.moveDown("<cross-parameter>");
    crossParameterContext.setKind(ElementKind.CROSS_PARAMETER);

    parametersContext.moveDown(rootNode);
    parametersContext.setParameters(parameterValues);

    for (final Group current : groups.getGroups()) {
        for (int i = 0; i < parameterValues.length; i++) {
            final ParameterDescriptorImpl paramDesc = ParameterDescriptorImpl.class
                    .cast(parameterDescriptors.get(i));
            parametersContext.setBean(parameterValues[i]);
            parametersContext.moveDown(new NodeImpl.ParameterNodeImpl(paramDesc.getName(), i));
            for (final ConstraintDescriptor<?> constraintDescriptor : paramDesc.getConstraintDescriptors()) {
                final ConstraintValidation<?> validation = ConstraintValidation.class
                        .cast(constraintDescriptor);
                parametersContext.setCurrentGroup(paramDesc.mapGroup(current));
                validation.validateGroupContext(parametersContext);
            }
            parametersContext.moveUp(null, null);
        }

        for (final ConstraintDescriptor<?> d : crossParamConstraints) {
            final ConstraintValidation<?> validation = ConstraintValidation.class.cast(d);
            crossParameterContext.setCurrentGroup(crossParamDescriptor.mapGroup(current));
            validation.validateGroupContext(crossParameterContext);
        }

        if (gps.length == 0 && parametersContext.getListener().getConstraintViolations().size()
                + crossParameterContext.getListener().getConstraintViolations().size() > 0) {
            break;
        }
    }

    for (final Group current : groups.getGroups()) {
        for (int i = 0; i < parameterValues.length; i++) {
            final ParameterDescriptorImpl paramDesc = ParameterDescriptorImpl.class
                    .cast(parameterDescriptors.get(i));
            if (paramDesc.isCascaded() && parameterValues[i] != null) {
                parametersContext.setBean(parameterValues[i]);
                parametersContext.moveDown(new NodeImpl.ParameterNodeImpl(paramDesc.getName(), i));
                initMetaBean(parametersContext, factoryContext.getMetaBeanFinder(),
                        parameterValues[i].getClass());
                parametersContext.setCurrentGroup(paramDesc.mapGroup(current));
                ValidationHelper.validateContext(parametersContext,
                        new JsrValidationCallback(parametersContext), factoryContext.isTreatMapsLikeBeans());
                parametersContext.moveUp(null, null);
            }
        }
    }

    for (final List<Group> eachSeq : groups.getSequences()) {
        for (final Group current : eachSeq) {
            for (int i = 0; i < parameterValues.length; i++) {
                final ParameterDescriptorImpl paramDesc = ParameterDescriptorImpl.class
                        .cast(parameterDescriptors.get(i));
                parametersContext.setBean(parameterValues[i]);
                parametersContext.moveDown(new NodeImpl.ParameterNodeImpl(paramDesc.getName(), i));
                for (final ConstraintDescriptor<?> constraintDescriptor : paramDesc
                        .getConstraintDescriptors()) {
                    final ConstraintValidation<?> validation = ConstraintValidation.class
                            .cast(constraintDescriptor);
                    parametersContext.setCurrentGroup(paramDesc.mapGroup(current));
                    validation.validateGroupContext(parametersContext);
                }
                parametersContext.moveUp(null, null);
            }

            for (final ConstraintDescriptor<?> d : crossParamConstraints) {
                final ConstraintValidation<?> validation = ConstraintValidation.class.cast(d);
                crossParameterContext.setCurrentGroup(crossParamDescriptor.mapGroup(current));
                validation.validateGroupContext(crossParameterContext);
            }

            if (parametersContext.getListener().getConstraintViolations().size()
                    + crossParameterContext.getListener().getConstraintViolations().size() > 0) {
                break;
            }
        }

        for (final Group current : eachSeq) {
            for (int i = 0; i < parameterValues.length; i++) {
                final ParameterDescriptorImpl paramDesc = ParameterDescriptorImpl.class
                        .cast(parameterDescriptors.get(i));
                if (paramDesc.isCascaded() && parameterValues[i] != null) {
                    parametersContext.setBean(parameterValues[i]);
                    parametersContext.moveDown(new NodeImpl.ParameterNodeImpl(paramDesc.getName(), i));
                    initMetaBean(parametersContext, factoryContext.getMetaBeanFinder(),
                            parameterValues[i].getClass());
                    parametersContext.setCurrentGroup(paramDesc.mapGroup(current));
                    ValidationHelper.validateContext(parametersContext,
                            new JsrValidationCallback(parametersContext),
                            factoryContext.isTreatMapsLikeBeans());
                    parametersContext.moveUp(null, null);
                }
            }
        }
    }
    if (constructorDescriptor.isCascaded()) {
        if (parametersContext.getValidatedValue() != null) {
            initMetaBean(parametersContext, factoryContext.getMetaBeanFinder(),
                    parametersContext.getValidatedValue().getClass());

            for (final Group current : groups.getGroups()) {
                parametersContext.setCurrentGroup(constructorDescriptor.mapGroup(current));
                ValidationHelper.validateContext(parametersContext,
                        new JsrValidationCallback(parametersContext), factoryContext.isTreatMapsLikeBeans());
            }
            for (final List<Group> eachSeq : groups.getSequences()) {
                for (final Group current : eachSeq) {
                    parametersContext.setCurrentGroup(constructorDescriptor.mapGroup(current));
                    ValidationHelper.validateContext(parametersContext,
                            new JsrValidationCallback(parametersContext),
                            factoryContext.isTreatMapsLikeBeans());
                    if (!parametersContext.getListener().isEmpty()) {
                        break;
                    }
                }
            }
        }
        if (crossParameterContext.getValidatedValue() != null) {
            initMetaBean(crossParameterContext, factoryContext.getMetaBeanFinder(),
                    crossParameterContext.getValidatedValue().getClass());

            for (final Group current : groups.getGroups()) {
                crossParameterContext.setCurrentGroup(constructorDescriptor.mapGroup(current));
                ValidationHelper.validateContext(crossParameterContext,
                        new JsrValidationCallback(crossParameterContext),
                        factoryContext.isTreatMapsLikeBeans());
            }
            for (final List<Group> eachSeq : groups.getSequences()) {
                for (final Group current : eachSeq) {
                    crossParameterContext.setCurrentGroup(constructorDescriptor.mapGroup(current));
                    ValidationHelper.validateContext(crossParameterContext,
                            new JsrValidationCallback(crossParameterContext),
                            factoryContext.isTreatMapsLikeBeans());
                    if (!crossParameterContext.getListener().isEmpty()) {
                        break;
                    }
                }
            }
        }
    }

    @SuppressWarnings("unchecked")
    final Set<ConstraintViolation<T>> parameterViolations = Set.class
            .cast(parametersContext.getListener().getConstraintViolations());
    violations.addAll(parameterViolations);
    @SuppressWarnings("unchecked")
    final Set<ConstraintViolation<T>> crossParameterViolations = Set.class
            .cast(crossParameterContext.getListener().getConstraintViolations());
    violations.addAll(crossParameterViolations);

    return violations;
}

From source file:jp.co.acroquest.jsonic.JSON.java

/**
 * Ignore this property. A default behavior is to ignore transient or declaring method in java.lang.Object.
 * You can override this method if you have to change default behavior.
 * /*from  w w w  .j a v  a  2  s  . com*/
 * @param context current context
 * @param target target class
 * @param member target member
 * @return true if this member must be ignored.
 */
protected boolean ignore(Context context, Class<?> target, Member member) {
    if (Modifier.isTransient(member.getModifiers()))
        return true;
    if (member.getDeclaringClass().equals(Object.class))
        return true;
    return false;
}

From source file:com.parse.ParseObject.java

private static boolean isAccessible(Member m) {
    return Modifier.isPublic(m.getModifiers())
            || (m.getDeclaringClass().getPackage().getName().equals("com.parse")
                    && !Modifier.isPrivate(m.getModifiers()) && !Modifier.isProtected(m.getModifiers()));
}

From source file:com.clark.func.Functions.java

/**
 * XXX Default access superclass workaround
 * /*from   w w w  .  j a  va  2s.c o  m*/
 * When a public class has a default access superclass with public members,
 * these members are accessible. Calling them from compiled code works fine.
 * Unfortunately, on some JVMs, using reflection to invoke these members
 * seems to (wrongly) to prevent access even when the modifer is public.
 * Calling setAccessible(true) solves the problem but will only work from
 * sufficiently privileged code. Better workarounds would be gratefully
 * accepted.
 * 
 * @param o
 *            the AccessibleObject to set as accessible
 */
static void setAccessibleWorkaround(AccessibleObject o) {
    if (o == null || o.isAccessible()) {
        return;
    }
    Member m = (Member) o;
    if (Modifier.isPublic(m.getModifiers()) && isPackageAccess(m.getDeclaringClass().getModifiers())) {
        try {
            o.setAccessible(true);
        } catch (SecurityException e) {
            // ignore in favor of subsequent IllegalAccessException
        }
    }
}