Search in sources :

Example 6 with ProtoSchemaBuilderException

use of org.infinispan.protostream.annotations.ProtoSchemaBuilderException 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 ProtoSchemaBuilderException

use of org.infinispan.protostream.annotations.ProtoSchemaBuilderException in project protostream by infinispan.

the class ProtoEnumTypeMetadata method scanMemberAnnotations.

@Override
public void scanMemberAnnotations() {
    if (membersByNumber == null) {
        membersByNumber = new TreeMap<>();
        membersByName = new HashMap<>();
        for (XEnumConstant ec : annotatedEnumClass.getEnumConstants()) {
            ProtoEnumValue annotation = ec.getAnnotation(ProtoEnumValue.class);
            if (annotation == null) {
                throw new ProtoSchemaBuilderException("Enum constants must have the @ProtoEnumValue annotation: " + getAnnotatedClassName() + '.' + ec.getName());
            }
            int number = getNumber(annotation, ec);
            if (membersByNumber.containsKey(number)) {
                throw new ProtoSchemaBuilderException("Found duplicate definition of Protobuf enum tag " + number + " on enum constant: " + getAnnotatedClassName() + '.' + ec.getName() + " clashes with " + membersByNumber.get(number).getJavaEnumName());
            }
            String name = annotation.name();
            if (name.isEmpty()) {
                name = ec.getName();
            }
            if (membersByName.containsKey(name)) {
                throw new ProtoSchemaBuilderException("Found duplicate definition of Protobuf enum constant " + name + " on enum constant: " + getAnnotatedClassName() + '.' + ec.getName() + " clashes with " + membersByName.get(name).getJavaEnumName());
            }
            ProtoEnumValueMetadata pevm = new ProtoEnumValueMetadata(number, name, ec.getOrdinal(), getJavaClassName() + '.' + ec.getName(), ec.getDocumentation());
            membersByNumber.put(number, pevm);
            membersByName.put(pevm.getProtoName(), pevm);
            if (isAdapter()) {
                XEnumConstant enumConstant = javaClass.getEnumConstant(ec.getName());
                if (enumConstant == null) {
                    throw new ProtoSchemaBuilderException(getAnnotatedClassName() + '.' + ec.getName() + " does not have a corresponding enum value in " + getJavaClassName());
                }
            }
        }
        if (isAdapter()) {
            for (XEnumConstant ec : javaClass.getEnumConstants()) {
                XEnumConstant enumConstant = annotatedEnumClass.getEnumConstant(ec.getName());
                if (enumConstant == null) {
                    throw new ProtoSchemaBuilderException(getAnnotatedClassName() + " does not have a corresponding enum value for " + getJavaClassName() + '.' + ec.getName());
                }
            }
        }
        if (membersByNumber.isEmpty()) {
            throw new ProtoSchemaBuilderException("Enums must contain at least one value: " + getAnnotatedClassName());
        }
    }
}
Also used : ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) ProtoEnumValue(org.infinispan.protostream.annotations.ProtoEnumValue) XEnumConstant(org.infinispan.protostream.annotations.impl.types.XEnumConstant)

Example 8 with ProtoSchemaBuilderException

use of org.infinispan.protostream.annotations.ProtoSchemaBuilderException in project protostream by infinispan.

the class ProtoEnumTypeMetadata method getProtoName.

private static String getProtoName(XClass annotatedEnumClass, XClass enumClass) {
    ProtoName annotation = annotatedEnumClass.getAnnotation(ProtoName.class);
    ProtoEnum protoEnumAnnotation = annotatedEnumClass.getAnnotation(ProtoEnum.class);
    if (annotation != null) {
        if (protoEnumAnnotation != null) {
            throw new ProtoSchemaBuilderException("@ProtoEnum annotation cannot be used together with @ProtoName: " + annotatedEnumClass.getName());
        }
        return annotation.value().isEmpty() ? enumClass.getSimpleName() : annotation.value();
    }
    return protoEnumAnnotation == null || protoEnumAnnotation.name().isEmpty() ? enumClass.getSimpleName() : protoEnumAnnotation.name();
}
Also used : ProtoEnum(org.infinispan.protostream.annotations.ProtoEnum) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) ProtoName(org.infinispan.protostream.annotations.ProtoName)

Example 9 with ProtoSchemaBuilderException

use of org.infinispan.protostream.annotations.ProtoSchemaBuilderException 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 10 with ProtoSchemaBuilderException

use of org.infinispan.protostream.annotations.ProtoSchemaBuilderException in project protostream by infinispan.

the class ProtoSchemaBuilderTest method testDiscoveryWithoutAutoImport.

@Test
public void testDiscoveryWithoutAutoImport() throws Exception {
    SerializationContext ctx = createContext();
    try {
        new ProtoSchemaBuilder().fileName("DiscoveryWithoutAutoImport.proto").addClass(OuterMessage1.class).autoImportClasses(false).build(ctx);
        fail("ProtoSchemaBuilderException was expected");
    } catch (ProtoSchemaBuilderException e) {
        assertEquals("Found a reference to class org.infinispan.protostream.annotations.impl.ProtoSchemaBuilderTest.InnerMessage1" + " which was not added to the builder and 'autoImportClasses' is disabled.", e.getMessage());
    }
    // ensure that it starts working once we turn autoImportClasses on
    String schema = new ProtoSchemaBuilder().fileName("DiscoveryWithAutoImport.proto").addClass(OuterMessage1.class).autoImportClasses(true).build(ctx);
    assertTrue(schema.contains("message OuterMessage1"));
    assertTrue(schema.contains("message InnerMessage1"));
    assertTrue(schema.contains("baseField1"));
}
Also used : SerializationContext(org.infinispan.protostream.SerializationContext) ProtoSchemaBuilder(org.infinispan.protostream.annotations.ProtoSchemaBuilder) ProtoSchemaBuilderException(org.infinispan.protostream.annotations.ProtoSchemaBuilderException) Test(org.junit.Test) AbstractProtoStreamTest(org.infinispan.protostream.test.AbstractProtoStreamTest)

Aggregations

ProtoSchemaBuilderException (org.infinispan.protostream.annotations.ProtoSchemaBuilderException)18 XClass (org.infinispan.protostream.annotations.impl.types.XClass)12 Optional (java.util.Optional)4 SerializationContext (org.infinispan.protostream.SerializationContext)4 ArrayList (java.util.ArrayList)3 Collection (java.util.Collection)3 List (java.util.List)3 XMethod (org.infinispan.protostream.annotations.impl.types.XMethod)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 TypeMirror (javax.lang.model.type.TypeMirror)2 ProtoUnknownFieldSet (org.infinispan.protostream.annotations.ProtoUnknownFieldSet)2 XConstructor (org.infinispan.protostream.annotations.impl.types.XConstructor)2 AutoService (com.google.auto.service.AutoService)1 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 Instant (java.time.Instant)1