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