Source code: com/puppycrawl/tools/checkstyle/bcel/generic/Utils.java
1 //Tested with BCEL-5.1
2 //http://jakarta.apache.org/builds/jakarta-bcel/release/v5.1/
3
4 package com.puppycrawl.tools.checkstyle.bcel.generic;
5
6 import org.apache.bcel.generic.ReferenceType;
7 import org.apache.bcel.generic.Type;
8
9 /**
10 * Utility methods.
11 * @author Rick Giles
12 * @version 17-Jun-2003
13 */
14 public class Utils
15 {
16 /**
17 * Tests whether one type is compatible with another for method
18 * invocation conversion. This includes assignment conversion,
19 * except the implicit narrowing of integer constants.
20 * JLS Section 5.2
21 * @param aSubType the type to be converted.
22 * @param aSuperType the converted type.
23 * @return true if aSubType can be converted to aSuperType.
24 */
25 public static boolean isCompatible(Type aSubType, Type aSuperType)
26 {
27 boolean result = false;
28
29 if (aSubType.equals(aSuperType)) {
30 // identity conversion
31 result = true;
32 }
33 else if ((aSubType instanceof ReferenceType)
34 && (aSuperType instanceof ReferenceType))
35 {
36 // widening reference conversion?
37 final ReferenceType aSubRefType = (ReferenceType) aSubType;
38 result = aSubRefType.isAssignmentCompatibleWith(aSuperType);
39 }
40 // widening primitive conversion?
41 else if (aSubType.equals(Type.BYTE)) {
42 result =
43 aSuperType.equals(Type.SHORT)
44 || aSuperType.equals(Type.INT)
45 || aSuperType.equals(Type.LONG)
46 || aSuperType.equals(Type.FLOAT)
47 || aSuperType.equals(Type.DOUBLE);
48 }
49 else if (aSubType.equals(Type.SHORT)) {
50 result =
51 aSuperType.equals(Type.INT)
52 || aSuperType.equals(Type.LONG)
53 || aSuperType.equals(Type.FLOAT)
54 || aSuperType.equals(Type.DOUBLE);
55 }
56 else if (aSubType.equals(Type.INT)) {
57 result =
58 aSuperType.equals(Type.LONG)
59 || aSuperType.equals(Type.FLOAT)
60 || aSuperType.equals(Type.DOUBLE);
61 }
62 else if (aSubType.equals(Type.LONG)) {
63 result =
64 aSuperType.equals(Type.FLOAT) || aSuperType.equals(Type.DOUBLE);
65 }
66 else if (aSubType.equals(Type.DOUBLE)) {
67 result = aSuperType.equals(Type.DOUBLE);
68 }
69 return result;
70 }
71 }