test/tools/javac/processing/model/element/TestResourceVariable.java

Print this page


   1 /*
   2  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug  6911256 6964740 6967842
  27  * @summary Test that the resource variable kind is appropriately set
  28  * @author  Joseph D. Darcy
  29  * @library ../../../lib
  30  * @build   JavacTestingAbstractProcessor TestResourceVariable
  31  * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java
  32  */
  33 
  34 // Bug should be filed for this misbehavior
  35 
  36 import java.io.*;
  37 import javax.annotation.processing.*;
  38 import javax.lang.model.*;
  39 import javax.lang.model.element.*;
  40 import javax.lang.model.type.*;
  41 import javax.lang.model.util.*;
  42 import java.util.*;
  43 import com.sun.source.tree.*;
  44 import com.sun.source.util.*;
  45 import static javax.tools.Diagnostic.Kind.*;
  46 
  47 /**
  48  * Using the tree API, retrieve element representations of the
  49  * resource of an ARM block and verify their kind tags are set
  50  * appropriately.
  51  */
  52 public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable {
  53     int resourceVariableCount = 0;
  54 
  55     public boolean process(Set<? extends TypeElement> annotations,


  65                         treePath.getCompilationUnit());
  66            }
  67            if (resourceVariableCount != 3)
  68                throw new RuntimeException("Bad resource variable count " +
  69                                           resourceVariableCount);
  70        }
  71        return true;
  72     }
  73 
  74     @Override
  75     public void close() {}
  76 
  77     private void test1() {
  78         try(TestResourceVariable trv = this) {}
  79     }
  80 
  81     private void test2() {
  82         try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {}
  83     }
  84 



























  85     class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
  86        private Trees trees;
  87 
  88        public ResourceVariableScanner(Trees trees) {
  89            super();
  90            this.trees = trees;
  91        }
  92        @Override
  93        public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
  94            Element element = trees.getElement(trees.getPath(cu, node));
  95            if (element == null) {
  96                System.out.println("Null variable element: " + node);
  97            } else {
  98                System.out.println("Name: " + element.getSimpleName() +
  99                                   "\tKind: " + element.getKind());
 100            }
 101            if (element != null &&
 102                element.getKind() == ElementKind.RESOURCE_VARIABLE) {
 103                resourceVariableCount++;
 104            }
 105            return super.visitVariable(node, cu);
 106        }
 107    }
 108 }
   1 /*
   2  * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug  6911256 6964740 6967842 6961571
  27  * @summary Test that the resource variable kind is appropriately set
  28  * @author  Joseph D. Darcy
  29  * @library ../../../lib
  30  * @build   JavacTestingAbstractProcessor TestResourceVariable
  31  * @compile -processor TestResourceVariable -proc:only TestResourceVariable.java
  32  */
  33 


  34 import java.io.*;
  35 import javax.annotation.processing.*;
  36 import javax.lang.model.*;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.type.*;
  39 import javax.lang.model.util.*;
  40 import java.util.*;
  41 import com.sun.source.tree.*;
  42 import com.sun.source.util.*;
  43 import static javax.tools.Diagnostic.Kind.*;
  44 
  45 /**
  46  * Using the tree API, retrieve element representations of the
  47  * resource of an ARM block and verify their kind tags are set
  48  * appropriately.
  49  */
  50 public class TestResourceVariable extends JavacTestingAbstractProcessor implements AutoCloseable {
  51     int resourceVariableCount = 0;
  52 
  53     public boolean process(Set<? extends TypeElement> annotations,


  63                         treePath.getCompilationUnit());
  64            }
  65            if (resourceVariableCount != 3)
  66                throw new RuntimeException("Bad resource variable count " +
  67                                           resourceVariableCount);
  68        }
  69        return true;
  70     }
  71 
  72     @Override
  73     public void close() {}
  74 
  75     private void test1() {
  76         try(TestResourceVariable trv = this) {}
  77     }
  78 
  79     private void test2() {
  80         try(TestResourceVariable trv1 = this; TestResourceVariable trv2 = trv1) {}
  81     }
  82 
  83     /**
  84      * Verify that a resource variable modeled as an element behaves
  85      * as expected under 6 and 7 specific visitors.
  86      */
  87     private static void testResourceVariable(Element element) {
  88         ElementVisitor visitor6 = new ElementKindVisitor6<Void, Void>() {};
  89 
  90         try {
  91             visitor6.visit(element);
  92             throw new RuntimeException("Expected UnknownElementException not thrown.");
  93         } catch (UnknownElementException uee) {
  94             ; // Expected.
  95         }
  96 
  97         ElementKindVisitor7 visitor7 = new ElementKindVisitor7<Object, Void>() {
  98             @Override
  99             public Object visitVariableAsResourceVariable(VariableElement e,
 100                                                           Void p) {
 101                 return e; // a non-null value
 102             }
 103         };
 104 
 105         if (visitor7.visit(element) == null) {
 106             throw new RuntimeException("Null result of resource variable visitation.");
 107         }
 108     }
 109 
 110     class ResourceVariableScanner extends TreeScanner<Void, CompilationUnitTree> {
 111        private Trees trees;
 112 
 113        public ResourceVariableScanner(Trees trees) {
 114            super();
 115            this.trees = trees;
 116        }
 117        @Override
 118        public Void visitVariable(VariableTree node, CompilationUnitTree cu) {
 119            Element element = trees.getElement(trees.getPath(cu, node));
 120            


 121            System.out.println("Name: " + element.getSimpleName() +
 122                               "\tKind: " + element.getKind());
 123            if (element.getKind() == ElementKind.RESOURCE_VARIABLE) {
 124                testResourceVariable(element);

 125                resourceVariableCount++;
 126            }
 127            return super.visitVariable(node, cu);
 128        }
 129     }
 130 }