Fail the build if the meta-data are invalid
Make sure to fail the build with a proper compilation error message if the user-defined meta-data are invalid. For now, this takes care of the JSON format but other checks may be added in the future. Closes gh-3329
This commit is contained in:
@@ -47,6 +47,7 @@ import javax.tools.Diagnostic.Kind;
|
||||
import org.springframework.boot.configurationprocessor.fieldvalues.FieldValuesParser;
|
||||
import org.springframework.boot.configurationprocessor.fieldvalues.javac.JavaCompilerFieldValuesParser;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemMetadata;
|
||||
|
||||
/**
|
||||
@@ -354,13 +355,15 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
}
|
||||
catch (FileNotFoundException ex) {
|
||||
// No additional metadata
|
||||
return metadata;
|
||||
}
|
||||
catch (InvalidConfigurationMetadataException e) {
|
||||
log(e.getKind(), e.getMessage());
|
||||
}
|
||||
catch (Exception ex) {
|
||||
logWarning("Unable to merge additional metadata");
|
||||
logWarning(getStackTrace(ex));
|
||||
return metadata;
|
||||
}
|
||||
return metadata;
|
||||
}
|
||||
|
||||
private String getStackTrace(Exception ex) {
|
||||
@@ -370,7 +373,11 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor
|
||||
}
|
||||
|
||||
private void logWarning(String msg) {
|
||||
this.processingEnv.getMessager().printMessage(Kind.WARNING, msg);
|
||||
log(Kind.WARNING, msg);
|
||||
}
|
||||
|
||||
private void log(Kind kind, String msg) {
|
||||
this.processingEnv.getMessager().printMessage(kind, msg);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -21,12 +21,15 @@ import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.annotation.processing.ProcessingEnvironment;
|
||||
import javax.tools.Diagnostic;
|
||||
import javax.tools.FileObject;
|
||||
import javax.tools.StandardLocation;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.InvalidConfigurationMetadataException;
|
||||
import org.springframework.boot.configurationprocessor.metadata.JsonMarshaller;
|
||||
|
||||
/**
|
||||
@@ -83,6 +86,10 @@ public class MetadataStore {
|
||||
catch (IOException ex) {
|
||||
return null;
|
||||
}
|
||||
catch (JSONException ex) {
|
||||
throw new InvalidConfigurationMetadataException("Invalid additional meta-data in '" +
|
||||
METADATA_PATH + "': " + ex.getMessage(), Diagnostic.Kind.ERROR);
|
||||
}
|
||||
finally {
|
||||
in.close();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright 2012-2015 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.configurationprocessor.metadata;
|
||||
|
||||
import javax.tools.Diagnostic;
|
||||
|
||||
/**
|
||||
* Thrown to indicate that some meta-data is invalid. Define the
|
||||
* severity to determine whether it has to fail the build.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class InvalidConfigurationMetadataException extends RuntimeException {
|
||||
|
||||
private final Diagnostic.Kind kind;
|
||||
|
||||
public InvalidConfigurationMetadataException(String message, Diagnostic.Kind kind) {
|
||||
super(message);
|
||||
this.kind = kind;
|
||||
}
|
||||
|
||||
public Diagnostic.Kind getKind() {
|
||||
return kind;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -27,6 +27,7 @@ import org.json.JSONObject;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;
|
||||
import org.springframework.boot.configurationprocessor.metadata.ItemHint;
|
||||
@@ -52,6 +53,7 @@ import org.springframework.boot.configurationsample.specific.InnerClassAnnotated
|
||||
import org.springframework.boot.configurationsample.specific.InnerClassProperties;
|
||||
import org.springframework.boot.configurationsample.specific.InnerClassRootConfig;
|
||||
import org.springframework.boot.configurationsample.specific.SimplePojo;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.Matchers.empty;
|
||||
@@ -78,6 +80,9 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
@Rule
|
||||
public ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private TestCompiler compiler;
|
||||
|
||||
@Before
|
||||
@@ -346,6 +351,16 @@ public class ConfigurationMetadataAnnotationProcessorTests {
|
||||
.fromSource(AdditionalMetadata.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mergeOfInvalidAdditionalMetadata() throws IOException {
|
||||
File additionalMetadataFile = createAdditionalMetadataFile();
|
||||
FileCopyUtils.copy("Hello World", new FileWriter(additionalMetadataFile));
|
||||
|
||||
thrown.expect(IllegalStateException.class);
|
||||
thrown.expectMessage("Compilation failed");
|
||||
compile(SimpleProperties.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mergingOfSimpleHint() throws Exception {
|
||||
writeAdditionalHints(ItemHint.newHint("simple.the-name", new ItemHint.ValueHint(
|
||||
|
||||
Reference in New Issue
Block a user