Skip to content

Commit

Permalink
Issue469 compiler warnings (#472)
Browse files Browse the repository at this point in the history
* Overrode the getSupportedSourceVersion method to the Annotation processor
We don't care about new language constructs because we are only scanning for variable declaration of AndHow Properties.  By using 'latestSupported', the version of whatever JDK is compiling will be accepted.
* Added better class docs

* Added a test to ensure we are not getting compiler warnings

* Formatted for consistent tabs
  • Loading branch information
eeverman authored Dec 5, 2018
1 parent d58d7c6 commit fa35b8a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.io.*;
import java.util.*;
import javax.annotation.processing.*;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.*;
import javax.tools.Diagnostic;

Expand Down Expand Up @@ -63,6 +64,13 @@ public AndHowCompileProcessor() {
runDate = new GregorianCalendar();
}

@Override
public SourceVersion getSupportedSourceVersion() {
//Only scanning for declaration of AndHow Properties, so should
//be immune to most new language constructs.
return SourceVersion.latestSupported();
}

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.yarnandtail.andhow.compile;

import java.util.HashSet;
import java.util.Set;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.ToolProvider;
import org.junit.Before;
import static org.yarnandtail.andhow.compile.AndHowCompileProcessor_PropertyTest.pkg;
import org.yarnandtail.andhow.util.IOUtil;
import org.yarnandtail.compile.MemoryFileManager;
import org.yarnandtail.compile.TestClassLoader;
Expand All @@ -31,6 +26,7 @@ public class AndHowCompileProcessorTestBase {

JavaCompiler compiler;
MemoryFileManager manager;
DiagnosticCollector<JavaFileObject> diagnostics;
TestClassLoader loader;

Set<TestSource> sources; //New set of source files to compile
Expand All @@ -39,6 +35,7 @@ public class AndHowCompileProcessorTestBase {
public void setupTest() {
compiler = ToolProvider.getSystemJavaCompiler();
manager = new MemoryFileManager(compiler);
diagnostics = new DiagnosticCollector();
loader = new TestClassLoader(manager);
sources = new HashSet();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.nio.charset.Charset;
import java.util.*;
import javax.tools.*;
import javax.tools.Diagnostic.Kind;
import org.junit.Test;
import org.yarnandtail.andhow.util.IOUtil;

Expand Down Expand Up @@ -40,13 +41,15 @@ public void testServiceRegistrationOfOneProdAndOneTestInit() throws Exception {
sources.add(buildTestSource(pkg, AndHowTestInitAbstract_NAME));
sources.add(buildTestSource(pkg, AndHowTestInitA_NAME));

JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

String prodInitSvs = IOUtil.toString(loader.getResourceAsStream(INIT_SVS_PATH), Charset.forName("UTF-8"));
String testInitSvs = IOUtil.toString(loader.getResourceAsStream(TEST_INIT_SVS_PATH), Charset.forName("UTF-8"));

assertEquals("Should be no warn/errors", 0, diagnostics.getDiagnostics().stream().filter(
d -> d.getKind().equals(Kind.ERROR) || d.getKind().equals(Kind.WARNING)).count());

//
//Test the initiation files
Expand All @@ -66,12 +69,15 @@ public void testServiceRegistrationOfOneProdInit() throws Exception {
sources.add(buildTestSource(pkg, AndHowInitAbstract_NAME));
sources.add(buildTestSource(pkg, AndHowInitA_NAME));

JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

String prodInitSvs = IOUtil.toString(loader.getResourceAsStream(INIT_SVS_PATH), Charset.forName("UTF-8"));

assertEquals("Should be no warn/errors", 0, diagnostics.getDiagnostics().stream().filter(
d -> d.getKind().equals(Kind.ERROR) || d.getKind().equals(Kind.WARNING)).count());

//
//Test the initiation files
assertNotNull(prodInitSvs);
Expand All @@ -87,12 +93,15 @@ public void testServiceRegistrationOfOneTestInit() throws Exception {
sources.add(buildTestSource(pkg, AndHowTestInitAbstract_NAME));
sources.add(buildTestSource(pkg, AndHowTestInitA_NAME));

JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

String testInitSvs = IOUtil.toString(loader.getResourceAsStream(TEST_INIT_SVS_PATH), Charset.forName("UTF-8"));

assertEquals("Should be no warn/errors", 0, diagnostics.getDiagnostics().stream().filter(
d -> d.getKind().equals(Kind.ERROR) || d.getKind().equals(Kind.WARNING)).count());


//
//Test the initiation files
Expand All @@ -116,6 +125,8 @@ public void testServiceRegistrationOfAndHowInitWithTooManyProdInstances() throws
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

fail("Should have thrown an exception");

} catch (RuntimeException e) {

Expand Down Expand Up @@ -155,6 +166,8 @@ public void testServiceRegistrationOfAndHowInitWithTooManyTestInstances() throws
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

fail("Should have thrown an exception");

} catch (RuntimeException e) {

assertNotNull(e.getCause());
Expand Down Expand Up @@ -195,6 +208,8 @@ public void testServiceRegistrationOfAndHowInitWithTooManyInstAndBadProperties()
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

fail("Should have thrown an exception");

} catch (RuntimeException e) {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package org.yarnandtail.andhow.compile;

import org.yarnandtail.compile.*;
import org.yarnandtail.andhow.service.*;
import java.nio.charset.Charset;
import java.util.*;
import javax.tools.*;
import static org.junit.Assert.*;
import org.junit.Test;
import org.yarnandtail.andhow.compile.CompileProblem.PropMissingFinal;
import org.yarnandtail.andhow.compile.CompileProblem.PropMissingStatic;
import org.yarnandtail.andhow.compile.CompileProblem.PropMissingStaticFinal;
import org.yarnandtail.andhow.service.*;
import org.yarnandtail.andhow.util.IOUtil;
import static org.yarnandtail.andhow.compile.CompileProblem.*;

import static org.junit.Assert.*;
import org.junit.Before;

/**
* A lot of this code was borrowed from here:
Expand All @@ -34,14 +33,17 @@ public void testComplexNestedPropertySampleClass() throws Exception {

sources.add(buildTestSource(pkg, classSimpleName));

JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

Object genClass = loader.loadClass(genName(pkg, classSimpleName)).newInstance();
String genSvsFile = IOUtil.toString(
loader.getResourceAsStream(REGISTRAR_SVS_PATH), Charset.forName("UTF-8"));

assertEquals("Should be no warn/errors", 0, diagnostics.getDiagnostics().stream().filter(
d -> d.getKind().equals(Diagnostic.Kind.ERROR) || d.getKind().equals(Diagnostic.Kind.WARNING)).count());

assertNotNull(genClass);

PropertyRegistrar registrar = (PropertyRegistrar)genClass;
Expand Down Expand Up @@ -81,14 +83,17 @@ public void testSimpleHappyPathClass() throws Exception {

sources.add(buildTestSource(pkg, classSimpleName));

JavaCompiler.CompilationTask task = compiler.getTask(null, manager, null, options, null, sources);
JavaCompiler.CompilationTask task = compiler.getTask(null, manager, diagnostics, options, null, sources);
task.setProcessors(Collections.singleton(new AndHowCompileProcessor()));
task.call();

Object genClass = loader.loadClass(genName(pkg, classSimpleName)).newInstance();
String genSvsFile = IOUtil.toString(
loader.getResourceAsStream(REGISTRAR_SVS_PATH), Charset.forName("UTF-8"));

assertEquals("Should be no warn/errors", 0, diagnostics.getDiagnostics().stream().filter(
d -> d.getKind().equals(Diagnostic.Kind.ERROR) || d.getKind().equals(Diagnostic.Kind.WARNING)).count());

assertNotNull(genClass);

PropertyRegistrar registrar = (PropertyRegistrar)genClass;
Expand Down

0 comments on commit fa35b8a

Please sign in to comment.