Preconditions.java revision d23b567c2bfb369bd15bbd8a6d3a42386c72364c
1/* 2 * Copyright 2012, Google Inc. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32package org.jf.dexlib2.util; 33 34import org.jf.dexlib2.Format; 35import org.jf.dexlib2.Opcode; 36 37public class Preconditions { 38 public static void checkFormat(Opcode opcode, Format expectedFormat) { 39 if (opcode.format != expectedFormat) { 40 throw new IllegalArgumentException( 41 String.format("Invalid opcode %s for %s", opcode.name, expectedFormat.name())); 42 } 43 } 44 45 public static int checkNibbleRegister(int register) { 46 if ((register & 0xFFFFFFF0) != 0) { 47 throw new IllegalArgumentException( 48 String.format("Invalid register: v%d. Must be between v0 and v15, inclusive.", register)); 49 } 50 return register; 51 } 52 53 public static int checkByteRegister(int register) { 54 if ((register & 0xFFFFFF00) != 0) { 55 throw new IllegalArgumentException( 56 String.format("Invalid register: v%d. Must be between v0 and v255, inclusive.", register)); 57 } 58 return register; 59 } 60 61 public static int checkShortRegister(int register) { 62 if ((register & 0xFFFF0000) != 0) { 63 throw new IllegalArgumentException( 64 String.format("Invalid register: v%d. Must be between v0 and v65535, inclusive.", register)); 65 } 66 return register; 67 } 68 69 public static int checkNibbleLiteral(int literal) { 70 if ((literal & 0xFFFFFFF0) != 0) { 71 throw new IllegalArgumentException( 72 String.format("Invalid literal value: %d. Must be between -8 and 7, inclusive.", literal)); 73 } 74 return literal; 75 } 76 77 public static int checkByteLiteral(int literal) { 78 if ((literal & 0xFFFFFF00) != 0) { 79 throw new IllegalArgumentException( 80 String.format("Invalid literal value: %d. Must be between -128 and 127, inclusive.", literal)); 81 } 82 return literal; 83 } 84 85 public static int checkShortLiteral(int literal) { 86 if ((literal & 0xFFFF0000) != 0) { 87 throw new IllegalArgumentException( 88 String.format("Invalid literal value: %d. Must be between -32768 and 32767, inclusive.", literal)); 89 } 90 return literal; 91 } 92 93 public static int checkIntegerHatLiteral(int literal) { 94 if ((literal & 0xFFFF) != 0) { 95 throw new IllegalArgumentException( 96 String.format("Invalid literal value: %d. Low 16 bits must be zeroed out.", literal)); 97 } 98 return literal; 99 } 100 101 public static long checkLongHatLiteral(long literal) { 102 if ((literal & 0xFFFFFFFFFFFFL) != 0) { 103 throw new IllegalArgumentException( 104 String.format("Invalid literal value: %d. Low 16 bits must be zeroed out.", literal)); 105 } 106 return literal; 107 } 108 109 public static int checkByteCodeOffset(int register) { 110 if ((register & 0xFFFFFF00) != 0) { 111 throw new IllegalArgumentException( 112 String.format("Invalid code offset: %d. Must be between -8 and 7, inclusive.", register)); 113 } 114 return register; 115 } 116 117 public static int checkShortCodeOffset(int register) { 118 if ((register & 0xFFFF0000) != 0) { 119 throw new IllegalArgumentException( 120 String.format("Invalid code offset: %d. Must be between -32768 and 32767, inclusive.", register)); 121 } 122 return register; 123 } 124 125 public static String checkReference(String reference, int referenceType) { 126 //TODO: implement this 127 return reference; 128 } 129 130 public static int check35cRegisterCount(int registerCount) { 131 if (registerCount < 0 || registerCount > 5) { 132 throw new IllegalArgumentException( 133 String.format("Invalid register count: %d. Must be between 0 and 5, inclusive.", registerCount)); 134 } 135 return registerCount; 136 } 137 138 public static int check3rcRegisterCount(int registerCount) { 139 if ((registerCount & 0xFFFFFF00) == 0) { 140 throw new IllegalArgumentException( 141 String.format("Invalid register count: %d. Must be between 0 and 255, inclusive.", registerCount)); 142 } 143 return registerCount; 144 } 145 146 public static void checkValueArg(int valueArg, int maxValue) { 147 if (valueArg > maxValue) { 148 throw new IllegalArgumentException( 149 String.format("Invalid value_arg value %d for an encoded_value. Expecting 0..%d, inclusive", 150 valueArg, maxValue)); 151 } 152 } 153} 154