Search in sources :

Example 6 with XClass

use of org.infinispan.protostream.annotations.impl.types.XClass in project protostream by infinispan.

the class ProtoMessageTypeMetadata method findGetter.

private XMethod findGetter(String propertyName, XClass propertyType) {
    boolean isBoolean = propertyType == typeFactory.fromClass(boolean.class) || propertyType == typeFactory.fromClass(Boolean.class);
    String methodName = (isBoolean ? "is" : "get") + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
    if (isAdapter) {
        // lookup a java-bean style method first
        XMethod getter = annotatedClass.getMethod(methodName);
        if (getter == null && isBoolean) {
            // retry with 'get' instead of 'is'
            methodName = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
            getter = annotatedClass.getMethod(methodName, javaClass);
        }
        if (getter == null) {
            // try the property name directly
            getter = annotatedClass.getMethod(propertyName, javaClass);
        }
        if (getter == null) {
            throw new ProtoSchemaBuilderException("No getter method found for property '" + propertyName + "' of type " + propertyType.getCanonicalName() + " in class " + getAnnotatedClassName());
        }
        XClass returnType = getter.getReturnType();
        if (returnType == typeFactory.fromClass(Optional.class)) {
            returnType = getter.determineOptionalReturnType();
        }
        if (returnType != propertyType) {
            throw new ProtoSchemaBuilderException("No suitable getter method found for property '" + propertyName + "' of type " + propertyType.getCanonicalName() + " in class " + getAnnotatedClassName() + ". The candidate method does not have a suitable return type: " + getter);
        }
        return getter;
    } else {
        // lookup a java-bean style method first
        XMethod getter = javaClass.getMethod(methodName);
        if (getter == null && isBoolean) {
            // retry with 'get' instead of 'is'
            methodName = "get" + Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
            getter = javaClass.getMethod(methodName);
        }
        if (getter == null) {
            // try the property name directly
            getter = javaClass.getMethod(propertyName);
        }
        if (getter == null) {
            throw new ProtoSchemaBuilderException("No getter method found for property '" + propertyName + "' of type " + propertyType.getCanonicalName() + " in class " + javaClass.getCanonicalName());
        }
        XClass returnType = getter.getReturnType();
        if (returnType == typeFactory.fromClass(Optional.class)) {
            returnType = getter.determineOptionalReturnType();
        }
        if (returnType != propertyType) {
            throw new ProtoSchemaBuilderException("No suitable getter method found for property '" + propertyName + "' of type " + propertyType.getCanonicalName() + " in class " + javaClass.getCanonicalName() + ". The candidate method does not have a suitable return type: " + getter);
        }
        return getter;
    }
}
Also used : ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) Optional(java.util.Optional) XMethod(org.infinispan.protostream.annotations.impl.types.XMethod) XClass(org.infinispan.protostream.annotations.impl.types.XClass)

Example 7 with XClass

use of org.infinispan.protostream.annotations.impl.types.XClass in project protostream by infinispan.

the class CompileTimeProtoSchemaGenerator method getAdapterFor.

@Override
protected XClass getAdapterFor(XClass annotatedClass) {
    ProtoAdapter protoAdapter;
    try {
        protoAdapter = annotatedClass.getAnnotation(ProtoAdapter.class);
        if (protoAdapter == null) {
            return null;
        }
    } catch (ClassCastException e) {
        // javac soiling pants
        throw new ProtoSchemaBuilderException("The class referenced by the ProtoAdapter annotation " + "does not exist, possibly due to compilation errors in your source code or due to " + "incremental compilation issues caused by your build system. Please try a clean rebuild.");
    }
    // TODO [anistor] also ensure that typeMirror is not part of current serCtxInit and is not scanned for @ProtoXyz annotations even if present
    TypeMirror typeMirror = DangerousActions.getTypeMirror(protoAdapter, ProtoAdapter::value);
    XClass target = ((MirrorTypeFactory) typeFactory).fromTypeMirror(typeMirror);
    if (target == annotatedClass) {
        throw new ProtoSchemaBuilderException(annotatedClass.getName() + " has an invalid @ProtoAdapter annotation pointing to self");
    }
    return target;
}
Also used : ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) TypeMirror(javax.lang.model.type.TypeMirror) ProtoAdapter(org.infinispan.protostream.annotations.ProtoAdapter) XClass(org.infinispan.protostream.annotations.impl.types.XClass) MirrorTypeFactory(org.infinispan.protostream.annotations.impl.processor.types.MirrorTypeFactory)

Example 8 with XClass

use of org.infinispan.protostream.annotations.impl.types.XClass in project protostream by infinispan.

the class MirrorTypeFactory method fromClass.

