1 /*
   2  * Copyright (c) 2018, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.jfr.api.metadata.valuedescriptor;
  27 
  28 import java.util.HashMap;
  29 import java.util.Map;
  30 
  31 import jdk.jfr.ValueDescriptor;
  32 import jdk.testlibrary.Asserts;
  33 import jdk.testlibrary.jfr.CommonHelper;
  34 import jdk.testlibrary.jfr.VoidFunction;
  35 
  36 /*
  37  * @test
  38  * @summary Test ValueDescriptor.getAnnotations()
  39  * @key jfr
  40  * @library /lib/testlibrary
  41  * @run main/othervm jdk.jfr.api.metadata.valuedescriptor.TestClasses
  42  */
  43 public class TestClasses {
  44 
  45     public static void main(String[] args) throws Throwable {
  46         @SuppressWarnings("rawtypes")
  47         Map<String, Class> valid = new HashMap<>();
  48         valid.put("byte", byte.class);
  49         valid.put("short", short.class);
  50         valid.put("int", int.class);
  51         valid.put("char", char.class);
  52         valid.put("float", float.class);
  53         valid.put("double", double.class);
  54         valid.put("boolean", boolean.class);
  55         valid.put("double", double.class);
  56         valid.put("long", long.class);
  57         valid.put("java.lang.String", String.class);
  58         valid.put("java.lang.Class", Class.class);
  59         valid.put("java.lang.Thread", Thread.class);
  60 
  61         for (String name : valid.keySet()) {
  62             Class<?> t = valid.get(name);
  63             System.out.println(t.getName());
  64             ValueDescriptor d = new ValueDescriptor(t, "dummy");
  65             String typeName = d.getTypeName() + (d.isArray() ? "[]" : "");
  66             System.out.printf("%s -> typeName %s%n", name, typeName);
  67             Asserts.assertEquals(name, typeName, "Wrong type name");
  68         }
  69 
  70         // Test some illegal classes
  71         verifyIllegalArg(()->{new ValueDescriptor(Float.class, "ids");}, "Arrays of non-primitives should give Exception");
  72         verifyIllegalArg(()->{new ValueDescriptor(Integer[].class, "ids");}, "Arrays of non-primitives should give Exception");
  73         verifyIllegalArg(()->{new ValueDescriptor(Class[].class, "ids");}, "Arrays of non-primitives should give Exception");
  74         verifyIllegalArg(()->{new ValueDescriptor(MyClass.class, "MyClass");}, "MyClass should give Exception");
  75     }
  76 
  77     private static class MyClass {
  78         @SuppressWarnings("unused")
  79         int id;
  80     }
  81 
  82     private static void verifyIllegalArg(VoidFunction f, String msg) throws Throwable {
  83         CommonHelper.verifyException(f, msg, IllegalArgumentException.class);
  84     }
  85 }