Search in sources :

Example 71 with Member

use of java.lang.reflect.Member in project geode by apache.

the class AttributeDescriptor method readReflection.

// used when the resolution of an attribute must be on a superclass
// instead of the runtime class
private Object readReflection(Object target) throws NameNotFoundException, QueryInvocationTargetException {
    Support.Assert(target != null);
    Support.Assert(target != QueryService.UNDEFINED);
    if (target instanceof Token) {
        return QueryService.UNDEFINED;
    }
    Class resolutionClass = target.getClass();
    Member m = getReadMember(resolutionClass);
    try {
        if (m instanceof Method) {
            try {
                return ((Method) m).invoke(target, (Object[]) null);
            } catch (EntryDestroyedException e) {
                // eat the Exception
                return QueryService.UNDEFINED;
            } catch (IllegalAccessException e) {
                throw new NameNotFoundException(LocalizedStrings.AttributeDescriptor_METHOD_0_IN_CLASS_1_IS_NOT_ACCESSIBLE_TO_THE_QUERY_PROCESSOR.toLocalizedString(new Object[] { m.getName(), target.getClass().getName() }), e);
            } catch (InvocationTargetException e) {
                // if the target exception is Exception, wrap that,
                // otherwise wrap the InvocationTargetException itself
                Throwable t = e.getTargetException();
                if ((t instanceof EntryDestroyedException)) {
                    // eat the exception
                    return QueryService.UNDEFINED;
                }
                if (t instanceof Exception)
                    throw new QueryInvocationTargetException(t);
                throw new QueryInvocationTargetException(e);
            }
        } else {
            try {
                return ((Field) m).get(target);
            } catch (IllegalAccessException e) {
                throw new NameNotFoundException(LocalizedStrings.AttributeDescriptor_FIELD_0_IN_CLASS_1_IS_NOT_ACCESSIBLE_TO_THE_QUERY_PROCESSOR.toLocalizedString(new Object[] { m.getName(), target.getClass().getName() }), e);
            } catch (EntryDestroyedException e) {
                return QueryService.UNDEFINED;
            }
        }
    } catch (EntryDestroyedException e) {
        // eat the exception
        return QueryService.UNDEFINED;
    }
}
Also used : EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) NameNotFoundException(org.apache.geode.cache.query.NameNotFoundException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Token(org.apache.geode.internal.cache.Token) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) PdxSerializationException(org.apache.geode.pdx.PdxSerializationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NameNotFoundException(org.apache.geode.cache.query.NameNotFoundException) EntryDestroyedException(org.apache.geode.cache.EntryDestroyedException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) Field(java.lang.reflect.Field) AccessibleObject(java.lang.reflect.AccessibleObject) Member(java.lang.reflect.Member)

Example 72 with Member

use of java.lang.reflect.Member in project Entitas-Java by Rubentxu.

the class TypeResolver method getMemberRef.

private static Member getMemberRef(Class<?> type) {
    Object constantPool;
    try {
        constantPool = GET_CONSTANT_POOL.invoke(type);
    } catch (Exception ignore) {
        return null;
    }
    Member result = null;
    for (int i = getConstantPoolSize(constantPool) - 1; i >= 0; i--) {
        Member member = getConstantPoolMethodAt(constantPool, i);
        // Skip SerializedLambda constructors and members of the "type" class
        if (member == null || (member instanceof Constructor && member.getDeclaringClass().getName().equals("java.lang.invoke.SerializedLambda")) || member.getDeclaringClass().isAssignableFrom(type))
            continue;
        result = member;
        // Return if not valueOf method
        if (!(member instanceof Method) || !isAutoBoxingMethod((Method) member))
            break;
    }
    return result;
}
Also used : Constructor(java.lang.reflect.Constructor) AccessibleObject(java.lang.reflect.AccessibleObject) Method(java.lang.reflect.Method) Member(java.lang.reflect.Member)

Example 73 with Member

use of java.lang.reflect.Member in project qi4j-sdk by Qi4j.

the class EntityAssemblyImpl method newNamedAssociationModel.

public NamedAssociationModel newNamedAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for entities in NamedAssociation
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the NamedAssociation itself
    valueConstraintsModel = constraintsFor(annotations, NamedAssociation.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance namedValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        namedValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = namedAssociationDeclarations.metaInfoFor(accessor);
    NamedAssociationModel associationModel = new NamedAssociationModel(accessor, valueConstraintsInstance, namedValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Optional(org.qi4j.api.common.Optional) NamedAssociation(org.qi4j.api.association.NamedAssociation) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation)

Example 74 with Member

use of java.lang.reflect.Member in project qi4j-sdk by Qi4j.

the class ValueAssemblyImpl method newNamedAssociationModel.

public NamedAssociationModel newNamedAssociationModel(AccessibleObject accessor, Iterable<Class<? extends Constraint<?, ?>>> constraintClasses) {
    Iterable<Annotation> annotations = Annotations.findAccessorAndTypeAnnotationsIn(accessor);
    boolean optional = first(filter(isType(Optional.class), annotations)) != null;
    // Constraints for entities in NamedAssociation
    ValueConstraintsModel valueConstraintsModel = constraintsFor(annotations, GenericAssociationInfo.associationTypeOf(accessor), ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance valueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        valueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    // Constraints for the NamedAssociation itself
    valueConstraintsModel = constraintsFor(annotations, NamedAssociation.class, ((Member) accessor).getName(), optional, constraintClasses, accessor);
    ValueConstraintsInstance namedValueConstraintsInstance = null;
    if (valueConstraintsModel.isConstrained()) {
        namedValueConstraintsInstance = valueConstraintsModel.newInstance();
    }
    MetaInfo metaInfo = stateDeclarations.metaInfoFor(accessor);
    NamedAssociationModel associationModel = new NamedAssociationModel(accessor, valueConstraintsInstance, namedValueConstraintsInstance, metaInfo);
    return associationModel;
}
Also used : Optional(org.qi4j.api.common.Optional) NamedAssociation(org.qi4j.api.association.NamedAssociation) MetaInfo(org.qi4j.api.common.MetaInfo) ValueConstraintsModel(org.qi4j.runtime.composite.ValueConstraintsModel) NamedAssociationModel(org.qi4j.runtime.association.NamedAssociationModel) ValueConstraintsInstance(org.qi4j.runtime.composite.ValueConstraintsInstance) Member(java.lang.reflect.Member) Annotation(java.lang.annotation.Annotation)

Example 75 with Member

use of java.lang.reflect.Member in project mvel by mvel.

the class ASMAccessorOptimizer method optimizeSetAccessor.

public Accessor optimizeSetAccessor(ParserContext pCtx, char[] property, int start, int offset, Object ctx, Object thisRef, VariableResolverFactory factory, boolean rootThisRef, Object value, Class ingressType) {
    this.expr = property;
    this.start = this.cursor = start;
    this.end = start + offset;
    this.length = start + offset;
    this.first = true;
    this.ingressType = ingressType;
    compiledInputs = new ArrayList<ExecutableStatement>();
    this.pCtx = pCtx;
    this.ctx = ctx;
    this.thisRef = thisRef;
    this.variableFactory = factory;
    char[] root = null;
    PropertyVerifier verifier = new PropertyVerifier(property, this.pCtx = pCtx);
    int split = findLastUnion();
    if (split != -1) {
        root = subset(property, 0, split);
    }
    AccessorNode rootAccessor = null;
    _initJIT2();
    if (root != null) {
        int _length = this.length;
        int _end = this.end;
        char[] _expr = this.expr;
        this.length = end = (this.expr = root).length;
        // run the compiler but don't finish building.
        deferFinish = true;
        noinit = true;
        compileAccessor();
        ctx = this.val;
        this.expr = _expr;
        this.cursor = start + root.length + 1;
        this.length = _length - root.length - 1;
        this.end = this.cursor + this.length;
    } else {
        assert debug("ALOAD 1");
        mv.visitVarInsn(ALOAD, 1);
    }
    try {
        skipWhitespace();
        if (collection) {
            int st = cursor;
            whiteSpaceSkip();
            if (st == end)
                throw new PropertyAccessException("unterminated '['", expr, start, pCtx);
            if (scanTo(']'))
                throw new PropertyAccessException("unterminated '['", expr, start, pCtx);
            String ex = new String(expr, st, cursor - st).trim();
            assert debug("CHECKCAST " + ctx.getClass().getName());
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            if (ctx instanceof Map) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Map.class)) {
                    propHandlerByteCodePut(ex, ctx, Map.class, value);
                } else {
                    // noinspection unchecked
                    ((Map) ctx).put(eval(ex, ctx, variableFactory), convert(value, returnType = verifier.analyze()));
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (value != null && returnType != value.getClass()) {
                        dataConversion(returnType);
                        checkcast(returnType);
                    }
                    assert debug("INVOKEINTERFACE Map.put");
                    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
                    assert debug("POP");
                    mv.visitInsn(POP);
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else if (ctx instanceof List) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(List.class)) {
                    propHandlerByteCodePut(ex, ctx, List.class, value);
                } else {
                    // noinspection unchecked
                    ((List) ctx).set(eval(ex, ctx, variableFactory, Integer.class), convert(value, returnType = verifier.analyze()));
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx));
                    unwrapPrimitive(int.class);
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (value != null && !value.getClass().isAssignableFrom(returnType)) {
                        dataConversion(returnType);
                        checkcast(returnType);
                    }
                    assert debug("INVOKEINTERFACE List.set");
                    mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "set", "(ILjava/lang/Object;)Ljava/lang/Object;");
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(ctx.getClass())) {
                propHandlerByteCodePut(ex, ctx, ctx.getClass(), value);
            } else if (ctx.getClass().isArray()) {
                if (MVEL.COMPILER_OPT_ALLOW_OVERRIDE_ALL_PROPHANDLING && hasPropertyHandler(Array.class)) {
                    propHandlerByteCodePut(ex, ctx, Array.class, value);
                } else {
                    Class type = getBaseComponentType(ctx.getClass());
                    Object idx = eval(ex, ctx, variableFactory);
                    writeLiteralOrSubexpression(subCompileExpression(ex.toCharArray(), pCtx), int.class);
                    if (!(idx instanceof Integer)) {
                        dataConversion(Integer.class);
                        idx = DataConversion.convert(idx, Integer.class);
                        unwrapPrimitive(int.class);
                    }
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    if (type.isPrimitive())
                        unwrapPrimitive(type);
                    else if (!type.equals(value.getClass())) {
                        dataConversion(type);
                    }
                    arrayStore(type);
                    // noinspection unchecked
                    Array.set(ctx, (Integer) idx, convert(value, type));
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                }
            } else {
                throw new PropertyAccessException("cannot bind to collection property: " + new String(expr) + ": not a recognized collection type: " + ctx.getClass(), expr, start, pCtx);
            }
            deferFinish = false;
            noinit = false;
            _finishJIT();
            try {
                deferFinish = false;
                return _initializeAccessor();
            } catch (Exception e) {
                throw new CompileException("could not generate accessor", expr, start, e);
            }
        }
        String tk = new String(expr, this.cursor, this.length);
        Member member = getFieldOrWriteAccessor(ctx.getClass(), tk, value == null ? null : ingressType);
        if (GlobalListenerFactory.hasSetListeners()) {
            mv.visitVarInsn(ALOAD, 1);
            mv.visitLdcInsn(tk);
            mv.visitVarInsn(ALOAD, 3);
            mv.visitVarInsn(ALOAD, 4);
            mv.visitMethodInsn(INVOKESTATIC, NAMESPACE + "integration/GlobalListenerFactory", "notifySetListeners", "(Ljava/lang/Object;Ljava/lang/String;L" + NAMESPACE + "integration/VariableResolverFactory;Ljava/lang/Object;)V");
            GlobalListenerFactory.notifySetListeners(ctx, tk, variableFactory, value);
        }
        if (member instanceof Field) {
            checkcast(ctx.getClass());
            Field fld = (Field) member;
            Label jmp = null;
            Label jmp2 = new Label();
            if (fld.getType().isPrimitive()) {
                assert debug("ASTORE 5");
                mv.visitVarInsn(ASTORE, 5);
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                if (value == null)
                    value = PropertyTools.getPrimitiveInitialValue(fld.getType());
                jmp = new Label();
                assert debug("IFNOTNULL jmp");
                mv.visitJumpInsn(IFNONNULL, jmp);
                assert debug("ALOAD 5");
                mv.visitVarInsn(ALOAD, 5);
                assert debug("ICONST_0");
                mv.visitInsn(ICONST_0);
                assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
                mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
                assert debug("GOTO jmp2");
                mv.visitJumpInsn(GOTO, jmp2);
                assert debug("jmp:");
                mv.visitLabel(jmp);
                assert debug("ALOAD 5");
                mv.visitVarInsn(ALOAD, 5);
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                unwrapPrimitive(fld.getType());
            } else {
                assert debug("ALOAD 4");
                mv.visitVarInsn(ALOAD, 4);
                checkcast(fld.getType());
            }
            if (jmp == null && value != null && !fld.getType().isAssignableFrom(value.getClass())) {
                if (!canConvert(fld.getType(), value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + fld.getType(), expr, start);
                }
                dataConversion(fld.getType());
                fld.set(ctx, convert(value, fld.getType()));
            } else {
                fld.set(ctx, value);
            }
            assert debug("PUTFIELD " + getInternalName(fld.getDeclaringClass()) + "." + tk);
            mv.visitFieldInsn(PUTFIELD, getInternalName(fld.getDeclaringClass()), tk, getDescriptor(fld.getType()));
            assert debug("jmp2:");
            mv.visitLabel(jmp2);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
        } else if (member != null) {
            assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            Method meth = (Method) member;
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            Class targetType = meth.getParameterTypes()[0];
            Label jmp;
            Label jmp2 = new Label();
            if (value != null && !targetType.isAssignableFrom(value.getClass())) {
                if (!canConvert(targetType, value.getClass())) {
                    throw new CompileException("cannot convert type: " + value.getClass() + ": to " + meth.getParameterTypes()[0], expr, start);
                }
                dataConversion(getWrapperClass(targetType));
                if (targetType.isPrimitive()) {
                    unwrapPrimitive(targetType);
                } else
                    checkcast(targetType);
                meth.invoke(ctx, convert(value, meth.getParameterTypes()[0]));
            } else {
                if (targetType.isPrimitive()) {
                    if (value == null)
                        value = PropertyTools.getPrimitiveInitialValue(targetType);
                    jmp = new Label();
                    assert debug("IFNOTNULL jmp");
                    mv.visitJumpInsn(IFNONNULL, jmp);
                    assert debug("ICONST_0");
                    mv.visitInsn(ICONST_0);
                    assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
                    mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
                    assert debug("GOTO jmp2");
                    mv.visitJumpInsn(GOTO, jmp2);
                    assert debug("jmp:");
                    mv.visitLabel(jmp);
                    assert debug("ALOAD 4");
                    mv.visitVarInsn(ALOAD, 4);
                    unwrapPrimitive(targetType);
                } else {
                    checkcast(targetType);
                }
                meth.invoke(ctx, value);
            }
            assert debug("INVOKEVIRTUAL " + getInternalName(meth.getDeclaringClass()) + "." + meth.getName());
            mv.visitMethodInsn(INVOKEVIRTUAL, getInternalName(meth.getDeclaringClass()), meth.getName(), getMethodDescriptor(meth));
            assert debug("jmp2:");
            mv.visitLabel(jmp2);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
        } else if (ctx instanceof Map) {
            assert debug("CHECKCAST " + getInternalName(ctx.getClass()));
            mv.visitTypeInsn(CHECKCAST, getInternalName(ctx.getClass()));
            assert debug("LDC '" + tk + "'");
            mv.visitLdcInsn(tk);
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            assert debug("INVOKEINTERFACE java/util/Map.put");
            mv.visitMethodInsn(INVOKEINTERFACE, "java/util/Map", "put", "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
            assert debug("ALOAD 4");
            mv.visitVarInsn(ALOAD, 4);
            // noinspection unchecked
            ((Map) ctx).put(tk, value);
        } else {
            throw new PropertyAccessException("could not access property (" + tk + ") in: " + ingressType.getName(), expr, start, pCtx);
        }
    } catch (InvocationTargetException e) {
        throw new PropertyAccessException("could not access property", expr, start, e, pCtx);
    } catch (IllegalAccessException e) {
        throw new PropertyAccessException("could not access property", expr, start, e, pCtx);
    }
    try {
        deferFinish = false;
        noinit = false;
        _finishJIT();
        return _initializeAccessor();
    } catch (Exception e) {
        throw new CompileException("could not generate accessor", expr, start, e);
    }
}
Also used : ExecutableStatement(org.mvel2.compiler.ExecutableStatement) PropertyVerifier(org.mvel2.compiler.PropertyVerifier) Label(org.mvel2.asm.Label) PropertyAccessException(org.mvel2.PropertyAccessException) Method(java.lang.reflect.Method) CompileException(org.mvel2.CompileException) InvocationTargetException(java.lang.reflect.InvocationTargetException) PropertyAccessException(org.mvel2.PropertyAccessException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Array(java.lang.reflect.Array) ReflectionUtil.toNonPrimitiveArray(org.mvel2.util.ReflectionUtil.toNonPrimitiveArray) Field(java.lang.reflect.Field) AccessorNode(org.mvel2.compiler.AccessorNode) CompileException(org.mvel2.CompileException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) Member(java.lang.reflect.Member)

Aggregations

Member (java.lang.reflect.Member)135 Method (java.lang.reflect.Method)41 Field (java.lang.reflect.Field)30 ArrayList (java.util.ArrayList)13 AccessibleObject (java.lang.reflect.AccessibleObject)12 Type (java.lang.reflect.Type)12 Annotation (java.lang.annotation.Annotation)11 Constructor (java.lang.reflect.Constructor)10 TypeVariable (java.lang.reflect.TypeVariable)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)8 ParameterizedType (java.lang.reflect.ParameterizedType)7 Map (java.util.Map)7 MetaInfo (org.qi4j.api.common.MetaInfo)7 InjectionPoint (com.google.inject.spi.InjectionPoint)6 AnnotatedElement (java.lang.reflect.AnnotatedElement)6 LinkedHashSet (java.util.LinkedHashSet)6 Optional (org.qi4j.api.common.Optional)6 ValueConstraintsInstance (org.qi4j.runtime.composite.ValueConstraintsInstance)6 ValueConstraintsModel (org.qi4j.runtime.composite.ValueConstraintsModel)6 HashSet (java.util.HashSet)5