# HG changeset patch # User Peter Mehlitz # Date 1423272535 28800 # Node ID b822e76655857e7eb1965adc2506cb0c6c16217b # Parent 3a19eedcc13ddc871f8d246920697c43d7cc96bc added a @JPFAttribute(TYPE_NAME,...) annotation for model classes (class, field and method target), which causes JPF to automatically set attribute objects that are instantiated from the provided type name args. Note that the respective attribute classes need to have a public default constructor. Added a JPFAttrAnnotationTest to show how to use it. This is the generic mechanism to use if we need to mark ClassInfos, MethodInfos and FieldInfos either from sources (using annotations), or from config files (type names/matchers used from listeners etc.) - base the processing on attributes, and set them from annotations via @JPFAttribute Refactored MethodInfo linking to happen from Initializer.setMethodDone() so that annotations are already parsed (setMethod() is too early since none of the classfile method attributes are parsed at this point) diff -r 3a19eedcc13d -r b822e7665585 src/annotations/gov/nasa/jpf/annotation/JPFAttribute.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/annotations/gov/nasa/jpf/annotation/JPFAttribute.java Fri Feb 06 17:28:55 2015 -0800 @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2015, United States Government, as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All rights reserved. + * + * The Java Pathfinder core (jpf-core) platform is licensed under the + * Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gov.nasa.jpf.annotation; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * a model annotation that causes JPF to automatically set a corresponding + * attribute for the target construct upon class loading + */ +@Retention(RetentionPolicy.RUNTIME) +public @interface JPFAttribute { + String[] value(); +} diff -r 3a19eedcc13d -r b822e7665585 src/main/gov/nasa/jpf/jvm/JVMClassInfo.java --- a/src/main/gov/nasa/jpf/jvm/JVMClassInfo.java Fri Feb 06 10:12:12 2015 -0800 +++ b/src/main/gov/nasa/jpf/jvm/JVMClassInfo.java Fri Feb 06 17:28:55 2015 -0800 @@ -258,15 +258,13 @@ public void setMethod (ClassFile cf, int methodIndex, int accessFlags, String name, String signature) { MethodInfo mi = MethodInfo.create(name, signature, accessFlags); curMi = mi; - - mi.linkToClass(JVMClassInfo.this); - - methods.put(mi.getUniqueName(), mi); } @Override public void setMethodDone (ClassFile cf, int methodIndex){ curMi.setLocalVarAnnotations(); + + JVMClassInfo.this.setMethod(curMi); } @Override diff -r 3a19eedcc13d -r b822e7665585 src/main/gov/nasa/jpf/vm/ClassInfo.java --- a/src/main/gov/nasa/jpf/vm/ClassInfo.java Fri Feb 06 10:12:12 2015 -0800 +++ b/src/main/gov/nasa/jpf/vm/ClassInfo.java Fri Feb 06 17:28:55 2015 -0800 @@ -362,6 +362,8 @@ } else { instanceFields[iInstance++] = fi; } + + processJPFAttrAnnotation(fi); } iFields = instanceFields; @@ -371,19 +373,48 @@ } } - public void setMethods (MethodInfo[] methods) { - if (methods != null && methods.length > 0) { - HashMap map = new LinkedHashMap(); + protected void setMethod (MethodInfo mi){ + mi.linkToClass(this); + methods.put( mi.getUniqueName(), mi); + processJPFAttrAnnotation(mi); + } + + public void setMethods (MethodInfo[] newMethods) { + if (newMethods != null && newMethods.length > 0) { + methods = new LinkedHashMap(); - for (int i = 0; i < methods.length; i++) { - MethodInfo mi = methods[i]; - mi.linkToClass(this); - map.put(mi.getUniqueName(), mi); + for (int i = 0; i < newMethods.length; i++) { + setMethod( newMethods[i]); } - - this.methods = map; } } + + protected void processJPFAttrAnnotation(InfoObject infoObj){ + AnnotationInfo ai = infoObj.getAnnotation("gov.nasa.jpf.annotation.JPFAttribute"); + if (ai != null){ + String[] attrTypes = ai.getValueAsStringArray(); + if (attrTypes != null){ + ClassLoader loader = config.getClassLoader(); + + for (String clsName : attrTypes){ + try { + Class attrCls = loader.loadClass(clsName); + Object attr = attrCls.newInstance(); // needs to have a default ctor + infoObj.addAttr(attr); + + } catch (ClassNotFoundException cnfx){ + logger.warning("attribute class not found: " + clsName); + + } catch (IllegalAccessException iax){ + logger.warning("attribute class has no public default ctor: " + clsName); + + } catch (InstantiationException ix){ + logger.warning("attribute class has no default ctor: " + clsName); + } + } + } + } + } public AnnotationInfo getResolvedAnnotationInfo (String typeName){ return classLoader.getResolvedAnnotationInfo( typeName); @@ -511,6 +542,7 @@ setAssertionStatus(); processJPFConfigAnnotation(); + processJPFAttrAnnotation(this); loadAnnotationListeners(); } diff -r 3a19eedcc13d -r b822e7665585 src/tests/gov/nasa/jpf/test/vm/basic/JPFAttrAnnotationTest.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tests/gov/nasa/jpf/test/vm/basic/JPFAttrAnnotationTest.java Fri Feb 06 17:28:55 2015 -0800 @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2015, United States Government, as represented by the + * Administrator of the National Aeronautics and Space Administration. + * All rights reserved. + * + * The Java Pathfinder core (jpf-core) platform is licensed under the + * Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package gov.nasa.jpf.test.vm.basic; + +import gov.nasa.jpf.ListenerAdapter; +import gov.nasa.jpf.annotation.JPFAttribute; +import gov.nasa.jpf.util.test.TestJPF; +import gov.nasa.jpf.vm.ClassInfo; +import gov.nasa.jpf.vm.FieldInfo; +import gov.nasa.jpf.vm.MethodInfo; +import gov.nasa.jpf.vm.VM; +import org.junit.Test; + +/** + * regression test for JPFAttribute annotations + */ +public class JPFAttrAnnotationTest extends TestJPF { + + static final String LISTENER = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$LoadListener"; + static final String ATTR_CLS = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$MyAttr"; // needs to const + static final String TGT_CLS = "gov.nasa.jpf.test.vm.basic.JPFAttrAnnotationTest$SomeClass"; + + public static class MyAttr {} + + @JPFAttribute(ATTR_CLS) + public static class SomeClass { + + Object data1 = 42; + + @JPFAttribute(ATTR_CLS) + Object data2 = "whatever"; + + public void foo(){} + + @JPFAttribute(ATTR_CLS) + public void bar(){ + System.out.println("SomeClass.bar() executed"); + } + } + + public static class LoadListener extends ListenerAdapter { + + @Override + public void classLoaded (VM vm, ClassInfo ci){ + if (ci.getName().equals(TGT_CLS)){ + System.out.println("#--- checking attribute annotations of " + ci.getName()); + + assertTrue( ci.hasAttr(MyAttr.class)); + System.out.println("# class attr Ok"); + + MethodInfo mi = ci.getMethod("bar()V", false); + assertTrue( mi.hasAttr(MyAttr.class)); + System.out.println("# method bar() attr Ok"); + + mi = ci.getMethod("foo()V", false); + assertFalse( mi.hasAttr(MyAttr.class)); + + FieldInfo fi = ci.getDeclaredInstanceField("data2"); + assertTrue( fi.hasAttr(MyAttr.class)); + System.out.println("# field data2 attr Ok"); + + fi = ci.getDeclaredInstanceField("data1"); + assertFalse( fi.hasAttr(MyAttr.class)); + } + } + } + + @Test + public void testAttrs(){ + if (verifyNoPropertyViolation("+listener=" + LISTENER)){ + SomeClass o = new SomeClass(); + o.bar(); + } + } + +}