Created
November 29, 2012 15:48
-
-
Save fbricon/4169929 to your computer and use it in GitHub Desktop.
Incremental build friendly ModuleAnnotationProcessor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2011 JBoss, by Red Hat, Inc | |
* | |
* 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.jboss.errai.common.rebind; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
import java.io.Writer; | |
import java.net.URI; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.LinkedHashSet; | |
import java.util.List; | |
import java.util.Set; | |
import javax.annotation.processing.AbstractProcessor; | |
import javax.annotation.processing.RoundEnvironment; | |
import javax.annotation.processing.SupportedAnnotationTypes; | |
import javax.annotation.processing.SupportedSourceVersion; | |
import javax.lang.model.SourceVersion; | |
import javax.lang.model.element.Element; | |
import javax.lang.model.element.TypeElement; | |
import javax.tools.FileObject; | |
import javax.tools.StandardLocation; | |
/** | |
* @author Mike Brock | |
*/ | |
@SupportedSourceVersion(SourceVersion.RELEASE_6) | |
@SupportedAnnotationTypes("*") | |
public class ModuleAnnotationProcessor extends AbstractProcessor { | |
final Set<Element> allKnownElements = new HashSet<Element>(); | |
@SuppressWarnings("NullArgumentToVariableArgMethod") | |
@Override | |
public boolean process(final Set<? extends TypeElement> typeElements, | |
final RoundEnvironment roundEnvironment) { | |
allKnownElements.addAll(roundEnvironment.getRootElements()); | |
if (roundEnvironment.processingOver()) { | |
final Set<String> currentElements = new LinkedHashSet<String>(allKnownElements.size()); | |
try { | |
FileObject fo = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", "classlist.mf", null); | |
File existingFile = new File(fo.toUri()); | |
if (existingFile.canRead()) { | |
BufferedReader br = new BufferedReader(new FileReader(existingFile)); | |
String line; | |
while((line = br.readLine()) != null) { | |
currentElements.add(line); | |
} | |
br.close(); | |
} | |
boolean hasContentChanged = false; | |
for (final Element e : allKnownElements) { | |
if (currentElements.add(e.toString())) { | |
hasContentChanged = true; | |
} | |
} | |
if (hasContentChanged) { | |
StringBuilder sb = new StringBuilder(); | |
for(String e : currentElements) { | |
sb.append(e).append('\n'); | |
} | |
final Writer writer = fo.openWriter(); | |
writer.write(sb.toString()); | |
writer.close(); | |
} | |
} | |
catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment