From 5efa41663619ca89e076dd7c4dcccdf7fa2444ca Mon Sep 17 00:00:00 2001 From: Adrian Nistor Date: Mon, 17 May 2021 15:57:49 +0300 Subject: [PATCH] IPROTO-213 Protostream processor must ensure it has the same version as protostream core * use manifest implementation version for comparison --- .../org/infinispan/protostream/Version.java | 68 +++++++++---------- ...ProtoSchemaBuilderAnnotationProcessor.java | 29 +++++--- 2 files changed, 53 insertions(+), 44 deletions(-) diff --git a/core/src/main/java/org/infinispan/protostream/Version.java b/core/src/main/java/org/infinispan/protostream/Version.java index adbb8528f..c075490a9 100644 --- a/core/src/main/java/org/infinispan/protostream/Version.java +++ b/core/src/main/java/org/infinispan/protostream/Version.java @@ -1,49 +1,38 @@ package org.infinispan.protostream; -import java.io.IOException; -import java.io.InputStream; import java.util.Objects; -import java.util.Properties; /** - * Contains version information about this ProtoStream release. + * Provides version information about this ProtoStream release. * * @author anistor@redhat.com * @since 4.2 */ public final class Version implements Comparable { - private static final Version VERSION = getArtifactVersion(); + private static final Version VERSION = getVersion(Version.class); /** - * We try to obtain the Maven pom.properties of the artifact and get the build version from there. + * Try to obtain the version from the manifest. */ - private static Version getArtifactVersion() { - int major = 0; - int minor = 0; - int micro = 0; - String suffix = null; - - InputStream res = Version.class.getResourceAsStream("/META-INF/maven/org.infinispan.protostream/protostream/pom.properties"); - if (res != null) { - try { - Properties pomProps = new Properties(); - pomProps.load(res); - String version = pomProps.getProperty("version", "0.0.0-UNKNOWN"); - String[] versionParts = version.split("[.\\-]"); - major = Integer.parseInt(versionParts[0]); - if (versionParts.length > 1) { - minor = Integer.parseInt(versionParts[1]); - } - if (versionParts.length > 2) { - micro = Integer.parseInt(versionParts[2]); - } - if (versionParts.length > 3) { - suffix = versionParts[3]; - } - } catch (IOException | NumberFormatException | ArrayIndexOutOfBoundsException e) { - // ignored - } + public static Version getVersion(Class clazz) { + String version = clazz.getPackage().getImplementationVersion(); + if (version == null) { + return null; + } + + int major; + int minor; + int micro; + String suffix; + try { + String[] versionParts = version.split("[.\\-]"); + major = Integer.parseInt(versionParts[0]); + minor = versionParts.length > 1 ? Integer.parseInt(versionParts[1]) : 0; + micro = versionParts.length > 2 ? Integer.parseInt(versionParts[2]) : 0; + suffix = versionParts.length > 3 ? versionParts[3] : null; + } catch (Exception e) { + return null; } return new Version(major, minor, micro, suffix); @@ -72,7 +61,14 @@ public Version(int major, int minor, int micro, String suffix) { this.minor = minor; this.micro = micro; this.suffix = suffix; - versionString = major + "." + minor + "." + micro + (suffix != null ? "." + suffix : ""); + if (suffix == null) { + suffix = ""; + } else if ("SNAPSHOT".equals(suffix)) { + suffix = "-SNAPSHOT"; + } else { + suffix = '.' + suffix; + } + versionString = major + "." + minor + "." + micro + suffix; } public int getMajor() { @@ -124,7 +120,11 @@ public int compareTo(Version other) { if (d == 0) { d = micro - other.micro; if (d == 0) { - d = suffix.compareTo(other.suffix); + if (suffix == null) { + d = other.suffix == null ? 0 : -1; + } else { + d = other.suffix == null ? 1 : suffix.compareTo(other.suffix); + } } } } diff --git a/processor/src/main/java/org/infinispan/protostream/annotations/impl/processor/AutoProtoSchemaBuilderAnnotationProcessor.java b/processor/src/main/java/org/infinispan/protostream/annotations/impl/processor/AutoProtoSchemaBuilderAnnotationProcessor.java index 70488b076..92fe6bd78 100644 --- a/processor/src/main/java/org/infinispan/protostream/annotations/impl/processor/AutoProtoSchemaBuilderAnnotationProcessor.java +++ b/processor/src/main/java/org/infinispan/protostream/annotations/impl/processor/AutoProtoSchemaBuilderAnnotationProcessor.java @@ -41,6 +41,7 @@ import org.infinispan.protostream.ProtobufUtil; import org.infinispan.protostream.SerializationContext; import org.infinispan.protostream.SerializationContextInitializer; +import org.infinispan.protostream.Version; import org.infinispan.protostream.WrappedMessage; import org.infinispan.protostream.annotations.AutoProtoSchemaBuilder; import org.infinispan.protostream.annotations.ProtoSchemaBuilderException; @@ -67,7 +68,7 @@ public final class AutoProtoSchemaBuilderAnnotationProcessor extends AbstractPro */ static final String ANNOTATION_NAME = "org.infinispan.protostream.annotations.AutoProtoSchemaBuilder"; - private static boolean checkForMinRequiredJava = true; + private static boolean checkRequiredEnv = true; private final ServiceLoaderFileGenerator serviceLoaderFileGenerator = new ServiceLoaderFileGenerator(SerializationContextInitializer.class); @@ -102,6 +103,8 @@ public synchronized void init(ProcessingEnvironment processingEnv) { messager = processingEnv.getMessager(); generatedFilesWriter = new GeneratedFilesWriter(filer); + + ensureRequiredEnv(); } @Override @@ -172,8 +175,6 @@ public boolean process(Set annotations, RoundEnvironment logDebug("AutoProtoSchemaBuilderAnnotationProcessor annotations=%s, rootElements=%s", annotations, roundEnv.getRootElements()); } - ensureMinRequiredJava(); - Optional claimedAnnotation = annotations.stream() .filter(a -> a.getQualifiedName().contentEquals(ANNOTATION_NAME)) .findAny(); @@ -208,15 +209,23 @@ public boolean process(Set annotations, RoundEnvironment return claimedAnnotation.isPresent(); } - private void ensureMinRequiredJava() { - if (checkForMinRequiredJava && getJavaMajorVersion() < 9) { + private void ensureRequiredEnv() { + if (checkRequiredEnv) { + // check requirements and complain only once + checkRequiredEnv = false; - // check and complain only once - checkForMinRequiredJava = false; + if (getJavaMajorVersion() < 9) { + reportWarning(null, "Please ensure you use at least Java ver. 9 for compilation in order to avoid various " + + "compiler related bugs from older Java versions that impact the AutoProtoSchemaBuilder annotation " + + "processor (you can still set the output target to 8 or above)."); + } - reportWarning(null, "Please ensure you use at least Java ver. 9 for compilation in order to avoid various " + - "compiler related bugs from older Java versions that impact the AutoProtoSchemaBuilder annotation " + - "processor (you can still set the output target to 8 or above)."); + Version procVersion = Version.getVersion(AutoProtoSchemaBuilder.class); + if (Version.getVersion().compareTo(procVersion) != 0) { + // protostream core and processor versions must be identical to ensure compatibility + reportWarning(null, "Version mismatch! protostream (%s) and protostream-processor (%s) are expected to be the same version. " + + "Mixing different versions is unsupported and can lead to unintended consequences.", Version.getVersion(), procVersion); + } } }