19 * limitations under the License. |
19 * limitations under the License. |
20 */ |
20 */ |
21 |
21 |
22 package com.sun.org.apache.bcel.internal.generic; |
22 package com.sun.org.apache.bcel.internal.generic; |
23 |
23 |
24 import com.sun.org.apache.bcel.internal.ExceptionConstants; |
24 import com.sun.org.apache.bcel.internal.ExceptionConst; |
|
25 |
25 /** |
26 /** |
26 * CHECKCAST - Check whether object is of given type |
27 * CHECKCAST - Check whether object is of given type |
27 * <PRE>Stack: ..., objectref -> ..., objectref</PRE> |
28 * <PRE>Stack: ..., objectref -> ..., objectref</PRE> |
28 * |
29 * |
29 * @author <A HREF="mailto:markus.dahm@berlin.de">M. Dahm</A> |
30 * @version $Id: CHECKCAST.java 1747278 2016-06-07 17:28:43Z britter $ |
30 */ |
31 */ |
31 public class CHECKCAST extends CPInstruction |
32 public class CHECKCAST extends CPInstruction implements LoadClass, ExceptionThrower, StackProducer, |
32 implements LoadClass, ExceptionThrower, StackProducer, StackConsumer { |
33 StackConsumer { |
33 /** |
|
34 * Empty constructor needed for the Class.newInstance() statement in |
|
35 * Instruction.readInstruction(). Not to be used otherwise. |
|
36 */ |
|
37 CHECKCAST() {} |
|
38 |
34 |
39 /** Check whether object is of given type |
35 /** |
40 * @param n index to class in constant pool |
36 * Empty constructor needed for the Class.newInstance() statement in |
41 */ |
37 * Instruction.readInstruction(). Not to be used otherwise. |
42 public CHECKCAST(int index) { |
38 */ |
43 super(com.sun.org.apache.bcel.internal.Constants.CHECKCAST, index); |
39 CHECKCAST() { |
44 } |
40 } |
45 |
41 |
46 /** @return exceptions this instruction may cause |
|
47 */ |
|
48 public Class[] getExceptions() { |
|
49 Class[] cs = new Class[1 + ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length]; |
|
50 |
42 |
51 System.arraycopy(ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION, 0, |
43 /** Check whether object is of given type |
52 cs, 0, ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length); |
44 * @param index index to class in constant pool |
53 cs[ExceptionConstants.EXCS_CLASS_AND_INTERFACE_RESOLUTION.length] = |
45 */ |
54 ExceptionConstants.CLASS_CAST_EXCEPTION; |
46 public CHECKCAST(final int index) { |
55 return cs; |
47 super(com.sun.org.apache.bcel.internal.Const.CHECKCAST, index); |
56 } |
48 } |
57 |
49 |
58 public ObjectType getLoadClassType(ConstantPoolGen cpg) { |
|
59 Type t = getType(cpg); |
|
60 |
50 |
61 if(t instanceof ArrayType) |
51 /** @return exceptions this instruction may cause |
62 t = ((ArrayType) t).getBasicType(); |
52 */ |
|
53 @Override |
|
54 public Class<?>[] getExceptions() { |
|
55 return ExceptionConst.createExceptions(ExceptionConst.EXCS.EXCS_CLASS_AND_INTERFACE_RESOLUTION, |
|
56 ExceptionConst.CLASS_CAST_EXCEPTION); |
|
57 } |
63 |
58 |
64 return (t instanceof ObjectType)? (ObjectType) t : null; |
|
65 } |
|
66 |
59 |
67 /** |
60 @Override |
68 * Call corresponding visitor method(s). The order is: |
61 public ObjectType getLoadClassType( final ConstantPoolGen cpg ) { |
69 * Call visitor methods of implemented interfaces first, then |
62 Type t = getType(cpg); |
70 * call methods according to the class hierarchy in descending order, |
63 if (t instanceof ArrayType) { |
71 * i.e., the most specific visitXXX() call comes last. |
64 t = ((ArrayType) t).getBasicType(); |
72 * |
65 } |
73 * @param v Visitor object |
66 return (t instanceof ObjectType) ? (ObjectType) t : null; |
74 */ |
67 } |
75 public void accept(Visitor v) { |
68 |
76 v.visitLoadClass(this); |
69 |
77 v.visitExceptionThrower(this); |
70 /** |
78 v.visitStackProducer(this); |
71 * Call corresponding visitor method(s). The order is: |
79 v.visitStackConsumer(this); |
72 * Call visitor methods of implemented interfaces first, then |
80 v.visitTypedInstruction(this); |
73 * call methods according to the class hierarchy in descending order, |
81 v.visitCPInstruction(this); |
74 * i.e., the most specific visitXXX() call comes last. |
82 v.visitCHECKCAST(this); |
75 * |
83 } |
76 * @param v Visitor object |
|
77 */ |
|
78 @Override |
|
79 public void accept( final Visitor v ) { |
|
80 v.visitLoadClass(this); |
|
81 v.visitExceptionThrower(this); |
|
82 v.visitStackProducer(this); |
|
83 v.visitStackConsumer(this); |
|
84 v.visitTypedInstruction(this); |
|
85 v.visitCPInstruction(this); |
|
86 v.visitCHECKCAST(this); |
|
87 } |
84 } |
88 } |