Search in sources :

Example 66 with Annotation

use of java.lang.annotation.Annotation in project orientdb by orientechnologies.

the class OConsoleApplication method formatCommandSpecs.

protected String formatCommandSpecs(final String iCommand, final Method m) {
    final StringBuilder buffer = new StringBuilder();
    final StringBuilder signature = new StringBuilder();
    signature.append(iCommand);
    String paramName = null;
    String paramDescription = null;
    boolean paramOptional = false;
    buffer.append("\n\nWHERE:\n\n");
    for (Annotation[] annotations : m.getParameterAnnotations()) {
        for (Annotation ann : annotations) {
            if (ann instanceof com.orientechnologies.common.console.annotation.ConsoleParameter) {
                paramName = ((com.orientechnologies.common.console.annotation.ConsoleParameter) ann).name();
                paramDescription = ((com.orientechnologies.common.console.annotation.ConsoleParameter) ann).description();
                paramOptional = ((com.orientechnologies.common.console.annotation.ConsoleParameter) ann).optional();
                break;
            }
        }
        if (paramName == null)
            paramName = "?";
        if (paramOptional)
            signature.append(" [<" + paramName + ">]");
        else
            signature.append(" <" + paramName + ">");
        buffer.append("* ");
        buffer.append(String.format("%-18s", paramName));
        if (paramDescription != null)
            buffer.append(paramDescription);
        if (paramOptional)
            buffer.append(" (optional)");
        buffer.append("\n");
    }
    signature.append(buffer);
    return signature.toString();
}
Also used : ConsoleParameter(com.orientechnologies.common.console.annotation.ConsoleParameter) Annotation(java.lang.annotation.Annotation)

Example 67 with Annotation

use of java.lang.annotation.Annotation in project orientdb by orientechnologies.

the class OObjectSerializerHelper method analyzeClass.

