Mercurial > hg > Members > kono > jpf-core
comparison src/main/gov/nasa/jpf/vm/ClassInfo.java @ 7:b822e7665585
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)
author | Peter Mehlitz <Peter.C.Mehlitz@nasa.gov> |
---|---|
date | Fri, 06 Feb 2015 17:28:55 -0800 |
parents | 61d41facf527 |
children | e15b03204dc7 |
comparison
equal
deleted
inserted
replaced
6:3a19eedcc13d | 7:b822e7665585 |
---|---|
360 if (fi.isStatic()) { | 360 if (fi.isStatic()) { |
361 staticFields[iStatic++] = fi; | 361 staticFields[iStatic++] = fi; |
362 } else { | 362 } else { |
363 instanceFields[iInstance++] = fi; | 363 instanceFields[iInstance++] = fi; |
364 } | 364 } |
365 | |
366 processJPFAttrAnnotation(fi); | |
365 } | 367 } |
366 | 368 |
367 iFields = instanceFields; | 369 iFields = instanceFields; |
368 sFields = staticFields; | 370 sFields = staticFields; |
369 | 371 |
370 // we can't link the fields yet because we need the superclasses to be resolved | 372 // we can't link the fields yet because we need the superclasses to be resolved |
371 } | 373 } |
372 } | 374 } |
373 | 375 |
374 public void setMethods (MethodInfo[] methods) { | 376 protected void setMethod (MethodInfo mi){ |
375 if (methods != null && methods.length > 0) { | 377 mi.linkToClass(this); |
376 HashMap<String, MethodInfo> map = new LinkedHashMap<String, MethodInfo>(); | 378 methods.put( mi.getUniqueName(), mi); |
377 | 379 processJPFAttrAnnotation(mi); |
378 for (int i = 0; i < methods.length; i++) { | 380 } |
379 MethodInfo mi = methods[i]; | 381 |
380 mi.linkToClass(this); | 382 public void setMethods (MethodInfo[] newMethods) { |
381 map.put(mi.getUniqueName(), mi); | 383 if (newMethods != null && newMethods.length > 0) { |
382 } | 384 methods = new LinkedHashMap<String, MethodInfo>(); |
383 | 385 |
384 this.methods = map; | 386 for (int i = 0; i < newMethods.length; i++) { |
385 } | 387 setMethod( newMethods[i]); |
388 } | |
389 } | |
390 } | |
391 | |
392 protected void processJPFAttrAnnotation(InfoObject infoObj){ | |
393 AnnotationInfo ai = infoObj.getAnnotation("gov.nasa.jpf.annotation.JPFAttribute"); | |
394 if (ai != null){ | |
395 String[] attrTypes = ai.getValueAsStringArray(); | |
396 if (attrTypes != null){ | |
397 ClassLoader loader = config.getClassLoader(); | |
398 | |
399 for (String clsName : attrTypes){ | |
400 try { | |
401 Class<?> attrCls = loader.loadClass(clsName); | |
402 Object attr = attrCls.newInstance(); // needs to have a default ctor | |
403 infoObj.addAttr(attr); | |
404 | |
405 } catch (ClassNotFoundException cnfx){ | |
406 logger.warning("attribute class not found: " + clsName); | |
407 | |
408 } catch (IllegalAccessException iax){ | |
409 logger.warning("attribute class has no public default ctor: " + clsName); | |
410 | |
411 } catch (InstantiationException ix){ | |
412 logger.warning("attribute class has no default ctor: " + clsName); | |
413 } | |
414 } | |
415 } | |
416 } | |
386 } | 417 } |
387 | 418 |
388 public AnnotationInfo getResolvedAnnotationInfo (String typeName){ | 419 public AnnotationInfo getResolvedAnnotationInfo (String typeName){ |
389 return classLoader.getResolvedAnnotationInfo( typeName); | 420 return classLoader.getResolvedAnnotationInfo( typeName); |
390 } | 421 } |
509 | 540 |
510 linkFields(); // computes field offsets | 541 linkFields(); // computes field offsets |
511 | 542 |
512 setAssertionStatus(); | 543 setAssertionStatus(); |
513 processJPFConfigAnnotation(); | 544 processJPFConfigAnnotation(); |
545 processJPFAttrAnnotation(this); | |
514 loadAnnotationListeners(); | 546 loadAnnotationListeners(); |
515 } | 547 } |
516 | 548 |
517 protected ClassInfo(){ | 549 protected ClassInfo(){ |
518 nClassInfos++; | 550 nClassInfos++; |