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 com.sun.tools.javac.tree.JCTree;
  39. import com.sun.tools.javac.tree.TreeInfo;
  40. import org.orekit.annotation.DefaultDataContext;

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

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

  63.     /** Compiler Trees. */
  64.     private Trees trees;

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

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

  74.     @Override
  75.     public void started(final TaskEvent taskEvent) {
  76.     }

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

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

  86.         /** Compilation root. */
  87.         private final CompilationUnitTree root;

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

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

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

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

  113.         /**
  114.          * Check if this bit of code calls into the default data context from outside the
  115.          * default data context.
  116.          *
  117.          * @param tree to check.
  118.          */
  119.         private void check(final Tree tree) {
  120.             final Element element = TreeInfo.symbolFor((JCTree) tree);
  121.             check(tree, element);
  122.         }

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

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

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

  183.     }

  184. }