2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.test.vm.reflection;
21 import gov.nasa.jpf.util.test.TestJPF;
23 import java.lang.annotation.Annotation;
24 import java.lang.annotation.Retention;
25 import java.lang.annotation.RetentionPolicy;
26 import java.lang.reflect.Constructor;
28 import org.junit.Test;
31 * regression test for constructor reflection
33 public class ConstructorTest extends TestJPF {
35 @Retention(RetentionPolicy.RUNTIME)
41 @A("this is a superclass ctor annotation")
45 static class X extends Y {
48 @A("this is a ctor annotation")
49 public X (@A("this is a parameter annotation") String x) {
51 System.out.println(x);
56 public void testConstructorCall() {
57 if (verifyNoPropertyViolation()){
59 Class<X> cls = X.class;
60 Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
62 X x = ctor.newInstance("I'm an X");
65 } catch (Throwable t){
66 fail("ctor invocation failed: " + t);
80 public void testConstructorCallInteger() {
81 if (verifyNoPropertyViolation()) {
83 Class<I> cls = I.class;
84 Constructor<I> ctor = cls.getDeclaredConstructor(new Class<?>[] {Integer.class });
86 I obj = ctor.newInstance(42);
88 assertEquals(new Integer(42), obj.i);
89 } catch (Throwable t) {
90 fail("ctor invocation with Integer failed: " + t);
98 public void testAnnotations(){
99 if (verifyNoPropertyViolation()) {
101 Class<X> cls = X.class;
102 Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
104 Annotation[] ai = ctor.getDeclaredAnnotations();
105 assertTrue("no declared ctor annotations found", ai.length == 1);
107 assertTrue("wrong ctor annotation type", ai[0] instanceof A);
108 System.out.printf("ctor annotation: " + ai[0]);
110 } catch (Throwable t) {
111 fail("ctor.getDeclaredAnnotations() failed: " + t);
117 public void testParameterAnnotations(){
118 if (verifyNoPropertyViolation()) {
120 Class<X> cls = X.class;
121 Constructor<X> ctor = cls.getDeclaredConstructor(new Class<?>[] { String.class });
123 Annotation[][] pai = ctor.getParameterAnnotations();
124 assertTrue("no ctor parameter annotations found", pai.length == 1);
126 Annotation[] ai = pai[0];
127 assertTrue("wrong number of annotations for first ctor argument", ai.length == 1);
129 assertTrue("wrong parameter annotation type", ai[0] instanceof A);
130 System.out.printf("ctor parameter annotation: " + ai[0]);
132 } catch (Throwable t) {
133 fail("ctor.getParameterAnnotations() failed: " + t);