1 /*
   2  * Copyright (c) 2013, 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  * @run main BadClassFiles
  27  * @summary The reflection API should throw the correct exceptions.
  28  */
  29 import java.lang.Class;
  30 import java.lang.reflect.Method;
  31 import java.lang.reflect.Parameter;
  32 import java.lang.reflect.MalformedParametersException;
  33 
  34 public class BadClassFiles {
  35     private int errors = 0;
  36 
  37     private final Class<?>[] classes = {
  38         EmptyName.class,
  39         BadModifiers.class,
  40         BadNameIndex.class,
  41         NameIndexOutOfBounds.class,
  42         // Name with .
  43         BadName1.class,
  44         // Name with [
  45         BadName2.class,
  46         // Name with ;
  47         BadName3.class,
  48         // Name with /
  49         BadName4.class,
  50         // Name with single unicode backspace
  51         BadName5.class,
  52         // Name with a backspace that becomes empty because of it.
  53         BadName6.class
  54     };
  55 
  56     public static void main(String... args) throws NoSuchMethodException {
  57         new BadClassFiles().run();
  58     }
  59 
  60     public void assertBadParameters(Class<?> cls) throws NoSuchMethodException {
  61         try {
  62             System.err.println("Trying " + cls);
  63             final Method method = cls.getMethod("m", int.class, int.class);
  64             final Parameter[] params = method.getParameters();
  65             System.err.println("Name " + params[0].getName());
  66             System.err.println("Did not see expected exception");
  67             errors++;
  68         } catch(MalformedParametersException e) {
  69             System.err.println("Expected exception seen");
  70         }
  71     }
  72 
  73     public void run() throws NoSuchMethodException {
  74         for (Class<?> cls : classes)
  75             assertBadParameters(cls);
  76 
  77         if (errors != 0)
  78             throw new RuntimeException(errors + " errors in test");
  79     }
  80 }