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;
}
}
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());
}
}
}
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();
}
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;
}
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"));
}
Aggregations