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.
18 package gov.nasa.jpf.vm;
20 import gov.nasa.jpf.Config;
21 import gov.nasa.jpf.JPF;
22 import gov.nasa.jpf.JPFException;
23 import gov.nasa.jpf.util.JPFLogger;
24 import gov.nasa.jpf.util.LocationSpec;
25 import gov.nasa.jpf.vm.bytecode.ReturnInstruction;
26 import java.lang.reflect.Modifier;
27 import java.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
33 * information associated with a method. Each method in JPF
34 * is represented by a MethodInfo object
36 public class MethodInfo extends InfoObject implements GenericSignatureHolder {
38 static JPFLogger logger = JPF.getLogger("gov.nasa.jpf.vm.MethodInfo");
40 static final int INIT_MTH_SIZE = 4096;
41 protected static final ArrayList<MethodInfo> mthTable = new ArrayList<MethodInfo>(INIT_MTH_SIZE);
44 static final int DIRECT_CALL = -1;
46 static final LocalVarInfo[] EMPTY = new LocalVarInfo[0];
48 static final int[] EMPTY_INT = new int[0];
51 * Used to warn about local variable information.
53 protected static boolean warnedLocalInfo = false;
55 //--- various JPF method attributes
56 static final int EXEC_ATOMIC = 0x10000; // method executed atomically
57 static final int EXEC_HIDDEN = 0x20000; // method hidden from path
58 static final int FIREWALL = 0x40000; // firewall any unhandled exceptionHandlers
59 // (turn into UnhandledException throws)
60 static final int IS_CLINIT = 0x80000;
61 static final int IS_INIT = 0x100000;
63 static final int IS_REFLECTION = 0x200000; // this is a reflection direct call
64 static final int IS_DIRECT_CALL = 0x400000;
66 /** a unique int assigned to this method */
67 protected int globalId = -1;
70 * this is a lazy evaluated mangled name consisting of the name and
73 protected String uniqueName;
75 /** Name of the method */
76 protected String name;
78 /** Signature of the method */
79 protected String signature;
81 /** Generic signature of the method */
82 protected String genericSignature;
84 /** Class the method belongs to */
85 protected ClassInfo ci;
87 /** Instructions associated with the method */
88 protected Instruction[] code;
90 /** JPFConfigException handlers */
91 protected ExceptionHandler[] exceptionHandlers;
93 /** classnames of checked exception thrown by the method */
94 protected String[] thrownExceptionClassNames;
96 /** Table used for line numbers
97 * this assigns a line number to every instruction index, instead of
98 * using an array of ranges. Assuming we have 2-3 insns per line on average,
99 * this should still require less memory than a reference array with associated
100 * range objects, and allows faster access of instruction line numbers, which
101 * we might need for location specs
103 protected int[] lineNumbers;
105 /** Local variable information */
106 protected LocalVarInfo localVars[] = null;
108 /** Maximum number of local variables */
109 protected int maxLocals;
111 /** Maximum number of elements on the stack */
112 protected int maxStack;
114 /** null if we don't have any */
115 AnnotationInfo[][] parameterAnnotations;
117 //--- a batch of attributes
119 /** the standard Java modifier attributes */
120 protected int modifiers;
122 /** a batch of execution related JPF attributes */
123 protected int attributes;
126 //--- all the stuff we need for native methods
127 // <2do> pcm - turn this into a derived class
129 /** the number of stack slots for the arguments (incl. 'this'), lazy eval */
130 protected int argSize = -1;
132 /** number of arguments (excl. 'this'), lazy eval */
133 protected int nArgs = -1;
135 /** what return type do we have (again, lazy evaluated) */
136 protected byte returnType = -1;
138 /** number of stack slots for return value */
139 protected int retSize = -1;
141 /** used for native method parameter conversion (lazy evaluated) */
142 protected byte[] argTypes = null;
144 static boolean init (Config config) {
149 public static MethodInfo getMethodInfo (int globalId){
150 if (globalId >=0 && globalId <mthTable.size()){
151 return mthTable.get(globalId);
157 public static MethodInfo create (String name, String signature, int modifiers){
158 return new MethodInfo( name, signature, modifiers);
161 public static MethodInfo create (ClassInfo ci, String name, String signature, int modifiers){
162 return new MethodInfo( ci, name, signature, modifiers);
165 static MethodInfo create (ClassInfo ci, String name, String signature, int modifiers, int maxLocals, int maxStack){
166 return new MethodInfo( ci, name, signature, modifiers, maxLocals, maxStack);
170 * for direct call construction
171 * Note: this is only a partial initialization, the code still has to be created/installed by the caller
173 public MethodInfo (MethodInfo callee, int nLocals, int nOperands) {
174 globalId = DIRECT_CALL;
175 // we don't want direct call methods in the mthTable (would be a memory leak) so don't register
178 name = "[" + callee.name + ']'; // it doesn't allocate anything, so we don't have to be unique
180 genericSignature = "";
182 maxStack = nOperands; // <2do> cache for optimization
185 exceptionHandlers = null;
186 thrownExceptionClassNames = null;
189 // we need to preserve the ClassInfo so that class resolution for static method calls works
192 attributes |= IS_DIRECT_CALL;
193 modifiers = Modifier.STATIC; // always treated as static
195 // code still has to be installed by caller
199 * This is used to create synthetic methods of function object types
201 public MethodInfo(String name, String signature, int modifiers, int nLocals, int nOperands) {
202 this( name, signature, modifiers);
204 maxStack = nOperands;
209 * for NativeMethodInfo creation
211 public MethodInfo (MethodInfo mi) {
212 globalId = mi.globalId;
213 uniqueName = mi.uniqueName;
215 signature = mi.signature;
216 genericSignature = mi.genericSignature;
218 modifiers = mi.modifiers;
219 attributes = mi.attributes;
220 thrownExceptionClassNames = mi.thrownExceptionClassNames;
221 parameterAnnotations = mi.parameterAnnotations;
223 annotations = mi.annotations;
225 localVars = null; // there are no StackFrame localVarInfos, this is native
226 // code still has to be installed by caller
229 // <2do> this is going away
230 public MethodInfo (ClassInfo ci, String name, String signature, int modifiers, int maxLocals, int maxStack){
233 this.signature = signature;
234 this.uniqueName = getUniqueName(name, signature);
235 this.genericSignature = "";
236 this.maxLocals = maxLocals;
237 this.maxStack = maxStack;
238 this.modifiers = modifiers;
240 this.lineNumbers = null;
241 this.exceptionHandlers = null;
242 this.thrownExceptionClassNames = null;
244 // set attributes we can deduce from the name and the ClassInfo
246 if (name.equals("<init>")) {
247 attributes |= IS_INIT;
248 } else if (name.equals("<clinit>")) {
249 this.modifiers |= Modifier.SYNCHRONIZED;
250 attributes |= IS_CLINIT | FIREWALL;
252 if (ci.isInterface()) { // all interface methods are public
253 this.modifiers |= Modifier.PUBLIC;
257 this.globalId = mthTable.size();
262 public MethodInfo (String name, String signature, int modifiers){
264 this.signature = signature;
265 this.modifiers = modifiers;
266 this.uniqueName = getUniqueName(name, signature);
267 this.genericSignature = "";
269 if (name.equals("<init>")) {
270 attributes |= IS_INIT;
271 } else if (name.equals("<clinit>")) {
272 // for some reason clinits don't have the synchronized modifier, but they are synchronized
273 // we keep it consistent so that we don't have to implement special lock acquisition/release for clinits
274 this.modifiers |= Modifier.SYNCHRONIZED;
275 attributes |= IS_CLINIT | FIREWALL;
278 this.globalId = mthTable.size();
282 public MethodInfo (ClassInfo ci, String name, String signature, int modifiers){
283 this(name, signature, modifiers);
288 //--- setters used during construction
290 public void linkToClass (ClassInfo ci){
293 if (ci.isInterface()) { // all interface methods are public
294 this.modifiers |= Modifier.PUBLIC;
298 public void setMaxLocals(int maxLocals){
299 this.maxLocals = maxLocals;
302 public void setMaxStack(int maxStack){
303 this.maxStack = maxStack;
306 public void setCode (Instruction[] code){
307 for (int i=0; i<code.length; i++){
308 code[i].setMethodInfo(this);
314 public boolean hasParameterAnnotations() {
315 return (parameterAnnotations != null);
318 // since some listeners might call this on every method invocation, we should do a little optimization
319 static AnnotationInfo[][] NO_PARAMETER_ANNOTATIONS_0 = new AnnotationInfo[0][];
320 static AnnotationInfo[][] NO_PARAMETER_ANNOTATIONS_1 = { new AnnotationInfo[0] };
321 static AnnotationInfo[][] NO_PARAMETER_ANNOTATIONS_2 = { new AnnotationInfo[0], new AnnotationInfo[0] };
322 static AnnotationInfo[][] NO_PARAMETER_ANNOTATIONS_3 = { new AnnotationInfo[0], new AnnotationInfo[0], new AnnotationInfo[0] };
324 public AnnotationInfo[][] getParameterAnnotations() {
325 if (parameterAnnotations == null){ // keep this similar to getAnnotations()
326 int n = getNumberOfArguments();
328 case 0: return NO_PARAMETER_ANNOTATIONS_0;
329 case 1: return NO_PARAMETER_ANNOTATIONS_1;
330 case 2: return NO_PARAMETER_ANNOTATIONS_2;
331 case 3: return NO_PARAMETER_ANNOTATIONS_3;
333 AnnotationInfo[][] pai = new AnnotationInfo[n][];
334 for (int i=0; i<n; i++){
335 pai[i] = new AnnotationInfo[0];
341 return parameterAnnotations;
346 * return annotations for parameterIndex
348 public AnnotationInfo[] getParameterAnnotations(int parameterIndex){
349 if (parameterAnnotations == null){
352 if (parameterIndex >= getNumberOfArguments()){
355 return parameterAnnotations[parameterIndex];
362 public static int getNumberOfLoadedMethods () {
363 return mthTable.size();
366 void setAtomic (boolean isAtomic) {
368 attributes |= EXEC_ATOMIC;
370 attributes &= ~EXEC_ATOMIC;
373 public boolean isAtomic () {
374 return ((attributes & EXEC_ATOMIC) != 0);
377 void setHidden (boolean isHidden) {
379 attributes |= EXEC_HIDDEN;
381 attributes &= ~EXEC_HIDDEN;
384 public boolean isHidden () {
385 return ((attributes & EXEC_HIDDEN) != 0);
389 * turn unhandled exceptionHandlers at the JPF execution level
390 * into UnhandledException throws at the host VM level
391 * this is useful to implement firewalls for direct calls
392 * which should not let exceptionHandlers permeate into bytecode/
395 public void setFirewall (boolean isFirewalled) {
397 attributes |= FIREWALL;
399 attributes &= ~FIREWALL;
402 public boolean isFirewall () {
403 return ((attributes & FIREWALL) != 0);
409 public Object clone() {
411 return super.clone();
412 } catch (CloneNotSupportedException cnx) {
417 public int getGlobalId() {
421 public DirectCallStackFrame createRunStartStackFrame (ThreadInfo ti){
422 return ci.createRunStartStackFrame( ti, this);
425 public DirectCallStackFrame createDirectCallStackFrame (ThreadInfo ti, int nLocals){
426 return ci.createDirectCallStackFrame(ti, this, nLocals);
429 public boolean isSyncRelevant () {
430 return (name.charAt(0) != '<');
433 public boolean isInitOrClinit (){
434 return ((attributes & (IS_CLINIT | IS_INIT)) != 0);
437 public boolean isClinit () {
438 return ((attributes & IS_CLINIT) != 0);
441 public boolean isClinit (ClassInfo ci) {
442 return (((attributes & IS_CLINIT) != 0) && (this.ci == ci));
445 public boolean isInit() {
446 return ((attributes & IS_INIT) != 0);
449 public boolean isDirectCallStub(){
450 return ((attributes & IS_DIRECT_CALL) != 0);
454 * yet another name - this time with a non-mangled, but abbreviated signature
455 * and without return type (e.g. like "main(String[])"
457 public String getLongName () {
458 StringBuilder sb = new StringBuilder();
462 String[] argTypeNames = getArgumentTypeNames();
463 for (int i=0; i<argTypeNames.length; i++) {
464 String a = argTypeNames[i];
465 int idx = a.lastIndexOf('.');
467 a = a.substring(idx+1);
476 return sb.toString();
480 * return the minimal name that has to be unique for overloading
481 * used as a lookup key
482 * NOTE: with the silent introduction of covariant return types
483 * in Java 5.0, we have to use the full signature to be unique
485 public static String getUniqueName (String mname, String signature) {
486 return (mname + signature);
489 public String getStackTraceSource() {
490 return getSourceFileName();
493 public byte[] getArgumentTypes () {
494 if (argTypes == null) {
495 argTypes = Types.getArgumentTypes(signature);
496 nArgs = argTypes.length;
502 public String[] getArgumentTypeNames () {
503 return Types.getArgumentTypeNames(signature);
506 // TODO: Fix for Groovy's model-checking
507 public String[] getArgumentGenericTypeNames () {
508 // To accommodate methods that do not have generic types
509 if (genericSignature == null || genericSignature.equals("") || genericSignature.contains("<*>"))
510 return getArgumentTypeNames();
511 return Types.getArgumentTypeNames(genericSignature);
514 public int getArgumentsSize () {
516 argSize = Types.getArgumentsSize(signature);
527 * return only the LocalVarInfos for arguments, in order of definition
528 * or null if there are no localVarInfos.
529 * throw a JPFException if there are more immediately in scope vars than args
531 * NOTE - it is perfectly legal for a method to have arguments but no LocalVarInfos,
532 * which are code attributes, clients have to check for a non-null return value
533 * even if the method has arguments.
534 * Note also that abstract / interface methods don't have code and hence no
537 public LocalVarInfo[] getArgumentLocalVars(){
538 if (localVars == null){ // shortcut in case we don't have args or localVars;
542 int nArgs = getNumberOfStackArguments(); // we want 'this'
544 return new LocalVarInfo[0]; // rare enough so that we don't use a static
547 LocalVarInfo[] argLvis = new LocalVarInfo[nArgs];
548 int n = 0; // how many args we've got so far
550 for (LocalVarInfo lvi : localVars){
551 // arguments are the only ones that are immediately in scope
552 if (lvi.getStartPC() == 0){
553 if (n == nArgs){ // ARGH - more in-scope vars than args
554 throw new JPFException("inconsistent localVar table for method " + getFullName());
557 // order with respect to slot index - since this might get called
558 // frequently, we don't use java.util.Arrays.sort() but sort in
559 // on-the-fly. Note that we can have several localVar entries for the
560 // same name, but only one can be immediately in scope
561 int slotIdx = lvi.getSlotIndex();
564 for (i = 0; i < n; i++) {
565 if (slotIdx < argLvis[i].getSlotIndex()) {
566 for (int j=n; j>i; j--){
567 argLvis[j] = argLvis[j-1];
574 if (i == n) { // append
583 public String getReturnType () {
584 return Types.getReturnTypeSignature(signature);
587 public String getReturnTypeName () {
588 return Types.getReturnTypeName(signature);
591 public String getGenericReturnTypeName () {
592 // TODO: We need to double check but for some reason Groovy has a type of generic signature with "<*>"
593 // TODO: in the class file.
594 if (genericSignature == null || genericSignature.equals("") || genericSignature.contains("<*>"))
595 return Types.getReturnTypeName(signature);
596 return Types.getGenericReturnTypeName(genericSignature);
599 public String getSourceFileName () {
601 return ci.getSourceFileName();
607 public String getClassName () {
616 * Returns the class the method belongs to.
618 public ClassInfo getClassInfo () {
623 * @deprecated - use getFullName
626 public String getCompleteName () {
627 return getFullName();
631 * return classname.name (but w/o signature)
633 public String getBaseName() {
634 return getClassName() + '.' + name;
637 public boolean isCtor () {
638 return (name.equals("<init>"));
641 public boolean isInternalMethod () {
642 // <2do> pcm - should turn this into an attribute for efficiency reasons
643 return (name.equals("<clinit>") || uniqueName.equals("finalize()V"));
646 public boolean isThreadEntry (ThreadInfo ti) {
647 return (uniqueName.equals("run()V") && (ti.countStackFrames() == 1));
651 * Returns the full classname (if any) + name + signature.
653 public String getFullName () {
655 return ci.getName() + '.' + getUniqueName();
657 return getUniqueName();
662 * returns stack trace name: classname (if any) + name
664 public String getStackTraceName(){
666 return ci.getName() + '.' + name;
673 * return number of instructions
675 public int getNumberOfInstructions() {
684 * Returns a specific instruction.
686 public Instruction getInstruction (int i) {
691 if ((i < 0) || (i >= code.length)) {
699 * Returns the instruction at a certain position.
701 public Instruction getInstructionAt (int position) {
706 for (int i = 0, l = code.length; i < l; i++) {
707 if ((code[i] != null) && (code[i].getPosition() == position)) {
712 throw new JPFException("instruction not found");
716 * Returns the instructions of the method.
718 public Instruction[] getInstructions () {
722 public boolean includesLine (int line){
723 int len = code.length;
724 return (code[0].getLineNumber() <= line) && (code[len].getLineNumber() >= line);
727 public Instruction[] getInstructionsForLine (int line){
728 return getInstructionsForLineInterval(line,line);
731 public Instruction[] getInstructionsForLineInterval (int l1, int l2){
732 Instruction[] c = code;
734 // instruction line numbers don't have to be monotonic (they can decrease for loops)
735 // hence we cannot easily check for overlapping ranges
738 ArrayList<Instruction> matchingInsns = null;
740 for (int i = 0; i < c.length; i++) {
741 Instruction insn = c[i];
742 int line = insn.getLineNumber();
743 if (line == l1 || line == l2 || (line > l1 && line < l2)) {
744 if (matchingInsns == null) {
745 matchingInsns = new ArrayList<Instruction>();
747 matchingInsns.add(insn);
751 if (matchingInsns == null) {
754 return matchingInsns.toArray(new Instruction[matchingInsns.size()]);
762 public Instruction[] getMatchingInstructions (LocationSpec lspec){
763 return getInstructionsForLineInterval(lspec.getFromLine(), lspec.getToLine());
768 * Returns the line number for a given position.
770 public int getLineNumber (Instruction pc) {
771 if (lineNumbers == null) {
775 return pc.getPosition();
779 int idx = pc.getInstructionIndex();
780 if (idx < 0) idx = 0;
781 return lineNumbers[idx];
788 * Returns a table to translate positions into line numbers.
790 public int[] getLineNumbers () {
794 public boolean containsLineNumber (int n){
795 if (lineNumbers != null){
796 return (lineNumbers[0] <= n) && (lineNumbers[lineNumbers.length-1] <= n);
802 public boolean intersectsLineNumbers( int first, int last){
803 if (lineNumbers != null){
804 if ((last < lineNumbers[0]) || (first > lineNumbers[lineNumbers.length-1])){
813 public ExceptionHandler getHandlerFor (ClassInfo ciException, Instruction insn){
814 if (exceptionHandlers != null){
815 int position = insn.getPosition();
816 for (int i=0; i<exceptionHandlers.length; i++){
817 ExceptionHandler handler = exceptionHandlers[i];
818 if ((position >= handler.getBegin()) && (position < handler.getEnd())) {
819 // checks if this type of exception is caught here (null means 'any')
820 String handledType = handler.getName();
821 if ((handledType == null) // a catch-all handler
822 || ciException.isInstanceOf(handledType)) {
832 public boolean isMJI () {
836 public int getMaxLocals () {
840 public int getMaxStack () {
844 public ExceptionHandler[] getExceptions () {
845 return exceptionHandlers;
848 public String[] getThrownExceptionClassNames () {
849 return thrownExceptionClassNames;
853 public LocalVarInfo getLocalVar(String name, int pc){
854 LocalVarInfo[] vars = localVars;
856 for (int i = 0; i < vars.length; i++) {
857 LocalVarInfo lv = vars[i];
858 if (lv.matches(name, pc)) {
868 public LocalVarInfo getLocalVar (int slotIdx, int pc){
869 LocalVarInfo[] vars = localVars;
872 for (int i = 0; i < vars.length; i++) {
873 LocalVarInfo lv = vars[i];
874 if (lv.matches(slotIdx, pc)) {
883 public LocalVarInfo[] getLocalVars() {
889 * note that this might contain duplicates for variables with multiple
892 public String[] getLocalVariableNames() {
893 String[] names = new String[localVars.length];
895 for (int i=0; i<localVars.length; i++){
896 names[i] = localVars[i].getName();
903 public MethodInfo getOverriddenMethodInfo(){
904 MethodInfo smi = null;
907 ClassInfo sci = ci.getSuperClass();
909 smi = sci.getMethod(getUniqueName(), true);
917 * Returns the name of the method.
919 public String getName () {
923 public String getJNIName () {
924 return Types.getJNIMangledMethodName(null, name, signature);
927 public int getModifiers () {
932 * Returns true if the method is native
934 public boolean isNative () {
935 return ((modifiers & Modifier.NATIVE) != 0);
938 public boolean isAbstract () {
939 return ((modifiers & Modifier.ABSTRACT) != 0);
942 // overridden by NativeMethodInfo
943 public boolean isUnresolvedNativeMethod(){
944 return ((modifiers & Modifier.NATIVE) != 0);
947 // overridden by NativeMethodInfo
948 public boolean isJPFExecutable (){
949 return !hasAttr(NoJPFExec.class);
952 public int getNumberOfArguments () {
954 nArgs = Types.getNumberOfArguments(signature);
961 * Returns the size of the arguments.
962 * This returns the number of parameters passed on the stack, incl. 'this'
964 public int getNumberOfStackArguments () {
965 int n = getNumberOfArguments();
967 return isStatic() ? n : n + 1;
970 public int getNumberOfCallerStackSlots () {
971 return Types.getNumberOfStackSlots(signature, isStatic()); // includes return type
974 public Instruction getFirstInsn(){
981 public Instruction getLastInsn() {
983 return code[code.length-1];
989 * do we return Object references?
991 public boolean isReferenceReturnType () {
992 int r = getReturnTypeCode();
994 return ((r == Types.T_REFERENCE) || (r == Types.T_ARRAY));
997 public byte getReturnTypeCode () {
998 if (returnType < 0) {
999 returnType = Types.getReturnBuiltinType(signature);
1006 * what is the slot size of the return value
1008 public int getReturnSize() {
1010 switch (getReturnTypeCode()) {
1016 case Types.T_DOUBLE:
1029 public Class<? extends ChoiceGenerator<?>> getReturnChoiceGeneratorType (){
1030 switch (getReturnTypeCode()){
1031 case Types.T_BOOLEAN:
1032 return BooleanChoiceGenerator.class;
1038 return IntChoiceGenerator.class;
1041 return LongChoiceGenerator.class;
1044 return FloatChoiceGenerator.class;
1046 case Types.T_DOUBLE:
1047 return DoubleChoiceGenerator.class;
1050 case Types.T_REFERENCE:
1052 return ReferenceChoiceGenerator.class;
1059 * Returns the signature of the method.
1061 public String getSignature () {
1066 public String getGenericSignature() {
1067 return genericSignature;
1071 public void setGenericSignature(String sig){
1072 genericSignature = sig;
1076 * Returns true if the method is static.
1078 public boolean isStatic () {
1079 return ((modifiers & Modifier.STATIC) != 0);
1083 * is this a public method
1085 public boolean isPublic() {
1086 return ((modifiers & Modifier.PUBLIC) != 0);
1089 public boolean isPrivate() {
1090 return ((modifiers & Modifier.PRIVATE) != 0);
1093 public boolean isProtected() {
1094 return ((modifiers & Modifier.PROTECTED) != 0);
1098 * Returns true if the method is synchronized.
1100 public boolean isSynchronized () {
1101 return ((modifiers & Modifier.SYNCHRONIZED) != 0);
1104 // <2do> these modifiers are still java.lang.reflect internal and not
1105 // supported by public Modifier methods, but since we want to keep this
1106 // similar to the Method reflection and we get the modifiers from the
1107 // classfile we implement this with explicit values
1109 public boolean isSynthetic(){
1110 return ((modifiers & 0x00001000) != 0);
1112 public boolean isVarargs(){
1113 return ((modifiers & 0x00000080) != 0);
1117 * is this from a classfile or was it created by JPF (and hence should not
1118 * be visible in stacktraces etc)
1120 public boolean isJPFInternal(){
1121 // note this has a different meaning than Method.isSynthetic(), which
1122 // is defined in VM spec 4.7.8. What we mean here is that this MethodInfo
1123 // is not associated with any class (such as direct call MethodInfos), but
1124 // there might be more in the future
1125 return (ci == null);
1128 public String getUniqueName () {
1132 public boolean hasCode(){
1133 return (code != null);
1136 public boolean hasEmptyBody (){
1137 // only instruction is a return
1138 return (code.length == 1 && (code[0] instanceof ReturnInstruction));
1142 //--- parameter annotations
1143 //<2do> these are going away
1144 protected void startParameterAnnotations(int annotationCount){
1145 parameterAnnotations = new AnnotationInfo[annotationCount][];
1147 protected void setParameterAnnotations(int index, AnnotationInfo[] ai){
1148 parameterAnnotations[index] = ai;
1150 protected void finishParameterAnnotations(){
1154 public void setParameterAnnotations (AnnotationInfo[][] parameterAnnotations){
1155 this.parameterAnnotations = parameterAnnotations;
1158 //--- thrown exceptions
1159 //<2do> these are going away
1160 protected void startTrownExceptions (int exceptionCount){
1161 thrownExceptionClassNames = new String[exceptionCount];
1163 protected void setException (int index, String exceptionType){
1164 thrownExceptionClassNames[index] = Types.getClassNameFromTypeName(exceptionType);
1166 protected void finishThrownExceptions(){
1170 public void setThrownExceptions (String[] exceptions){
1171 thrownExceptionClassNames = exceptions;
1175 //--- exception handler table initialization
1176 //<2do> these are going away
1177 protected void startExceptionHandlerTable (int handlerCount){
1178 exceptionHandlers = new ExceptionHandler[handlerCount];
1180 protected void setExceptionHandler (int index, int startPc, int endPc, int handlerPc, String catchType){
1181 exceptionHandlers[index] = new ExceptionHandler(catchType, startPc, endPc, handlerPc);
1183 protected void finishExceptionHandlerTable(){
1187 public void setExceptionHandlers (ExceptionHandler[] handlers){
1188 exceptionHandlers = handlers;
1191 //--- local var table initialization
1192 // <2do> these are going away
1193 protected void startLocalVarTable (int localVarCount){
1194 localVars = new LocalVarInfo[localVarCount];
1196 protected void setLocalVar(int index, String varName, String descriptor, int scopeStartPc, int scopeEndPc, int slotIndex){
1197 localVars[index] = new LocalVarInfo(varName, descriptor, "", scopeStartPc, scopeEndPc, slotIndex);
1199 protected void finishLocalVarTable(){
1203 public void setLocalVarTable (LocalVarInfo[] locals){
1207 public void setLocalVarAnnotations (){
1208 if (localVars != null){
1209 for (VariableAnnotationInfo ai : getTargetTypeAnnotations(VariableAnnotationInfo.class)){
1210 for (int i = 0; i < ai.getNumberOfScopeEntries(); i++) {
1211 for (LocalVarInfo lv : localVars) {
1212 if (lv.getStartPC() == ai.getStartPC(i) && lv.getSlotIndex() == ai.getSlotIndex(i)) {
1213 lv.addTypeAnnotation(ai);
1221 public boolean hasTypeAnnotatedLocalVars (){
1222 if (localVars != null){
1223 for (LocalVarInfo lv : localVars){
1224 if (lv.hasTypeAnnotations()){
1233 public List<LocalVarInfo> getTypeAnnotatedLocalVars (){
1234 List<LocalVarInfo> list = null;
1236 if (localVars != null){
1237 for (LocalVarInfo lv : localVars){
1238 if (lv.hasTypeAnnotations()){
1240 list = new ArrayList<LocalVarInfo>();
1248 list = Collections.emptyList();
1254 public List<LocalVarInfo> getTypeAnnotatedLocalVars (String annotationClsName){
1255 List<LocalVarInfo> list = null;
1257 if (localVars != null){
1258 for (LocalVarInfo lv : localVars){
1259 AbstractTypeAnnotationInfo tai = lv.getTypeAnnotation(annotationClsName);
1262 list = new ArrayList<LocalVarInfo>();
1270 list = Collections.emptyList();
1277 //--- line number table initialization
1278 // <2do> these are going away
1279 protected void startLineNumberTable(int lineNumberCount){
1280 int len = code.length;
1281 int[] ln = new int[len];
1285 protected void setLineNumber(int index, int lineNumber, int startPc){
1286 int len = code.length;
1287 int[] ln = lineNumbers;
1289 for (int i=0; i<len; i++){
1290 Instruction insn = code[i];
1291 int pc = insn.getPosition();
1293 if (pc == startPc){ // this is the first insn with this line number
1299 protected void finishLineNumberTable (){
1300 int len = code.length;
1301 int[] ln = lineNumbers;
1302 int lastLine = ln[0];
1304 for (int i=1; i<len; i++){
1314 * note - this depends on that we already have a code array
1315 * and that the lines/startPcs are sorted (monotonic increasing)
1317 public void setLineNumbers (int[] lines, int[] startPcs){
1321 int len = code.length;
1322 int[] ln = new int[len];
1324 for (int i=0; i<len; i++){
1325 Instruction insn = code[i];
1326 int pc = insn.getPosition();
1328 if ((j < startPcs.length) && pc == startPcs[j]){
1329 lastLine = lines[j];
1340 * this version takes an already expanded line number array which has to be of
1341 * the same size as the code array
1343 public void setLineNumbers (int[] lines){
1344 if (lines.length != code.length){
1345 throw new JPFException("inconsitent code/line number size");
1347 lineNumbers = lines;
1351 public String toString() {
1352 return "MethodInfo[" + getFullName() + ']';
1355 // for debugging purposes
1357 System.out.println("--- " + this);
1358 for (int i = 0; i < code.length; i++) {
1359 System.out.printf("%2d [%d]: %s\n", i, code[i].getPosition(), code[i].toString());
1364 * Creates a method for a given class, by cloning this MethodInfo
1365 * and all the instructions belong to the method
1367 public MethodInfo getInstanceFor(ClassInfo ci) {
1371 clone = (MethodInfo)super.clone();
1374 clone.globalId = mthTable.size();
1380 clone.code = new Instruction[code.length];
1382 for(int i=0; i<code.length; i++) {
1383 clone.code[i] = code[i].typeSafeClone(clone);
1387 } catch (CloneNotSupportedException cnsx){
1388 cnsx.printStackTrace();