Skip to content

Commit

Permalink
Update the metadata annotation processor to support incremental builds
Browse files Browse the repository at this point in the history
This commit udpdates the metadata annotation processor so that change
data from an incremental build is merged with the metadata from the
previous build.

Closes spring-projectsgh-2321
  • Loading branch information
kdvolder authored and wilkinsona committed Jan 28, 2015
1 parent ccb2828 commit 8df43a8
Show file tree
Hide file tree
Showing 10 changed files with 710 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@
<artifactId>lombok</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- Ensure own annotation processor doens't kick in -->
<!-- Ensure own annotation processor doesn't kick in -->
<proc>none</proc>
</configuration>
</plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand Down Expand Up @@ -62,11 +64,14 @@
*
* @author Stephane Nicoll
* @author Phillip Webb
* @author Kris De Volder
* @since 1.2.0
*/
@SupportedAnnotationTypes({ "*" })
public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor {

public static final String METADATA_PATH = "META-INF/spring-configuration-metadata.json";

static final String CONFIGURATION_PROPERTIES_ANNOTATION = "org.springframework.boot."
+ "context.properties.ConfigurationProperties";

Expand All @@ -85,6 +90,17 @@ public class ConfigurationMetadataAnnotationProcessor extends AbstractProcessor

private ConfigurationMetadata metadata;

/**
* On incremental builds, holds the 'old' metadata (created by the previous build).
*/
private ConfigurationMetadata oldmetadata;

/**
* On incremental builds, keeps track of the types that where presented to the
* processor. This includes types that are not annotated.
*/
protected Set<String> processedSourceTypes;

private TypeUtils typeUtils;

private FieldValuesParser fieldValuesParser;
Expand All @@ -109,6 +125,10 @@ public synchronized void init(ProcessingEnvironment env) {
super.init(env);
this.metadata = new ConfigurationMetadata();
this.typeUtils = new TypeUtils(env);
this.oldmetadata = readMetadata();
if (isIncremental()) {
this.processedSourceTypes = new HashSet<String>();
}
try {
this.fieldValuesParser = new JavaCompilerFieldValuesParser(env);
}
Expand All @@ -119,10 +139,37 @@ public synchronized void init(ProcessingEnvironment env) {
}
}

protected boolean isIncremental() {
return this.oldmetadata != null;
}

protected boolean isDeleted(String sourceType) {
return this.processingEnv.getElementUtils().getTypeElement(sourceType) == null;
}

protected boolean isProcessed(String sourceType) {
return this.processedSourceTypes.contains(sourceType);
}

/**
* Called during incremental build on all the 'root elements' that are being presented
* to the processor.
*/
protected void markAsProcessed(Element element) {
if (element instanceof TypeElement) {
this.processedSourceTypes.add(this.typeUtils.getType(element));
}
}

@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
Elements elementUtils = this.processingEnv.getElementUtils();
if (isIncremental()) {
for (Element element : roundEnv.getRootElements()) {
markAsProcessed(element);
}
}
for (Element element : roundEnv.getElementsAnnotatedWith(elementUtils
.getTypeElement(configurationPropertiesAnnotation()))) {
processElement(element);
Expand Down Expand Up @@ -328,16 +375,19 @@ private Map<String, Object> getAnnotationElementValues(AnnotationMirror annotati
return values;
}

protected void writeMetaData(ConfigurationMetadata metadata) {
protected ConfigurationMetadata writeMetaData(ConfigurationMetadata metadata) {
metadata = mergeAdditionalMetadata(metadata);
if (isIncremental()) {
mergeOldMetadata(metadata);
}
if (!metadata.getItems().isEmpty()) {
try {
FileObject resource = this.processingEnv.getFiler().createResource(
StandardLocation.CLASS_OUTPUT, "",
"META-INF/spring-configuration-metadata.json");
StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
OutputStream outputStream = resource.openOutputStream();
try {
new JsonMarshaller().write(metadata, outputStream);
return metadata;
}
finally {
outputStream.close();
Expand All @@ -347,6 +397,42 @@ protected void writeMetaData(ConfigurationMetadata metadata) {
throw new IllegalStateException(ex);
}
}
return null;
}

protected ConfigurationMetadata readMetadata() {
try {
FileObject resource = this.processingEnv.getFiler().getResource(
StandardLocation.CLASS_OUTPUT, "", METADATA_PATH);
InputStream in = resource.openInputStream();
try {
ConfigurationMetadata data = new ConfigurationMetadata();
data.addAll(new JsonMarshaller().read(in));
if (!data.getItems().isEmpty()) {
return data;
}
}
finally {
in.close();
}
}
catch (IOException e) {
// no 'old' metadata
}
return null;
}

private void mergeOldMetadata(ConfigurationMetadata metadata) {
List<ItemMetadata> items = this.oldmetadata.getItems();
for (ItemMetadata oldItem : items) {
String sourceType = oldItem.getSourceType();
if (sourceType == null || isProcessed(sourceType) || isDeleted(sourceType)) {
// skip
}
else {
metadata.add(oldItem);
}
}
}

private ConfigurationMetadata mergeAdditionalMetadata(ConfigurationMetadata metadata) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* 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;

import java.util.Set;

import org.springframework.boot.configurationprocessor.metadata.ConfigurationMetadata;

/**
* Data object containing information about a finished build.
*
* @author Kris De Volder
*/
public class BuildResult {

public final ConfigurationMetadata metadata;

public final Set<String> processedTypes;

public final boolean isIncremental;

public BuildResult(boolean isIncremental, ConfigurationMetadata metadata,
Set<String> processedTypes) {
this.isIncremental = isIncremental;
this.metadata = metadata;
this.processedTypes = processedTypes;
}

public BuildResult(TestConfigurationMetadataAnnotationProcessor processor) {
this(processor.isIncremental(), processor.getMetadata(),
processor.processedSourceTypes);
}

}
Loading

0 comments on commit 8df43a8

Please sign in to comment.