protected static List<Field> analyzeClass(final Class<?> iClass) {
    final List<Field> properties = new ArrayList<Field>();
    classes.put(iClass.getName(), properties);
    String fieldName;
    Class<?> fieldType;
    int fieldModifier;
    boolean autoBinding;
    for (Class<?> currentClass = iClass; currentClass != Object.class; ) {
        for (Field f : currentClass.getDeclaredFields()) {
            fieldModifier = f.getModifiers();
            if (Modifier.isStatic(fieldModifier) || Modifier.isNative(fieldModifier) || Modifier.isTransient(fieldModifier))
                continue;
            if (f.getName().equals("this$0"))
                continue;
            if (jpaTransientClass != null) {
                Annotation ann = f.getAnnotation(jpaTransientClass);
                if (ann != null)
                    // @Transient DEFINED, JUMP IT
                    continue;
            }
            fieldName = f.getName();
            fieldType = f.getType();
            properties.add(f);
            // CHECK FOR AUTO-BINDING
            autoBinding = true;
            if (f.getAnnotation(OAccess.class) == null || f.getAnnotation(OAccess.class).value() == OAccess.OAccessType.PROPERTY)
                autoBinding = true;
            else // JPA 2+ AVAILABLE?
            if (jpaAccessClass != null) {
                Annotation ann = f.getAnnotation(jpaAccessClass);
                if (ann != null) {
                    // TODO: CHECK IF CONTAINS VALUE=FIELD
                    autoBinding = true;
                }
            }
            if (f.getAnnotation(ODocumentInstance.class) != null)
                // BOUND DOCUMENT ON IT
                boundDocumentFields.put(iClass, f);
            boolean idFound = false;
            if (f.getAnnotation(OId.class) != null) {
                // RECORD ID
                fieldIds.put(iClass, f);
                idFound = true;
            } else // JPA 1+ AVAILABLE?
            if (jpaIdClass != null && f.getAnnotation(jpaIdClass) != null) {
                // RECORD ID
                fieldIds.put(iClass, f);
                idFound = true;
            }
            if (idFound) {
                // CHECK FOR TYPE
                if (fieldType.isPrimitive())
                    OLogManager.instance().warn(OObjectSerializerHelper.class, "Field '%s' cannot be a literal to manage the Record Id", f.toString());
                else if (!ORID.class.isAssignableFrom(fieldType) && fieldType != String.class && fieldType != Object.class && !Number.class.isAssignableFrom(fieldType))
                    OLogManager.instance().warn(OObjectSerializerHelper.class, "Field '%s' cannot be managed as type: %s", f.toString(), fieldType);
            }
            boolean vFound = false;
            if (f.getAnnotation(OVersion.class) != null) {
                // RECORD ID
                fieldVersions.put(iClass, f);
                vFound = true;
            } else // JPA 1+ AVAILABLE?
            if (jpaVersionClass != null && f.getAnnotation(jpaVersionClass) != null) {
                // RECORD ID
                fieldVersions.put(iClass, f);
                vFound = true;
            }
            if (vFound) {
                // CHECK FOR TYPE
                if (fieldType.isPrimitive())
                    OLogManager.instance().warn(OObjectSerializerHelper.class, "Field '%s' cannot be a literal to manage the Version", f.toString());
                else if (fieldType != String.class && fieldType != Object.class && !Number.class.isAssignableFrom(fieldType))
                    OLogManager.instance().warn(OObjectSerializerHelper.class, "Field '%s' cannot be managed as type: %s", f.toString(), fieldType);
            }
            // JPA 1+ AVAILABLE?
            if (jpaEmbeddedClass != null && f.getAnnotation(jpaEmbeddedClass) != null) {
                if (embeddedFields.get(iClass) == null)
                    embeddedFields.put(iClass, new ArrayList<String>());
                embeddedFields.get(iClass).add(fieldName);
            }
            if (autoBinding)
                // TRY TO GET THE VALUE BY THE GETTER (IF ANY)
                try {
                    String getterName = "get" + OUtils.camelCase(fieldName);
                    Method m = currentClass.getMethod(getterName, NO_ARGS);
                    getters.put(iClass.getName() + "." + fieldName, m);
                } catch (Exception e) {
                    registerFieldGetter(iClass, fieldName, f);
                }
            else
                registerFieldGetter(iClass, fieldName, f);
            if (autoBinding)
                // TRY TO GET THE VALUE BY THE SETTER (IF ANY)
                try {
                    String getterName = "set" + OUtils.camelCase(fieldName);
                    Method m = currentClass.getMethod(getterName, f.getType());
                    setters.put(iClass.getName() + "." + fieldName, m);
                } catch (Exception e) {
                    registerFieldSetter(iClass, fieldName, f);
                }
            else
                registerFieldSetter(iClass, fieldName, f);
        }
        registerCallbacks(iClass, currentClass);
        currentClass = currentClass.getSuperclass();
        if (currentClass.equals(ODocument.class))
            // POJO EXTENDS ODOCUMENT: SPECIAL CASE: AVOID TO CONSIDER
            // ODOCUMENT FIELDS
            currentClass = Object.class;
    }
    return properties;
}
Also used : OVersion(com.orientechnologies.orient.core.annotation.OVersion) ArrayList(java.util.ArrayList) OAccess(com.orientechnologies.orient.core.annotation.OAccess) ODocumentInstance(com.orientechnologies.orient.core.annotation.ODocumentInstance) OId(com.orientechnologies.orient.core.annotation.OId) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) OException(com.orientechnologies.common.exception.OException) OSchemaException(com.orientechnologies.orient.core.exception.OSchemaException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) OObjectNotDetachedException(com.orientechnologies.orient.object.db.OObjectNotDetachedException) OSerializationException(com.orientechnologies.orient.core.exception.OSerializationException) Field(java.lang.reflect.Field) ODatabaseObject(com.orientechnologies.orient.core.db.object.ODatabaseObject) ORID(com.orientechnologies.orient.core.id.ORID)

Example 68 with Annotation

use of java.lang.annotation.Annotation in project powermock by powermock.

the class TestClassTransformer method forTestClass.

