Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue469 compiler warnings #472

Merged
merged 4 commits into from
Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a way to get the diagnostic messages (the javac version of logging) during the testing. This has the side benefit of preventing them from being printed out during the test.

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);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the diagnostic collector to the compiler task.

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());
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and check it here. There should be no ERROR or WARNING type messages.


//
//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