1 /*
   2  * Copyright (c) 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.  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.test.lib.Asserts;
  33 import jdk.test.lib.jfr.CommonHelper;
  34 import jdk.test.lib.jfr.VoidFunction;
  35 
  36 /**
  37  * @test
  38  * @summary Test ValueDescriptor.getAnnotations()
  39  * @key jfr
  40  * @requires vm.hasJFR
  41  * @library /test/lib
  42  * @run main/othervm jdk.jfr.api.metadata.valuedescriptor.TestClasses
  43  */
  44 public class TestClasses {
  45 
  46     public static void main(String[] args) throws Throwable {
  47         @SuppressWarnings("rawtypes")
  48         Map<String, Class> valid = new HashMap<>();
  49         valid.put("byte", byte.class);
  50         valid.put("short", short.class);
  51         valid.put("int", int.class);
  52         valid.put("char", char.class);
  53         valid.put("float", float.class);
  54         valid.put("double", double.class);
  55         valid.put("boolean", boolean.class);
  56         valid.put("double", double.class);
  57         valid.put("long", long.class);
  58         valid.put("java.lang.String", String.class);
  59         valid.put("java.lang.Class", Class.class);
  60         valid.put("java.lang.Thread", Thread.class);
  61 
  62         for (String name : valid.keySet()) {
  63             Class<?> t = valid.get(name);
  64             System.out.println(t.getName());
  65             ValueDescriptor d = new ValueDescriptor(t, "dummy");
  66             String typeName = d.getTypeName() + (d.isArray() ? "[]" : "");
  67             System.out.printf("%s -> typeName %s%n", name, typeName);
  68             Asserts.assertEquals(name, typeName, "Wrong type name");
  69         }
  70 
  71         // Test some illegal classes
  72         verifyIllegalArg(()->{new ValueDescriptor(Float.class, "ids");}, "Arrays of non-primitives should give Exception");
  73         verifyIllegalArg(()->{new ValueDescriptor(Integer[].class, "ids");}, "Arrays of non-primitives should give Exception");
  74         verifyIllegalArg(()->{new ValueDescriptor(Class[].class, "ids");}, "Arrays of non-primitives should give Exception");
  75         verifyIllegalArg(()->{new ValueDescriptor(MyClass.class, "MyClass");}, "MyClass should give Exception");
  76     }
  77 
  78     private static class MyClass {
  79         @SuppressWarnings("unused")
  80         int id;
  81     }
  82 
  83     private static void verifyIllegalArg(VoidFunction f, String msg) throws Throwable {
  84         CommonHelper.verifyException(f, msg, IllegalArgumentException.class);
  85     }
  86 }