public static ForTestClass forTestClass(final Class<?> testClass) {
    return new ForTestClass() {

        @Override
        public RemovesTestMethodAnnotation removesTestMethodAnnotation(final Class<? extends Annotation> testMethodAnnotation) {
            return new RemovesTestMethodAnnotation() {

                @Override
                public TestClassTransformer fromMethods(final Collection<Method> testMethodsThatRunOnOtherClassLoaders) {
                    return new TestClassTransformer(testClass, testMethodAnnotation) {

                        /**
                             * Is lazily initilized because of
                             * AbstractTestSuiteChunkerImpl#chunkClass(Class)
                             */
                        Collection<String> methodsThatRunOnOtherClassLoaders;

                        @Override
                        boolean mustHaveTestAnnotationRemoved(CtMethod method) throws NotFoundException {
                            if (null == methodsThatRunOnOtherClassLoaders) {
                                /* This lazy initialization is necessary - see above */
                                methodsThatRunOnOtherClassLoaders = new HashSet<String>();
                                for (Method m : testMethodsThatRunOnOtherClassLoaders) {
                                    methodsThatRunOnOtherClassLoaders.add(signatureOf(m));
                                }
                                testMethodsThatRunOnOtherClassLoaders.clear();
                            }
                            return methodsThatRunOnOtherClassLoaders.contains(signatureOf(method));
                        }
                    };
                }

                @Override
                public TestClassTransformer fromAllMethodsExcept(Method singleMethodToRunOnTargetClassLoader) {
                    final String targetMethodSignature = signatureOf(singleMethodToRunOnTargetClassLoader);
                    return new TestClassTransformer(testClass, testMethodAnnotation) {

                        @Override
                        boolean mustHaveTestAnnotationRemoved(CtMethod method) throws Exception {
                            return !signatureOf(method).equals(targetMethodSignature);
                        }
                    };
                }
            };
        }
    };
}
Also used : Collection(java.util.Collection) CtClass(javassist.CtClass) IndicateReloadClass(org.powermock.core.IndicateReloadClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) CtMethod(javassist.CtMethod)

Example 69 with Annotation

use of java.lang.annotation.Annotation in project powermock by powermock.

the class PowerMockStatement method clearMockFields.

private void clearMockFields(Object target, Object annotationEnabler) throws Exception {
    if (annotationEnabler != null) {
        Class<? extends Annotation>[] mockAnnotations = Whitebox.invokeMethod(annotationEnabler, "getMockAnnotations");
        Set<Field> mockFields = Whitebox.getFieldsAnnotatedWith(target, mockAnnotations);
        for (Field field : mockFields) {
            field.set(target, null);
        }
    }
}
Also used : Field(java.lang.reflect.Field) Annotation(java.lang.annotation.Annotation)

Example 70 with Annotation

use of java.lang.annotation.Annotation in project powermock by powermock.

the class PowerMockTestCase method clearMockFields.

private void clearMockFields() throws Exception, IllegalAccessException {
    if (annotationEnabler != null) {
        final Class<? extends Annotation>[] mockAnnotations = Whitebox.invokeMethod(annotationEnabler, "getMockAnnotations");
        Set<Field> mockFields = Whitebox.getFieldsAnnotatedWith(this, mockAnnotations);
        for (Field field : mockFields) {
            field.set(this, null);
        }
    }
}
Also used : Field(java.lang.reflect.Field) AfterClass(org.testng.annotations.AfterClass) BeforeClass(org.testng.annotations.BeforeClass) Annotation(java.lang.annotation.Annotation)

Aggregations

Annotation (java.lang.annotation.Annotation)707 Method (java.lang.reflect.Method)171 ArrayList (java.util.ArrayList)99 Field (java.lang.reflect.Field)76 Test (org.junit.Test)66 Type (java.lang.reflect.Type)64 HashMap (java.util.HashMap)54 HashSet (java.util.HashSet)52 Map (java.util.Map)35 ParameterizedType (java.lang.reflect.ParameterizedType)34 List (java.util.List)30 Set (java.util.Set)27 InvocationTargetException (java.lang.reflect.InvocationTargetException)22 IOException (java.io.IOException)20 BindingAnnotation (com.google.inject.BindingAnnotation)17 AbstractModule (com.google.inject.AbstractModule)16 TypeElement (javax.lang.model.element.TypeElement)15 Injector (com.google.inject.Injector)14 MediaType (okhttp3.MediaType)14 AnnotatedElement (java.lang.reflect.AnnotatedElement)13