Skip to content

Commit

Permalink
PluginAnnotationProcessor always succeed; now it should compile in Ja…
Browse files Browse the repository at this point in the history
…va 1.7
  • Loading branch information
bkarak committed Nov 2, 2012
1 parent 3a52e31 commit d814eca
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,172 +32,28 @@
*/
package eu.sqooss.service.abstractmetric;

import com.sun.source.tree.MethodTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.source.util.TreePathScanner;
import com.sun.source.util.Trees;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.tools.Diagnostic.Kind;
import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.ProcessingEnvironment;
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.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;

@SupportedSourceVersion(SourceVersion.RELEASE_6)
//@SupportedSourceVersion(SourceVersion.RELEASE_6)
@SupportedAnnotationTypes("eu.sqooss.service.abstractmetric.*")
public class PluginAnnotationProcessor extends AbstractProcessor {

Set<String> declActivators = new HashSet<String>();

private Trees trees;

@Override
public void init(ProcessingEnvironment pe) {
super.init(pe);
trees = Trees.instance(pe);
}

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

MethodVisitor mv = new MethodVisitor();

for (Element e : roundEnvironment.getRootElements()) {
for (AnnotationMirror mirror : e.getAnnotationMirrors()) {
String annotationType = mirror.getAnnotationType().toString();

if (annotationType.equals(MetricDeclarations.class.getName())) {
processMetricDeclarations(mirror);
} else if (annotationType.equals(MetricDecl.class.getName())) {
processingEnv.getMessager().printMessage(Kind.ERROR,
"The @MetricDecl annotation is only allowed " +
"as a child of @MetricDeclarations");
}
}

TreePath tp = trees.getPath(e);
mv.scan(tp, trees);
}



public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {
return true;
}

private void processMetricDeclarations(AnnotationMirror aMirror) {
try {
Map<? extends ExecutableElement, ? extends AnnotationValue> mirrorMap = aMirror.getElementValues();

for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> mirrorEntry : mirrorMap.entrySet()) {
final String mirrorKey = mirrorEntry.getKey().toString();

if (mirrorKey.equals("metrics()")) {
List<? extends AnnotationValue> anns = extractMetricDecl(mirrorEntry.getValue());
for (AnnotationValue annVal : anns) {
extractMetricDeclParams(annVal);
}
}
}
} catch (Exception ex) {
processingEnv.getMessager().printMessage(Kind.ERROR,
"processMetricDeclarations: " + ex.getMessage());
}
}

private List<? extends AnnotationValue> extractMetricDecl(
AnnotationValue aAnnotationValue) {
return (List<? extends AnnotationValue>) aAnnotationValue.getValue();
}

private void extractMetricDeclParams(AnnotationValue aAnnotationValue) {

AnnotationMirror am = (AnnotationMirror) aAnnotationValue;
Map<? extends ExecutableElement, ? extends AnnotationValue> amMap = am
.getElementValues();
String mnemonic = "", descr = "", act = "";
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> amMirrorEntry : amMap.entrySet()) {
String amMirrorKey = amMirrorEntry.getKey().toString();


if (amMirrorKey.equals("mnemonic()")) {
mnemonic = amMirrorEntry.getValue().toString();
mnemonic = mnemonic.substring(1, mnemonic.length() - 1);
} else if (amMirrorKey.equals("descr()")) {
descr = amMirrorEntry.getValue().toString();
} else if (amMirrorKey.equals("activator()")) {
act = amMirrorEntry.getValue().toString();
}
}

if (mnemonic.length() > 10)
processingEnv.getMessager().printMessage(Kind.ERROR,
"Mnemonic " + mnemonic + " is too long. " +
"A metric mnemonic can only be < 10 chars long");

if (mnemonic.equals(""))
processingEnv.getMessager().printMessage(Kind.ERROR,
"Metric mnemonic is an empty String");

if (descr == null || descr.length() <= 0)
processingEnv.getMessager().printMessage(Kind.WARNING,
"A valid description should be specified with metric "
+ mnemonic);

declActivators.add(act);
}

/*
* A simple visitor that records method objects for methods that comply with
* the Alitheia Core plug-in interface requirements.
*/
private class MethodVisitor extends TreePathScanner<Object, Trees> {

List<MethodTree> methods = new ArrayList<MethodTree>();

@Override
public Object visitMethod(MethodTree methodTree, Trees trees) {
String name = methodTree.getName().toString();

if (name.equals("run")) {
if (!methodTree.getReturnType().toString().equals("void"))
return super.visitMethod(methodTree, trees);

List<? extends VariableTree> params = methodTree.getParameters();

// The run method has exactly 1 argument
if (params.size() != 1)
return super.visitMethod(methodTree, trees);

String paramType = params.get(0).getType().toString();

if (paramType.startsWith("eu.sqooss.service.db"))
methods.add(methodTree);
}

if (name.equals("getResult")) {
methods.add(methodTree);
}

return super.visitMethod(methodTree, trees);
}

public List<MethodTree> getMethodList() {
return methods;
}
}
}
1 change: 1 addition & 0 deletions external/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# TODO: Describe the usage of external directory
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ public void update() throws Exception {
if (db.isDBSessionActive())db.commitDBSession();
}

@Override
public void jobStateChanged(Job j, State newState) {
if (newState == State.Error || newState == State.Finished)
progress = 100 - (float) (((double)jobCounter.decrementAndGet() / (double)numVersions) * 100);
}

@Override
public int progress() {
return (int) progress;
}
Expand Down
10 changes: 0 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- configure execution -->
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -262,5 +253,4 @@
</build>
</profile>
</profiles>

</project>

0 comments on commit d814eca

Please sign in to comment.