@Override
public XClass fromClass(Class<?> c) {
    if (c == null) {
        return null;
    }
    if (c == byte[].class) {
        XClass componentType = byteType;
        String fqn = "[" + componentType.getName();
        XClass xclass = classCache.get(fqn);
        if (xclass == null) {
            xclass = new MirrorArray(componentType);
            classCache.put(fqn, xclass);
        }
        return xclass;
    }
    if (c == void.class) {
        return voidType;
    }
    if (c == boolean.class) {
        return booleanType;
    }
    if (c == byte.class) {
        return byteType;
    }
    if (c == short.class) {
        return shortType;
    }
    if (c == int.class) {
        return intType;
    }
    if (c == long.class) {
        return longType;
    }
    if (c == char.class) {
        return charType;
    }
    if (c == float.class) {
        return floatType;
    }
    if (c == double.class) {
        return doubleType;
    }
    String typeName = c.getCanonicalName();
    if (typeName == null) {
        typeName = c.getName();
    }
    TypeElement typeElement = elements.getTypeElement(typeName);
    if (typeElement == null) {
        // this should never happen because once we have a java.lang.Class instance we should always be able to obtain its TypeElement
        throw new IllegalStateException("Type not found : " + typeName);
    }
    return fromTypeMirror(typeElement.asType());
}
Also used : TypeElement(javax.lang.model.element.TypeElement) XClass(org.infinispan.protostream.annotations.impl.types.XClass)

Example 9 with XClass

use of org.infinispan.protostream.annotations.impl.types.XClass in project protostream by infinispan.

the class MirrorTypeFactoryTest method testFromTypeMirror.

@Test
public void testFromTypeMirror() {
    ProcessingEnvironment processingEnvironmentMock = mock(ProcessingEnvironment.class);
    Elements elementsMock = mock(Elements.class);
    Types typesMock = mock(Types.class);
    when(processingEnvironmentMock.getElementUtils()).thenReturn(elementsMock);
    when(processingEnvironmentMock.getTypeUtils()).thenReturn(typesMock);
    TypeElement typeElementMock = mock(TypeElement.class);
    DeclaredType typeMirrorMock = mock(DeclaredType.class);
    when(typeMirrorMock.asElement()).thenReturn(typeElementMock);
    when(typeElementMock.asType()).thenReturn(typeMirrorMock);
    when(typeMirrorMock.getKind()).thenReturn(TypeKind.DECLARED);
    when(typeElementMock.getKind()).thenReturn(ElementKind.CLASS);
    Name nameMock = mock(Name.class);
    when(nameMock.toString()).thenReturn("java.lang.Integer");
    when(typeElementMock.getQualifiedName()).thenReturn(nameMock);
    when(elementsMock.getTypeElement("java.lang.Integer")).thenReturn(typeElementMock);
    when(elementsMock.getBinaryName(typeElementMock)).thenReturn(nameMock);
    MirrorTypeFactory typeFactory = new MirrorTypeFactory(processingEnvironmentMock);
    XClass integerClass = typeFactory.fromTypeMirror(typeMirrorMock);
    assertFalse(integerClass.isArray());
}
Also used : Types(javax.lang.model.util.Types) TypeElement(javax.lang.model.element.TypeElement) Elements(javax.lang.model.util.Elements) ProcessingEnvironment(javax.annotation.processing.ProcessingEnvironment) XClass(org.infinispan.protostream.annotations.impl.types.XClass) DeclaredType(javax.lang.model.type.DeclaredType) Name(javax.lang.model.element.Name) Test(org.junit.Test)

Example 10 with XClass

use of org.infinispan.protostream.annotations.impl.types.XClass in project protostream by infinispan.

the class ReservedProcessorTest method testEmpty.

@Test
public void testEmpty() {
    XClass classToTest = new ReflectionTypeFactory().fromClass(Integer.class);
    ReservedProcessor rp = new ReservedProcessor();
    rp.scan(classToTest);
    IndentWriter iw = new IndentWriter();
    rp.generate(iw);
    assertEquals("", iw.toString());
}
Also used : XClass(org.infinispan.protostream.annotations.impl.types.XClass) ReflectionTypeFactory(org.infinispan.protostream.annotations.impl.types.ReflectionTypeFactory) Test(org.junit.Test)

Aggregations

XClass (org.infinispan.protostream.annotations.impl.types.XClass)24 ProtoSchemaBuilderException (org.infinispan.protostream.annotations.ProtoSchemaBuilderException)11 Test (org.junit.Test)6 ReflectionTypeFactory (org.infinispan.protostream.annotations.impl.types.ReflectionTypeFactory)5 ArrayList (java.util.ArrayList)3 Optional (java.util.Optional)3 TypeElement (javax.lang.model.element.TypeElement)3 XConstructor (org.infinispan.protostream.annotations.impl.types.XConstructor)3 Collection (java.util.Collection)2 List (java.util.List)2 ProcessingEnvironment (javax.annotation.processing.ProcessingEnvironment)2 Name (javax.lang.model.element.Name)2 DeclaredType (javax.lang.model.type.DeclaredType)2 TypeMirror (javax.lang.model.type.TypeMirror)2 Elements (javax.lang.model.util.Elements)2 Types (javax.lang.model.util.Types)2 ProtoUnknownFieldSet (org.infinispan.protostream.annotations.ProtoUnknownFieldSet)2 XMethod (org.infinispan.protostream.annotations.impl.types.XMethod)2 JavaType (org.infinispan.protostream.descriptors.JavaType)2 Type (org.infinispan.protostream.descriptors.Type)2