DefaultDataContextPlugin.java

  1. /* Contributed in the public domain.
  2.  * Licensed to CS GROUP (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *   http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.orekit.compiler.plugin;

  18. import javax.annotation.processing.SupportedAnnotationTypes;
  19. import javax.annotation.processing.SupportedSourceVersion;
  20. import javax.lang.model.SourceVersion;
  21. import javax.lang.model.element.Element;
  22. import javax.tools.Diagnostic;
  23. import java.util.EnumSet;
  24. import java.util.Set;

  25. import com.sun.source.tree.CompilationUnitTree;
  26. import com.sun.source.tree.IdentifierTree;
  27. import com.sun.source.tree.MemberSelectTree;
  28. import com.sun.source.tree.NewClassTree;
  29. import com.sun.source.tree.Tree;
  30. import com.sun.source.util.JavacTask;
  31. import com.sun.source.util.Plugin;
  32. import com.sun.source.util.TaskEvent;
  33. import com.sun.source.util.TaskEvent.Kind;
  34. import com.sun.source.util.TaskListener;
  35. import com.sun.source.util.TreePath;
  36. import com.sun.source.util.TreeScanner;
  37. import com.sun.source.util.Trees;
  38. import org.orekit.annotation.DefaultDataContext;

  39. /**
  40.  * Processes {@link DefaultDataContext} to issue warnings at compile time.
  41.  *
  42.  * <p>To use this plugin add {@code -Xplugin:dataContextPlugin} to the javac command line.
  43.  * Tested with OpenJDK 8 and 11.
  44.  *
  45.  * <p>Do not reference this class unless executing within {@code javac} or you have added
  46.  * {@code tools.jar} to the class path. {@code tools.jar} is part of the JDK, not JRE, and
  47.  * is typically located at {@code JAVA_HOME/../lib/tools.jar}.
  48.  *
  49.  * @author Evan Ward
  50.  * @since 10.1
  51.  */
  52. @SupportedAnnotationTypes("org.orekit.annotation.DefaultDataContext")
  53. @SupportedSourceVersion(SourceVersion.RELEASE_8)
  54. public class DefaultDataContextPlugin implements Plugin, TaskListener {

  55.     /** Warning message. */
  56.     static final String MESSAGE = "Use of the default data context from a scope not " +
  57.             "annotated with @DefaultDataContext. This code may be unintentionally " +
  58.             "using the default data context.";
  59.     /** Annotation to search for. */
  60.     private static final Class<DefaultDataContext> ANNOTATION = DefaultDataContext.class;

  61.     /** Compiler Trees. */
  62.     private Trees trees;

  63.     @Override
  64.     public String getName() {
  65.         return "dataContextPlugin";
  66.     }

  67.     @Override
  68.     public synchronized void init(final JavacTask javacTask, final String... args) {
  69.         javacTask.addTaskListener(this);
  70.         trees = Trees.instance(javacTask);
  71.     }

  72.     @Override
  73.     public void started(final TaskEvent taskEvent) {
  74.     }

  75.     @Override
  76.     public void finished(final TaskEvent taskEvent) {
  77.         if (taskEvent.getKind() == Kind.ANALYZE) {
  78.             final CompilationUnitTree root = taskEvent.getCompilationUnit();
  79.             root.accept(new AnnotationTreeScanner(root), null);
  80.         }
  81.     }

  82.     /** Finds when an annotation is used and checks the scope has the same annotation. */
  83.     private class AnnotationTreeScanner extends TreeScanner<Void, Void> {

  84.         /** Compilation root. */
  85.         private final CompilationUnitTree root;

  86.         /**
  87.          * Create a scanner.
  88.          *
  89.          * @param root of the compilation.
  90.          */
  91.         AnnotationTreeScanner(final CompilationUnitTree root) {
  92.             this.root = root;
  93.         }

  94.         @Override
  95.         public Void visitIdentifier(final IdentifierTree identifierTree,
  96.                                     final Void aVoid) {
  97.             check(identifierTree);
  98.             return super.visitIdentifier(identifierTree, aVoid);
  99.         }

  100.         @Override
  101.         public Void visitMemberSelect(final MemberSelectTree memberSelectTree,
  102.                                       final Void aVoid) {
  103.             check(memberSelectTree);
  104.             return super.visitMemberSelect(memberSelectTree, aVoid);
  105.         }

  106.         @Override
  107.         public Void visitNewClass(final NewClassTree newClassTree, final Void aVoid) {
  108.             check(newClassTree);
  109.             return super.visitNewClass(newClassTree, aVoid);
  110.         }

  111.         /**
  112.          * Check if this bit of code calls into the default data context from outside the
  113.          * default data context.
  114.          *
  115.          * @param tree to check.
  116.          */
  117.         private void check(final Tree tree) {
  118.             final Element element = trees.getElement(trees.getPath(root, tree));
  119.             check(tree, element);
  120.         }

  121.         /**
  122.          * Check tricky bits of code.
  123.          *
  124.          * @param tree    used to check the containing scope and for logging.
  125.          * @param element to check for {@link #ANNOTATION}.
  126.          */
  127.         private void check(final Tree tree, final Element element) {
  128.             // element and its containing scopes.
  129.             if (isAnyElementAnnotated(element)) {
  130.                 // using code annotated with @DefaultDataContext
  131.                 // check if current scope is also annotated
  132.                 if (!isAnyElementAnnotated(trees.getPath(root, tree))) {
  133.                     // calling the default data context from a method without an annotation
  134.                     final String message = MESSAGE + " Used: " + element.getKind() + " " + element;
  135.                     trees.printMessage(Diagnostic.Kind.WARNING, message, tree, root);
  136.                 }
  137.             }
  138.         }

  139.         /**
  140.          * Determine if any enclosing element has {@link #ANNOTATION}. Walks towards the
  141.          * tree root checking each node for the annotation.
  142.          *
  143.          * @param element to start the search from. May be {@code null}.
  144.          * @return {@code true} if {@code element} or any of its parents are annotated,
  145.          * {@code false} otherwise.
  146.          */
  147.         private boolean isAnyElementAnnotated(final Element element) {
  148.             Element e = element;
  149.             while (e != null) {
  150.                 if (e.getAnnotation(ANNOTATION) != null) {
  151.                     return true;
  152.                 }
  153.                 e = e.getEnclosingElement();
  154.             }
  155.             return false;
  156.         }

  157.         /**
  158.          * Determine if any enclosing tree has {@link #ANNOTATION}. Walks towards the tree
  159.          * root checking each node for the annotation.
  160.          *
  161.          * @param path to start the search from. May be {@code null}.
  162.          * @return {@code true} if {@code path} or any of its parents are annotated,
  163.          * {@code false} otherwise.
  164.          */
  165.         private boolean isAnyElementAnnotated(final TreePath path) {
  166.             // Kinds of declarations which can be annotated
  167.             final Set<Tree.Kind> toCheck = EnumSet.of(
  168.                     Tree.Kind.METHOD, Tree.Kind.CLASS, Tree.Kind.VARIABLE,
  169.                     Tree.Kind.INTERFACE, Tree.Kind.ENUM);
  170.             TreePath next = path;
  171.             while (next != null) {
  172.                 if (toCheck.contains(next.getLeaf().getKind())) {
  173.                     if (trees.getElement(next).getAnnotation(ANNOTATION) != null) {
  174.                         return true;
  175.                     }
  176.                 }
  177.                 next = next.getParentPath();
  178.             }
  179.             return false;
  180.         }

  181.     }

  182. }