Code format, tabs to spaces, etc.

This commit is contained in:
Erik C. Thauvin 2016-07-18 17:45:07 -07:00
parent 3973b144bc
commit 240c928e77
16 changed files with 826 additions and 667 deletions

View file

@ -34,85 +34,83 @@ package net.thauvin.erik.semver;
/**
* The <code>Constants</code> class holds the constant variables used throughout this project.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2016-01-13
* @since 1.0
*/
public final class Constants
{
/**
* The default class name.
*/
public static final String DEFAULT_CLASS_NAME = "GeneratedVersion";
public final class Constants {
/**
* The default class name.
*/
public static final String DEFAULT_CLASS_NAME = "GeneratedVersion";
/**
* The default major version.
*/
public static final int DEFAULT_MAJOR = 1;
/**
* The default major version.
*/
public static final int DEFAULT_MAJOR = 1;
/**
* The default minor version.
*/
public static final int DEFAULT_MINOR = 0;
/**
* The default minor version.
*/
public static final int DEFAULT_MINOR = 0;
/**
* The default patch version.
*/
public static final int DEFAULT_PATCH = 0;
/**
* The default patch version.
*/
public static final int DEFAULT_PATCH = 0;
/**
* The default Velocity template.
*/
public static final String DEFAULT_TEMPLATE = "version.vm";
/**
* The default Velocity template.
*/
public static final String DEFAULT_TEMPLATE = "version.vm";
/**
* The empty string.
*/
public static final String EMPTY = "";
/**
* The empty string.
*/
public static final String EMPTY = "";
/**
* The build metadata property key.
*/
public static final String KEY_VERSION_BUILDMETA = "version.buildmeta";
/**
* The build metadata property key.
*/
public static final String KEY_VERSION_BUILDMETA = "version.buildmeta";
/**
* The major version property key.
*/
public static final String KEY_VERSION_MAJOR = "version.major";
/**
* The major version property key.
*/
public static final String KEY_VERSION_MAJOR = "version.major";
/**
* The minor version property key.
*/
public static final String KEY_VERSION_MINOR = "version.minor";
/**
* The minor version property key.
*/
public static final String KEY_VERSION_MINOR = "version.minor";
/**
* The patch version property key.
*/
public static final String KEY_VERSION_PATCH = "version.patch";
/**
* The patch version property key.
*/
public static final String KEY_VERSION_PATCH = "version.patch";
/**
* The pre-release version property key.
*/
public static final String KEY_VERSION_PRERELEASE = "version.prerelease";
/**
* The pre-release version property key.
*/
public static final String KEY_VERSION_PRERELEASE = "version.prerelease";
/**
* The project property key.
*/
public static final String KEY_VERSION_PROJECT = "version.project";
/**
* The project property key.
*/
public static final String KEY_VERSION_PROJECT = "version.project";
/**
* The velocity properties name.
*/
public static final String VELOCITY_PROPERTIES = "velocity.properties";
/**
* The velocity properties name.
*/
public static final String VELOCITY_PROPERTIES = "velocity.properties";
/**
* Disables the default constructor.
*
* @throws UnsupportedOperationException if the constructor is called.
*/
private Constants()
throws UnsupportedOperationException
{
throw new UnsupportedOperationException("Illegal constructor call.");
}
/**
* Disables the default constructor.
*
* @throws UnsupportedOperationException if the constructor is called.
*/
private Constants()
throws UnsupportedOperationException {
throw new UnsupportedOperationException("Illegal constructor call.");
}
}

View file

@ -39,41 +39,40 @@ import java.lang.annotation.Target;
/**
* The <code>Version</code> class implements the annotation interface.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2016-01-13
* @since 1.0
*/
@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
public @interface Version
{
String buildmeta() default Constants.EMPTY;
public @interface Version {
String buildmeta() default Constants.EMPTY;
String buildmetaKey() default Constants.KEY_VERSION_BUILDMETA;
String buildmetaKey() default Constants.KEY_VERSION_BUILDMETA;
String className() default Constants.DEFAULT_CLASS_NAME;
String className() default Constants.DEFAULT_CLASS_NAME;
int major() default Constants.DEFAULT_MAJOR;
int major() default Constants.DEFAULT_MAJOR;
String majorKey() default Constants.KEY_VERSION_MAJOR;
String majorKey() default Constants.KEY_VERSION_MAJOR;
int minor() default Constants.DEFAULT_MINOR;
int minor() default Constants.DEFAULT_MINOR;
String minorKey() default Constants.KEY_VERSION_MINOR;
String minorKey() default Constants.KEY_VERSION_MINOR;
int patch() default Constants.DEFAULT_PATCH;
int patch() default Constants.DEFAULT_PATCH;
String patchKey() default Constants.KEY_VERSION_PATCH;
String patchKey() default Constants.KEY_VERSION_PATCH;
String prerelease() default Constants.EMPTY;
String prerelease() default Constants.EMPTY;
String prereleaseKey() default Constants.KEY_VERSION_PRERELEASE;
String prereleaseKey() default Constants.KEY_VERSION_PRERELEASE;
String project() default Constants.EMPTY;
String project() default Constants.EMPTY;
String projectKey() default Constants.KEY_VERSION_PROJECT;
String projectKey() default Constants.KEY_VERSION_PROJECT;
String properties() default Constants.EMPTY;
String properties() default Constants.EMPTY;
String template() default Constants.DEFAULT_TEMPLATE;
String template() default Constants.DEFAULT_TEMPLATE;
}

View file

@ -34,203 +34,186 @@ package net.thauvin.erik.semver;
/**
* The <code>VersionInfo</code> class is used to hold and retrieve the semantic version values.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2016-01-16
* @since 1.0
*/
public class VersionInfo
{
private final long epoch = System.currentTimeMillis();
public class VersionInfo {
private final long epoch = System.currentTimeMillis();
private String buildmeta;
private String buildmeta;
private int major;
private int major;
private int minor;
private int minor;
private int patch;
private int patch;
private String prerelease;
private String prerelease;
private String project;
private String project;
/**
* Creates a new object with default values
*/
public VersionInfo()
{
major = Constants.DEFAULT_MAJOR;
minor = Constants.DEFAULT_MINOR;
patch = Constants.DEFAULT_PATCH;
buildmeta = Constants.EMPTY;
prerelease = Constants.EMPTY;
project = Constants.EMPTY;
}
/**
* Creates a new object with default values
*/
public VersionInfo() {
major = Constants.DEFAULT_MAJOR;
minor = Constants.DEFAULT_MINOR;
patch = Constants.DEFAULT_PATCH;
buildmeta = Constants.EMPTY;
prerelease = Constants.EMPTY;
project = Constants.EMPTY;
}
/**
* Creates a new object with values from a {@link net.thauvin.erik.semver.Version Version} object.
*
* @param version The version object.
*/
public VersionInfo(final Version version)
{
major = version.major();
minor = version.minor();
patch = version.patch();
buildmeta = version.buildmeta();
prerelease = version.prerelease();
project = version.project();
}
/**
* Creates a new object with values from a {@link net.thauvin.erik.semver.Version Version} object.
*
* @param version The version object.
*/
public VersionInfo(final Version version) {
major = version.major();
minor = version.minor();
patch = version.patch();
buildmeta = version.buildmeta();
prerelease = version.prerelease();
project = version.project();
}
/**
* Returns the build metadata.
*
* @return The build metadata.
*/
public String getBuildMetadata()
{
return buildmeta;
}
/**
* Returns the build metadata.
*
* @return The build metadata.
*/
public String getBuildMetadata() {
return buildmeta;
}
/**
* Sets the build metadata.
*
* @param buildmeta The new build metadata.
*/
public void setBuildMetadata(final String buildmeta)
{
this.buildmeta = buildmeta;
}
/**
* Sets the build metadata.
*
* @param buildmeta The new build metadata.
*/
public void setBuildMetadata(final String buildmeta) {
this.buildmeta = buildmeta;
}
/**
* Returns the build epoch/Unix timestamp.
*
* @return The build epoch.
*/
public long getEpoch()
{
return epoch;
}
/**
* Returns the build epoch/Unix timestamp.
*
* @return The build epoch.
*/
public long getEpoch() {
return epoch;
}
/**
* Returns the major version.
*
* @return The major version.
*/
public int getMajor()
{
return major;
}
/**
* Returns the major version.
*
* @return The major version.
*/
public int getMajor() {
return major;
}
/**
* Sets the major version.
*
* @param major The new major version.
*/
public void setMajor(final int major)
{
this.major = major;
}
/**
* Sets the major version.
*
* @param major The new major version.
*/
public void setMajor(final int major) {
this.major = major;
}
/**
* Returns the major version.
*
* @return The major version.
*/
public int getMinor()
{
return minor;
}
/**
* Returns the major version.
*
* @return The major version.
*/
public int getMinor() {
return minor;
}
/**
* Sets the minor version.
*
* @param minor The new minor version.
*/
public void setMinor(final int minor)
{
this.minor = minor;
}
/**
* Sets the minor version.
*
* @param minor The new minor version.
*/
public void setMinor(final int minor) {
this.minor = minor;
}
/**
* Returns the patch version.
*
* @return The patch version.
*/
public int getPatch()
{
return patch;
}
/**
* Returns the patch version.
*
* @return The patch version.
*/
public int getPatch() {
return patch;
}
/**
* Sets the patch version.
*
* @param patch The new patch version.
*/
public void setPatch(final int patch)
{
this.patch = patch;
}
/**
* Sets the patch version.
*
* @param patch The new patch version.
*/
public void setPatch(final int patch) {
this.patch = patch;
}
/**
* Returns the pre-release version.
*
* @return The pre-release version.
*/
public String getPreRelease()
{
return prerelease;
}
/**
* Returns the pre-release version.
*
* @return The pre-release version.
*/
public String getPreRelease() {
return prerelease;
}
/**
* Sets the pre-release version.
*
* @param prerelease The new pre-release version.
*/
public void setPreRelease(final String prerelease)
{
this.prerelease = prerelease;
}
/**
* Sets the pre-release version.
*
* @param prerelease The new pre-release version.
*/
public void setPreRelease(final String prerelease) {
this.prerelease = prerelease;
}
/**
* Returns the project name.
*
* @return The project name.
*/
public String getProject()
{
return project;
}
/**
* Returns the project name.
*
* @return The project name.
*/
public String getProject() {
return project;
}
/**
* Sets the project name.
*
* @param project The new project name.
*/
public void setProject(final String project)
{
this.project = project;
}
/**
* Sets the project name.
*
* @param project The new project name.
*/
public void setProject(final String project) {
this.project = project;
}
/**
* Returns the full version string.
* <p>
* Formatted as:
* <blockquote><code>MAJOR.MINOR.PATCH[-PRERELEASE][+BUILDMETADATA]</code></blockquote>
* <p>
* For example:
* <ul>
* <li><code>1.0.0</code></li>
* <li><code>1.0.0-beta</code></li>
* <li><code>1.0.0+20160124144700</code></li>
* <li><code>1.0.0-alpha+001</code></li>
* </ul>
*
* @return The version string.
*/
public String getVersion()
{
return Integer.toString(major) + '.' + Integer.toString(minor) + '.' + Integer.toString(patch) + (
prerelease.length() > 0 ? '-' + prerelease : "") + (buildmeta.length() > 0 ? '+' + buildmeta : "");
}
/**
* Returns the full version string.
* <p>
* Formatted as:
* <blockquote><code>MAJOR.MINOR.PATCH[-PRERELEASE][+BUILDMETADATA]</code></blockquote>
* <p>
* For example:
* <ul>
* <li><code>1.0.0</code></li>
* <li><code>1.0.0-beta</code></li>
* <li><code>1.0.0+20160124144700</code></li>
* <li><code>1.0.0-alpha+001</code></li>
* </ul>
*
* @return The version string.
*/
public String getVersion() {
return Integer.toString(major) + '.' + Integer.toString(minor) + '.' + Integer.toString(patch) + (
prerelease.length() > 0 ? '-' + prerelease : "") + (buildmeta.length() > 0 ? '+' + buildmeta : "");
}
}

View file

@ -52,200 +52,169 @@ import java.util.Set;
/**
* The <code>VersionProcessor</code> class implements a semantic version annotation processor.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @author <a href="mailto:erik@thauvin.net" target="_blank">Erik C. Thauvin</a>
* @created 2016-01-13
* @since 1.0
*/
public class VersionProcessor extends AbstractProcessor
{
private Filer filer;
public class VersionProcessor extends AbstractProcessor {
private Filer filer;
private Messager messager;
private Messager messager;
private void error(final String s)
{
log(Diagnostic.Kind.ERROR, s);
}
private void error(final String s) {
log(Diagnostic.Kind.ERROR, s);
}
private void error(final String s, final Throwable t)
{
messager.printMessage(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s));
}
private void error(final String s, final Throwable t) {
messager.printMessage(Diagnostic.Kind.ERROR, (t != null ? t.toString() : s));
}
private VersionInfo findValues(final Version version)
throws IOException
{
final VersionInfo versionInfo;
private VersionInfo findValues(final Version version)
throws IOException {
final VersionInfo versionInfo;
if (version.properties().length() > 0)
{
versionInfo = new VersionInfo();
if (version.properties().length() > 0) {
versionInfo = new VersionInfo();
final File propsFile = new File(version.properties());
if (propsFile.exists())
{
note("Found properties: " + propsFile);
final Properties p = new Properties();
final File propsFile = new File(version.properties());
if (propsFile.exists()) {
note("Found properties: " + propsFile);
final Properties p = new Properties();
try (FileReader reader = new FileReader(propsFile))
{
p.load(reader);
try (FileReader reader = new FileReader(propsFile)) {
p.load(reader);
versionInfo.setProject(p.getProperty(version.projectKey(), Constants.EMPTY));
versionInfo.setMajor(parseIntProperty(p, version.majorKey(), Constants.DEFAULT_MAJOR));
versionInfo.setMinor(parseIntProperty(p, version.minorKey(), Constants.DEFAULT_MINOR));
versionInfo.setPatch(parseIntProperty(p, version.patchKey(), Constants.DEFAULT_PATCH));
versionInfo.setBuildMetadata(p.getProperty(version.buildmetaKey(), Constants.EMPTY));
versionInfo.setPreRelease(p.getProperty(version.prereleaseKey(), Constants.EMPTY));
}
}
else
{
error("Could not find: " + propsFile);
throw new FileNotFoundException(propsFile + " (The system cannot find the file specified)");
}
}
else
{
versionInfo = new VersionInfo(version);
}
versionInfo.setProject(p.getProperty(version.projectKey(), Constants.EMPTY));
versionInfo.setMajor(parseIntProperty(p, version.majorKey(), Constants.DEFAULT_MAJOR));
versionInfo.setMinor(parseIntProperty(p, version.minorKey(), Constants.DEFAULT_MINOR));
versionInfo.setPatch(parseIntProperty(p, version.patchKey(), Constants.DEFAULT_PATCH));
versionInfo.setBuildMetadata(p.getProperty(version.buildmetaKey(), Constants.EMPTY));
versionInfo.setPreRelease(p.getProperty(version.prereleaseKey(), Constants.EMPTY));
}
} else {
error("Could not find: " + propsFile);
throw new FileNotFoundException(propsFile + " (The system cannot find the file specified)");
}
} else {
versionInfo = new VersionInfo(version);
}
return versionInfo;
}
return versionInfo;
}
/**
* {@inheritDoc}
*/
@Override
public Set<String> getSupportedAnnotationTypes()
{
final Set<String> result = new HashSet<>();
result.add(Version.class.getCanonicalName());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public Set<String> getSupportedAnnotationTypes() {
final Set<String> result = new HashSet<>();
result.add(Version.class.getCanonicalName());
return result;
}
/**
* {@inheritDoc}
*/
@Override
public SourceVersion getSupportedSourceVersion()
{
return SourceVersion.RELEASE_8;
}
/**
* {@inheritDoc}
*/
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.RELEASE_8;
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void init(final ProcessingEnvironment processingEnv)
{
super.init(processingEnv);
filer = processingEnv.getFiler();
messager = processingEnv.getMessager();
}
/**
* {@inheritDoc}
*/
@Override
public synchronized void init(final ProcessingEnvironment processingEnv) {
super.init(processingEnv);
filer = processingEnv.getFiler();
messager = processingEnv.getMessager();
}
/**
* {@inheritDoc}
*/
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv)
{
for (final Element element : roundEnv.getElementsAnnotatedWith(Version.class))
{
final Version version = element.getAnnotation(Version.class);
if (element.getKind() == ElementKind.CLASS)
{
final Element enclosingElement = element.getEnclosingElement();
if (enclosingElement.getKind() == ElementKind.PACKAGE)
{
final PackageElement packageElement = (PackageElement) enclosingElement;
try
{
final VersionInfo versionInfo = findValues(version);
note("Found version: " + versionInfo.getVersion());
writeTemplate(packageElement.getQualifiedName().toString(),
version.className(),
versionInfo,
version.template());
}
catch (IOException e)
{
error("IOException occurred while running the annotation processor", e);
}
}
}
}
return true;
}
private void log(final Diagnostic.Kind kind, final String s) {
messager.printMessage(kind, '[' + VersionProcessor.class.getSimpleName() + "] " + s);
}
private void log(final Diagnostic.Kind kind, final String s)
{
messager.printMessage(kind, '[' + VersionProcessor.class.getSimpleName() + "] " + s);
}
private void note(final String s) {
log(Diagnostic.Kind.NOTE, s);
}
private void note(final String s)
{
log(Diagnostic.Kind.NOTE, s);
}
private int parseIntProperty(final Properties p, final String property, final int defaultValue) {
try {
return Integer.parseInt(p.getProperty(property, Integer.toString(defaultValue)));
} catch (NumberFormatException ignore) {
warn("Invalid property value: " + property);
return defaultValue;
}
}
private int parseIntProperty(final Properties p, final String property, final int defaultValue)
{
try
{
return Integer.parseInt(p.getProperty(property, Integer.toString(defaultValue)));
}
catch (NumberFormatException ignore)
{
warn("Invalid property value: " + property);
return defaultValue;
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean process(final Set<? extends TypeElement> annotations, final RoundEnvironment roundEnv) {
for (final Element element : roundEnv.getElementsAnnotatedWith(Version.class)) {
final Version version = element.getAnnotation(Version.class);
if (element.getKind() == ElementKind.CLASS) {
final Element enclosingElement = element.getEnclosingElement();
if (enclosingElement.getKind() == ElementKind.PACKAGE) {
final PackageElement packageElement = (PackageElement) enclosingElement;
try {
final VersionInfo versionInfo = findValues(version);
note("Found version: " + versionInfo.getVersion());
writeTemplate(packageElement.getQualifiedName().toString(),
version.className(),
versionInfo,
version.template());
} catch (IOException e) {
error("IOException occurred while running the annotation processor", e);
}
}
}
}
return true;
}
private void warn(final String s)
{
log(Diagnostic.Kind.WARNING, s);
}
private void warn(final String s) {
log(Diagnostic.Kind.WARNING, s);
}
private void writeTemplate(final String packageName, final String className, final VersionInfo versionInfo,
final String template)
throws IOException
{
final Properties p = new Properties();
final URL url = this.getClass().getClassLoader().getResource(Constants.VELOCITY_PROPERTIES);
private void writeTemplate(final String packageName,
final String className,
final VersionInfo versionInfo,
final String template)
throws IOException {
final Properties p = new Properties();
final URL url = this.getClass().getClassLoader().getResource(Constants.VELOCITY_PROPERTIES);
if (url != null)
{
p.load(url.openStream());
if (url != null) {
p.load(url.openStream());
final VelocityEngine ve = new VelocityEngine(p);
ve.init();
final VelocityEngine ve = new VelocityEngine(p);
ve.init();
final VelocityContext vc = new VelocityContext();
vc.put("packageName", packageName);
vc.put("className", className);
vc.put("project", versionInfo.getProject());
vc.put("buildmeta", versionInfo.getBuildMetadata());
vc.put("epoch", versionInfo.getEpoch());
vc.put("patch", versionInfo.getPatch());
vc.put("major", versionInfo.getMajor());
vc.put("minor", versionInfo.getMinor());
vc.put("prerelease", versionInfo.getPreRelease());
final VelocityContext vc = new VelocityContext();
vc.put("packageName", packageName);
vc.put("className", className);
vc.put("project", versionInfo.getProject());
vc.put("buildmeta", versionInfo.getBuildMetadata());
vc.put("epoch", versionInfo.getEpoch());
vc.put("patch", versionInfo.getPatch());
vc.put("major", versionInfo.getMajor());
vc.put("minor", versionInfo.getMinor());
vc.put("prerelease", versionInfo.getPreRelease());
final Template vt = ve.getTemplate(template);
final Template vt = ve.getTemplate(template);
note("Loaded template: " + vt.getName());
note("Loaded template: " + vt.getName());
final JavaFileObject jfo = filer.createSourceFile(packageName + '.' + className);
try (final Writer writer = jfo.openWriter())
{
vt.merge(vc, writer);
}
final JavaFileObject jfo = filer.createSourceFile(packageName + '.' + className);
try (final Writer writer = jfo.openWriter()) {
vt.merge(vc, writer);
}
note("Generated source: " + jfo.getName());
}
else
{
error("Could not load '" + Constants.VELOCITY_PROPERTIES + "' from jar.");
}
}
note("Generated source: " + jfo.getName());
} else {
error("Could not load '" + Constants.VELOCITY_PROPERTIES + "' from jar.");
}
}
}