< prev index next >

test/langtools/tools/javac/classfiles/attributes/innerclasses/InnerClassesTestBase.java

Print this page


   1 /*
   2  * Copyright (c) 2014, 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  */


 182      *
 183      * @param innerClassType the inner class type
 184      */
 185     public void setInnerClassType(ClassType innerClassType) {
 186         this.innerClassType = innerClassType;
 187     }
 188 
 189     /**
 190      * Sets the outer class type.
 191      *
 192      * @param outerClassType the outer class type
 193      */
 194     public void setOuterClassType(ClassType outerClassType) {
 195         this.outerClassType = outerClassType;
 196     }
 197 
 198     private void test(String classToTest, TestCase test, String...skipClasses) {
 199         printf("Testing :\n%s\n", test.getSource());
 200         try {
 201             Map<String, Set<String>> class2Flags = test.getFlags();
 202             ClassFile cf = readClassFile(compile(test.getSource())
 203                     .getClasses().get(classToTest));
 204             InnerClasses_attribute innerClasses = (InnerClasses_attribute)
 205                     cf.getAttribute(Attribute.InnerClasses);
 206             int count = 0;
 207             for (Attribute a : cf.attributes.attrs) {
 208                 if (a instanceof InnerClasses_attribute) {
 209                     ++count;
 210                 }
 211             }
 212             checkEquals(1, count, "Number of inner classes attribute");
 213             if (!checkNotNull(innerClasses, "InnerClasses attribute should not be null")) {
 214                 return;
 215             }
 216             checkEquals(cf.constant_pool.
 217                     getUTF8Info(innerClasses.attribute_name_index).value, "InnerClasses",
 218                     "innerClasses.attribute_name_index");
 219             // Inner Classes attribute consists of length (2 bytes)
 220             // and 8 bytes for each inner class's entry.
 221             checkEquals(innerClasses.attribute_length,
 222                     2 + 8 * class2Flags.size(), "innerClasses.attribute_length");


 305             list.add(new TestCase(sb.toString(), class2Flags));
 306         }
 307         return list;
 308     }
 309 
 310     /**
 311      * Methods returns flags which must have type.
 312      *
 313      * @param type class, interface, enum or annotation
 314      * @param mods modifiers
 315      * @return set of access flags
 316      */
 317     protected Set<String> getFlags(ClassType type, List<Modifier> mods) {
 318         Set<String> flags = mods.stream()
 319                 .map(Modifier::getString)
 320                 .filter(str -> !str.isEmpty())
 321                 .map(str -> "ACC_" + str.toUpperCase())
 322                 .collect(Collectors.toSet());
 323         type.addSpecificFlags(flags);
 324         return flags;




 325     }
 326 
 327     private List<List<Modifier>> getAllCombinations(Modifier[] accessModifiers, Modifier[] otherModifiers) {
 328         List<List<Modifier>> list = new ArrayList<>();
 329         for (Modifier access : accessModifiers) {
 330             for (int i = 0; i < otherModifiers.length; ++i) {
 331                 Modifier mod1 = otherModifiers[i];
 332                 for (int j = i + 1; j < otherModifiers.length; ++j) {
 333                     Modifier mod2 = otherModifiers[j];
 334                     if (isForbidden(mod1, mod2)) {
 335                         continue;
 336                     }
 337                     list.add(Arrays.asList(access, mod1, mod2));
 338                 }
 339                 if (mod1 == Modifier.EMPTY) {
 340                     list.add(Collections.singletonList(access));
 341                 }
 342             }
 343         }
 344         return list;


   1 /*
   2  * Copyright (c) 2014, 2018, 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  */


 182      *
 183      * @param innerClassType the inner class type
 184      */
 185     public void setInnerClassType(ClassType innerClassType) {
 186         this.innerClassType = innerClassType;
 187     }
 188 
 189     /**
 190      * Sets the outer class type.
 191      *
 192      * @param outerClassType the outer class type
 193      */
 194     public void setOuterClassType(ClassType outerClassType) {
 195         this.outerClassType = outerClassType;
 196     }
 197 
 198     private void test(String classToTest, TestCase test, String...skipClasses) {
 199         printf("Testing :\n%s\n", test.getSource());
 200         try {
 201             Map<String, Set<String>> class2Flags = test.getFlags();
 202             ClassFile cf = readClassFile(compile(getCompileOptions(), test.getSource())
 203                     .getClasses().get(classToTest));
 204             InnerClasses_attribute innerClasses = (InnerClasses_attribute)
 205                     cf.getAttribute(Attribute.InnerClasses);
 206             int count = 0;
 207             for (Attribute a : cf.attributes.attrs) {
 208                 if (a instanceof InnerClasses_attribute) {
 209                     ++count;
 210                 }
 211             }
 212             checkEquals(1, count, "Number of inner classes attribute");
 213             if (!checkNotNull(innerClasses, "InnerClasses attribute should not be null")) {
 214                 return;
 215             }
 216             checkEquals(cf.constant_pool.
 217                     getUTF8Info(innerClasses.attribute_name_index).value, "InnerClasses",
 218                     "innerClasses.attribute_name_index");
 219             // Inner Classes attribute consists of length (2 bytes)
 220             // and 8 bytes for each inner class's entry.
 221             checkEquals(innerClasses.attribute_length,
 222                     2 + 8 * class2Flags.size(), "innerClasses.attribute_length");


 305             list.add(new TestCase(sb.toString(), class2Flags));
 306         }
 307         return list;
 308     }
 309 
 310     /**
 311      * Methods returns flags which must have type.
 312      *
 313      * @param type class, interface, enum or annotation
 314      * @param mods modifiers
 315      * @return set of access flags
 316      */
 317     protected Set<String> getFlags(ClassType type, List<Modifier> mods) {
 318         Set<String> flags = mods.stream()
 319                 .map(Modifier::getString)
 320                 .filter(str -> !str.isEmpty())
 321                 .map(str -> "ACC_" + str.toUpperCase())
 322                 .collect(Collectors.toSet());
 323         type.addSpecificFlags(flags);
 324         return flags;
 325     }
 326 
 327     protected List<String> getCompileOptions() {
 328         return Collections.emptyList();
 329     }
 330 
 331     private List<List<Modifier>> getAllCombinations(Modifier[] accessModifiers, Modifier[] otherModifiers) {
 332         List<List<Modifier>> list = new ArrayList<>();
 333         for (Modifier access : accessModifiers) {
 334             for (int i = 0; i < otherModifiers.length; ++i) {
 335                 Modifier mod1 = otherModifiers[i];
 336                 for (int j = i + 1; j < otherModifiers.length; ++j) {
 337                     Modifier mod2 = otherModifiers[j];
 338                     if (isForbidden(mod1, mod2)) {
 339                         continue;
 340                     }
 341                     list.add(Arrays.asList(access, mod1, mod2));
 342                 }
 343                 if (mod1 == Modifier.EMPTY) {
 344                     list.add(Collections.singletonList(access));
 345                 }
 346             }
 347         }
 348         return list;


< prev index next >