use of org.infinispan.protostream.annotations.impl.IndentWriter in project protostream by infinispan.
the class MarshallerSourceCodeGenerator method generateMessageMarshaller.
private void generateMessageMarshaller(ProtoMessageTypeMetadata pmtm) throws IOException {
String marshallerClassName = makeUniqueMarshallerClassName(pmtm);
if (log.isTraceEnabled()) {
log.tracef("Generating message marshaller %s for %s", marshallerClassName, pmtm.getJavaClass().getName());
}
IndentWriter iw = new IndentWriter();
addFileHeader(iw, pmtm.getAnnotatedClassName());
String fqn;
if (pmtm.getAnnotatedClass().getPackageName() != null) {
fqn = pmtm.getAnnotatedClass().getPackageName() + '.' + marshallerClassName;
iw.append("package ").append(pmtm.getAnnotatedClass().getPackageName()).append(";\n\n");
} else {
fqn = marshallerClassName;
}
if (pmtm.getJavaClass().getPackageName() != null) {
XClass toImport = pmtm.getJavaClass().isArray() ? pmtm.getJavaClass().getComponentType() : pmtm.getJavaClass();
String toImportName = toImport.getCanonicalName();
iw.append("import ").append(toImportName != null ? toImportName : toImport.getName()).append(";\n\n");
}
AutoProtoSchemaBuilderAnnotationProcessor.addGeneratedClassHeader(iw, true);
iw.append("@SuppressWarnings(\"all\")\n");
iw.append("public final class ").append(marshallerClassName).append(" extends ").append(GeneratedMarshallerBase.class.getName()).append(" implements ").append(ProtobufTagMarshaller.class.getName()).append('<').append(pmtm.getJavaClassName()).append('>');
String elementType = null;
if (pmtm.isIndexedContainer()) {
elementType = pmtm.getAnnotatedClass().getGenericInterfaceParameterTypes(IndexedElementContainerAdapter.class)[1];
iw.append(", ").append(IndexedElementContainerAdapter.class.getName()).append('<').append(pmtm.getJavaClassName()).append(", ").append(elementType).append(">");
} else if (pmtm.isIterableContainer()) {
elementType = pmtm.getAnnotatedClass().getGenericInterfaceParameterTypes(IterableElementContainerAdapter.class)[1];
iw.append(", ").append(IterableElementContainerAdapter.class.getName()).append('<').append(pmtm.getJavaClassName()).append(", ").append(elementType).append(">");
}
iw.append(" {\n\n");
iw.inc();
if (pmtm.isAdapter()) {
addAdapterField(iw, pmtm);
}
addMarshallerDelegateFields(iw, pmtm);
iw.append("@Override\npublic Class<").append(pmtm.getJavaClassName()).append("> getJavaClass() { return ").append(pmtm.getJavaClassName()).append(".class; }\n\n");
iw.append("@Override\npublic String getTypeName() { return \"").append(makeQualifiedTypeName(pmtm.getFullName())).append("\"; }\n\n");
if (pmtm.isIndexedContainer()) {
if (pmtm.isAdapter()) {
iw.append("@Override\npublic int getNumElements(").append(pmtm.getJavaClassName()).append(" container) { return ").append(ADAPTER_FIELD_NAME).append(".getNumElements(container); }\n");
iw.append("@Override\npublic ").append(elementType).append(" getElement(").append(pmtm.getJavaClassName()).append(" container, int index) { return ").append(ADAPTER_FIELD_NAME).append(".getElement(container, index); }\n");
iw.append("@Override\npublic void setElement(").append(pmtm.getJavaClassName()).append(" container, int index, ").append(elementType).append(" element) { ").append(ADAPTER_FIELD_NAME).append(".setElement(container, index, element); }\n");
} else {
iw.append("@Override\npublic int getNumElements(").append(pmtm.getJavaClassName()).append(" container) { return ((").append(IndexedElementContainerAdapter.class.getName()).append(") container).getNumElements(); }\n");
iw.append("@Override\npublic ").append(elementType).append(" getElement(").append(pmtm.getJavaClassName()).append(" container, int index) { return ((").append(IndexedElementContainerAdapter.class.getName()).append(") container).getElement(index); }\n");
iw.append("@Override\npublic void setElement(").append(pmtm.getJavaClassName()).append(" container, int index, ").append(elementType).append(" element) { ((").append(IndexedElementContainerAdapter.class.getName()).append(") container).setElement(index, element); }\n");
}
} else if (pmtm.isIterableContainer()) {
if (pmtm.isAdapter()) {
iw.append("@Override\npublic int getNumElements(").append(pmtm.getJavaClassName()).append(" container) { return ").append(ADAPTER_FIELD_NAME).append(".getNumElements(container); }\n");
iw.append("@Override\npublic java.util.Iterator getElements(").append(pmtm.getJavaClassName()).append(" container) { return ").append(ADAPTER_FIELD_NAME).append(".getElements(container); }\n");
iw.append("@Override\npublic void appendElement(").append(pmtm.getJavaClassName()).append(" container, ").append(elementType).append(" element) { ").append(ADAPTER_FIELD_NAME).append(".appendElement(container, element); }\n");
} else {
iw.append("@Override\npublic int getNumElements(").append(pmtm.getJavaClassName()).append(" container) { return ((").append(IterableElementContainerAdapter.class.getName()).append(") container).getNumElements(); }\n");
iw.append("@Override\npublic java.util.Iterator getElements(").append(pmtm.getJavaClassName()).append(" container) { return ((").append(IterableElementContainerAdapter.class.getName()).append(") container).getElements(); }\n");
iw.append("@Override\npublic void appendElement(").append(pmtm.getJavaClassName()).append(" container, ").append(elementType).append(" element) { ((").append(IterableElementContainerAdapter.class.getName()).append(") container).appendElement(element); }\n");
}
}
String readMethodSrc = generateReadMethodBody(pmtm);
String readMethodSig = "public " + pmtm.getJavaClassName() + " read(" + ProtobufTagMarshaller.ReadContext.class.getCanonicalName() + " $1) throws java.io.IOException";
if (log.isTraceEnabled()) {
log.tracef("%s %s", readMethodSig, readMethodSrc);
}
iw.append("@Override\n").append(readMethodSig).append(' ').append(readMethodSrc).append('\n');
String writeMethodSrc = generateWriteMethodBody(pmtm);
String writeMethodSig = "public void write(" + ProtobufTagMarshaller.WriteContext.class.getCanonicalName() + " $1, " + pmtm.getJavaClassName() + " $2) throws java.io.IOException";
if (log.isTraceEnabled()) {
log.tracef("%s %s", writeMethodSig, writeMethodSrc);
}
iw.append("@Override\n").append(writeMethodSig).append(' ').append(writeMethodSrc);
iw.dec();
iw.append("}\n");
emitSource(fqn, iw.toString(), pmtm);
}
use of org.infinispan.protostream.annotations.impl.IndentWriter in project protostream by infinispan.
the class MarshallerSourceCodeGenerator method generateEnumMarshaller.
private void generateEnumMarshaller(ProtoEnumTypeMetadata petm) throws IOException {
String marshallerClassName = makeUniqueMarshallerClassName(petm);
if (log.isTraceEnabled()) {
log.tracef("Generating enum marshaller %s for %s", marshallerClassName, petm.getJavaClass().getName());
}
IndentWriter iw = new IndentWriter();
addFileHeader(iw, petm.getAnnotatedClassName());
String fqn;
if (petm.getAnnotatedClass().getPackageName() != null) {
fqn = petm.getAnnotatedClass().getPackageName() + '.' + marshallerClassName;
iw.append("package ").append(petm.getAnnotatedClass().getPackageName()).append(";\n\n");
} else {
fqn = marshallerClassName;
}
if (petm.getJavaClass().getPackageName() != null) {
iw.append("import ").append(petm.getJavaClassName()).append(";\n\n");
}
iw.append("public final class ").append(marshallerClassName).append(" implements ").append(EnumMarshaller.class.getName()).append('<').append(petm.getJavaClassName()).append("> {\n\n");
iw.inc();
iw.append("@Override\npublic Class<").append(petm.getJavaClassName()).append("> getJavaClass() { return ").append(petm.getJavaClassName()).append(".class; }\n\n");
iw.append("@Override\npublic String getTypeName() { return \"").append(makeQualifiedTypeName(petm.getFullName())).append("\"; }\n\n");
String decodeSrc = generateEnumDecodeMethodBody(petm);
String decodeSig = "public " + petm.getJavaClassName() + " decode(int $1)";
if (log.isTraceEnabled()) {
log.tracef("%s %s", decodeSig, decodeSrc);
}
iw.append("@Override\n").append(decodeSig).append(' ').append(decodeSrc).append('\n');
String encodeSrc = generateEnumEncodeMethodBody(petm);
String encodeSig = "public int encode(" + petm.getJavaClassName() + " $1) throws IllegalArgumentException";
if (log.isTraceEnabled()) {
log.tracef("%s %s", encodeSig, encodeSrc);
}
iw.append("@Override\n").append(encodeSig).append(' ').append(encodeSrc);
iw.dec();
iw.append("}\n");
emitSource(fqn, iw.toString(), petm);
}
use of org.infinispan.protostream.annotations.impl.IndentWriter in project protostream by infinispan.
the class AutoProtoSchemaBuilderAnnotationProcessor method generateSerializationContextInitializer.
private String generateSerializationContextInitializer(Element annotatedElement, String annotatedElementFQN, AutoProtoSchemaBuilder annotation, Set<String> serCtxInitDeps, Collection<? extends TypeMirror> classes, Set<String> generatedMarshallerClasses, String packageName, String initializerClassName, String fileName, String protobufPackageName, String schemaSrc, String schemaResource) {
IndentWriter iw = new IndentWriter();
iw.append("/*\n");
iw.append(" Generated by ").append(getClass().getName()).append("\n");
iw.append(annotatedElement.getKind() == ElementKind.PACKAGE ? " for package " : " for class ").append(annotatedElementFQN).append("\n");
iw.append(" annotated with ").append(String.valueOf(annotation)).append("\n");
iw.append(" */\n\n");
if (packageName != null) {
iw.append("package ").append(packageName).append(";\n\n");
}
addGeneratedClassHeader(iw, classes);
addSchemaBuilderAnnotation(iw, initializerClassName, fileName, annotation.schemaFilePath(), protobufPackageName, classes, serCtxInitDeps, annotation.autoImportClasses(), schemaSrc == null, annotation.service());
iw.append("@SuppressWarnings(\"all\")\n");
iw.append("public class ").append(initializerClassName);
if (annotatedElement.getKind() == ElementKind.PACKAGE) {
iw.append(" implements ").append(schemaSrc == null ? SerializationContextInitializer.class.getName() : GeneratedSchema.class.getName());
} else {
iw.append(annotatedElement.getKind() == ElementKind.INTERFACE ? " implements " : " extends ").append(annotatedElementFQN);
if (schemaSrc != null) {
TypeMirror generatedSchemaType = elements.getTypeElement(GeneratedSchema.class.getName()).asType();
if (!types.isSubtype(annotatedElement.asType(), generatedSchemaType)) {
iw.append(annotatedElement.getKind() == ElementKind.INTERFACE ? ", " : " implements ").append(GeneratedSchema.class.getName());
}
}
}
iw.append(" {\n\n");
iw.inc();
if (schemaSrc != null && schemaResource == null) {
iw.append("private static final String PROTO_SCHEMA = ").append(makeStringLiteral(schemaSrc)).append(";\n\n");
}
int k = 0;
for (String s : serCtxInitDeps) {
iw.append("private final ").append(s).append(" dep").append(String.valueOf(k++)).append(" = new ").append(s).append("();\n\n");
}
if (schemaSrc != null) {
iw.append("@Override\npublic String getProtoFileName() { return \"").append(fileName).append("\"; }\n\n");
iw.append("@Override\npublic String getProtoFile() { return ");
if (schemaResource == null) {
iw.append("PROTO_SCHEMA");
} else {
iw.append("org.infinispan.protostream.impl.ResourceUtils.getResourceAsString(getClass(), \"").append(schemaResource).append("\")");
}
iw.append("; }\n\n");
if (schemaResource != null) {
iw.append("@Override\npublic java.io.Reader getProtoFileReader() { return ");
iw.append("org.infinispan.protostream.impl.ResourceUtils.getResourceAsReader(getClass(), \"").append(schemaResource).append("\")");
iw.append("; }\n\n");
}
} else {
// TODO [anistor] See IPROTO-154. This 'else' branch must removed in ver. 5, once methods getProtoFileName()/getProtoFile() are removed from SerializationContextInitializer
iw.append("@Override\npublic String getProtoFileName() { /* DUMMY! */ return \"").append(fileName).append("\"; }\n\n");
iw.append("@Override\npublic String getProtoFile() { /* DUMMY! */ return \"// ").append(fileName).append("\"; }\n\n");
}
iw.append("@Override\n");
iw.append("public void registerSchema(org.infinispan.protostream.SerializationContext serCtx) {\n");
iw.inc();
for (int j = 0; j < serCtxInitDeps.size(); j++) {
iw.append("dep").append(String.valueOf(j)).append(".registerSchema(serCtx);\n");
}
if (schemaSrc != null) {
iw.append("serCtx.registerProtoFiles(org.infinispan.protostream.FileDescriptorSource.fromString(getProtoFileName(), getProtoFile()));\n");
}
iw.dec();
iw.append("}\n\n");
iw.append("@Override\n");
iw.append("public void registerMarshallers(org.infinispan.protostream.SerializationContext serCtx) {\n");
iw.inc();
for (int j = 0; j < serCtxInitDeps.size(); j++) {
iw.append("dep").append(String.valueOf(j)).append(".registerMarshallers(serCtx);\n");
}
for (String name : generatedMarshallerClasses) {
iw.append("serCtx.registerMarshaller(new ").append(name).append("());\n");
}
iw.dec();
iw.append("}\n");
iw.dec();
iw.append("}\n");
return iw.toString();
}
Aggregations