Search in sources :

Example 21 with XClass

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

the class ReservedProcessorTest method testDuplicateReservedName.

@Test
public void testDuplicateReservedName() {
    expectedException.expect(ProtoSchemaBuilderException.class);
    expectedException.expectMessage("Found duplicate @ProtoReserved name \"triceratops\" in org.infinispan.protostream.annotations.impl.ReservedProcessorTest.DuplicateName");
    XClass classToTest = new ReflectionTypeFactory().fromClass(DuplicateName.class);
    new ReservedProcessor().scan(classToTest);
}
Also used : XClass(org.infinispan.protostream.annotations.impl.types.XClass) ReflectionTypeFactory(org.infinispan.protostream.annotations.impl.types.ReflectionTypeFactory) Test(org.junit.Test)

Example 22 with XClass

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

the class ReservedProcessorTest method testReservedNumbers.

@Test
public void testReservedNumbers() {
    XClass classToTest = new ReflectionTypeFactory().fromClass(TestingReserved.class);
    ReservedProcessor rp = new ReservedProcessor();
    rp.scan(classToTest);
    IndentWriter iw = new IndentWriter();
    rp.generate(iw);
    assertEquals("//reserved 1 to 7;\n//reserved \"a\", \"b\";\n", iw.toString());
}
Also used : XClass(org.infinispan.protostream.annotations.impl.types.XClass) ReflectionTypeFactory(org.infinispan.protostream.annotations.impl.types.ReflectionTypeFactory) Test(org.junit.Test)

Example 23 with XClass

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

the class AutoProtoSchemaBuilderAnnotationProcessor method processClass.

private void processClass(RoundEnvironment roundEnv, SerializationContext serCtx, TypeElement typeElement, AutoProtoSchemaBuilder builderAnnotation, AnnotatedClassScanner classScanner, ProcessorContext processorContext) throws IOException {
    if (typeElement.getNestingKind() == NestingKind.LOCAL || typeElement.getNestingKind() == NestingKind.ANONYMOUS) {
        throw new AnnotationProcessingException(typeElement, "Classes or interfaces annotated with @AutoProtoSchemaBuilder must not be local or anonymous.");
    }
    if (typeElement.getNestingKind() == NestingKind.MEMBER && !typeElement.getModifiers().contains(Modifier.STATIC)) {
        throw new AnnotationProcessingException(typeElement, "Nested classes or interfaces annotated with @AutoProtoSchemaBuilder must be static.");
    }
    if (typeElement.getModifiers().contains(Modifier.FINAL)) {
        throw new AnnotationProcessingException(typeElement, "Classes annotated with @AutoProtoSchemaBuilder must not be final.");
    }
    if (!builderAnnotation.className().isEmpty() && (!SourceVersion.isIdentifier(builderAnnotation.className()) || SourceVersion.isKeyword(builderAnnotation.className()))) {
        throw new AnnotationProcessingException(typeElement, "@AutoProtoSchemaBuilder.className annotation attribute must be a valid Java identifier and must not be fully qualified.");
    }
    TypeMirror serializationContextInitializerTypeMirror = elements.getTypeElement(SerializationContextInitializer.class.getName()).asType();
    if (!types.isSubtype(typeElement.asType(), serializationContextInitializerTypeMirror)) {
        throw new AnnotationProcessingException(typeElement, "Classes or interfaces annotated with @AutoProtoSchemaBuilder must implement/extend %s", SerializationContextInitializer.class.getName());
    }
    PackageElement packageElement = elements.getPackageOf(typeElement);
    String initializerPackageName = packageElement.isUnnamed() ? null : packageElement.getQualifiedName().toString();
    String initializerClassName = classScanner.getInitializerClassName();
    String initializerFQN = classScanner.getInitializerFQClassName();
    String protobufPackageName = builderAnnotation.schemaPackageName().isEmpty() ? null : builderAnnotation.schemaPackageName();
    String protobufFileName = getProtobufFileName(typeElement, builderAnnotation);
    ProcessorContext dependencies = processDependencies(roundEnv, serCtx, typeElement, builderAnnotation);
    warnOverrideExistingMethods(typeElement);
    Set<XClass> xclasses = classScanner.getXClasses();
    CompileTimeProtoSchemaGenerator protoSchemaGenerator = new CompileTimeProtoSchemaGenerator(typeFactory, generatedFilesWriter, serCtx, typeElement.getQualifiedName().toString(), protobufFileName, protobufPackageName, dependencies.marshalledClasses, xclasses, builderAnnotation.autoImportClasses(), classScanner);
    String schemaSrc = protoSchemaGenerator.generateAndRegister();
    writeSerializationContextInitializer(typeElement, typeElement.getQualifiedName().toString(), builderAnnotation, dependencies.initializerClassNames, classScanner.getClasses(), protoSchemaGenerator.getGeneratedMarshallerClasses(), initializerPackageName, initializerClassName, initializerFQN, protobufFileName, protobufPackageName, schemaSrc);
    processorContext.add(classScanner.getInitializerFQClassName(), protobufFileName, protoSchemaGenerator.getMarshalledClasses());
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) PackageElement(javax.lang.model.element.PackageElement) SerializationContextInitializer(org.infinispan.protostream.SerializationContextInitializer) XClass(org.infinispan.protostream.annotations.impl.types.XClass)

Example 24 with XClass

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

the class AutoProtoSchemaBuilderAnnotationProcessor method warnOverrideExistingMethods.

private void warnOverrideExistingMethods(TypeElement typeElement) {
    XClass annotatedType = typeFactory.fromTypeMirror(typeElement.asType());
    warnOverrideExistingMethod(annotatedType, "getProtoFileName");
    warnOverrideExistingMethod(annotatedType, "getProtoFile");
    XClass serializationContextClass = typeFactory.fromClass(SerializationContext.class);
    warnOverrideExistingMethod(annotatedType, "registerSchema", serializationContextClass);
    warnOverrideExistingMethod(annotatedType, "registerMarshallers", serializationContextClass);
}
Also used : XClass(org.infinispan.protostream.annotations.impl.types.XClass)

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