Java Code Examples for org.jf.util.ExceptionWithContext

The following examples show how to use org.jf.util.ExceptionWithContext. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: ZjDroid   Source File: DexBackedDexFile.java    License: Apache License 2.0 6 votes vote down vote up
private static void verifyMagicAndByteOrder(@Nonnull byte[] buf, int offset) {
    if (!HeaderItem.verifyMagic(buf, offset)) {
        StringBuilder sb = new StringBuilder("Invalid magic value:");
        for (int i=0; i<8; i++) {
            sb.append(String.format(" %02x", buf[i]));
        }
        throw new NotADexFile(sb.toString());
    }

    int endian = HeaderItem.getEndian(buf, offset);
    if (endian == HeaderItem.BIG_ENDIAN_TAG) {
        throw new ExceptionWithContext("Big endian dex files are not currently supported");
    }

    if (endian != HeaderItem.LITTLE_ENDIAN_TAG) {
        throw new ExceptionWithContext("Invalid endian tag: 0x%x", endian);
    }
}
 
Example 2
Source Project: ZjDroid   Source File: ImmutableEncodedValueFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static EncodedValue defaultValueForType(String type) {
    switch (type.charAt(0)) {
        case 'Z':
            return ImmutableBooleanEncodedValue.FALSE_VALUE;
        case 'B':
            return new ImmutableByteEncodedValue((byte)0);
        case 'S':
            return new ImmutableShortEncodedValue((short)0);
        case 'C':
            return new ImmutableCharEncodedValue((char)0);
        case 'I':
            return new ImmutableIntEncodedValue(0);
        case 'J':
            return new ImmutableLongEncodedValue(0);
        case 'F':
            return new ImmutableFloatEncodedValue(0);
        case 'D':
            return new ImmutableDoubleEncodedValue(0);
        case 'L':
        case '[':
            return ImmutableNullEncodedValue.INSTANCE;
        default:
            throw new ExceptionWithContext("Unrecognized type: %s", type);
    }
}
 
Example 3
Source Project: ZjDroid   Source File: ImmutableReferenceFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ImmutableReference of(Reference reference) {
    if (reference instanceof StringReference) {
        return ImmutableStringReference.of((StringReference)reference);
    }
    if (reference instanceof TypeReference) {
        return ImmutableTypeReference.of((TypeReference)reference);
    }
    if (reference instanceof FieldReference) {
        return ImmutableFieldReference.of((FieldReference)reference);
    }
    if (reference instanceof MethodReference) {
        return ImmutableMethodReference.of((MethodReference)reference);
    }
    throw new ExceptionWithContext("Invalid reference type");
}
 
Example 4
Source Project: HeyGirl   Source File: ImmutableReferenceFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ImmutableReference of(Reference reference) {
    if (reference instanceof StringReference) {
        return ImmutableStringReference.of((StringReference)reference);
    }
    if (reference instanceof TypeReference) {
        return ImmutableTypeReference.of((TypeReference)reference);
    }
    if (reference instanceof FieldReference) {
        return ImmutableFieldReference.of((FieldReference)reference);
    }
    if (reference instanceof MethodReference) {
        return ImmutableMethodReference.of((MethodReference)reference);
    }
    throw new ExceptionWithContext("Invalid reference type");
}
 
Example 5
Source Project: HeyGirl   Source File: ImmutableDebugItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ImmutableDebugItem of(DebugItem debugItem) {
    if (debugItem instanceof ImmutableDebugItem) {
        return (ImmutableDebugItem)debugItem;
    }
    switch (debugItem.getDebugItemType()) {
        case DebugItemType.START_LOCAL:
            return ImmutableStartLocal.of((StartLocal)debugItem);
        case DebugItemType.END_LOCAL:
            return ImmutableEndLocal.of((EndLocal)debugItem);
        case DebugItemType.RESTART_LOCAL:
            return ImmutableRestartLocal.of((RestartLocal)debugItem);
        case DebugItemType.PROLOGUE_END:
            return ImmutablePrologueEnd.of((PrologueEnd)debugItem);
        case DebugItemType.EPILOGUE_BEGIN:
            return ImmutableEpilogueBegin.of((EpilogueBegin)debugItem);
        case DebugItemType.SET_SOURCE_FILE:
            return ImmutableSetSourceFile.of((SetSourceFile)debugItem);
        case DebugItemType.LINE_NUMBER:
            return ImmutableLineNumber.of((LineNumber)debugItem);
        default:
            throw new ExceptionWithContext("Invalid debug item type: %d", debugItem.getDebugItemType());
    }
}
 
Example 6
Source Project: HeyGirl   Source File: RegisterType.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static RegisterType getRegisterType(@Nonnull ClassPath classPath, @Nonnull CharSequence type) {
    switch (type.charAt(0)) {
        case 'Z':
            return BOOLEAN_TYPE;
        case 'B':
            return BYTE_TYPE;
        case 'S':
            return SHORT_TYPE;
        case 'C':
            return CHAR_TYPE;
        case 'I':
            return INTEGER_TYPE;
        case 'F':
            return FLOAT_TYPE;
        case 'J':
            return LONG_LO_TYPE;
        case 'D':
            return DOUBLE_LO_TYPE;
        case 'L':
        case '[':
            return getRegisterType(REFERENCE, classPath.getClass(type));
        default:
            throw new ExceptionWithContext("Invalid type: " + type);
    }
}
 
Example 7
Source Project: HeyGirl   Source File: DexBackedDexFile.java    License: Apache License 2.0 6 votes vote down vote up
private static void verifyMagicAndByteOrder(@Nonnull byte[] buf, int offset) {
    if (!HeaderItem.verifyMagic(buf, offset)) {
        StringBuilder sb = new StringBuilder("Invalid magic value:");
        for (int i=0; i<8; i++) {
            sb.append(String.format(" %02x", buf[i]));
        }
        throw new NotADexFile(sb.toString());
    }

    int endian = HeaderItem.getEndian(buf, offset);
    if (endian == HeaderItem.BIG_ENDIAN_TAG) {
        throw new ExceptionWithContext("Big endian dex files are not currently supported");
    }

    if (endian != HeaderItem.LITTLE_ENDIAN_TAG) {
        throw new ExceptionWithContext("Invalid endian tag: 0x%x", endian);
    }
}
 
Example 8
Source Project: ZjDroid   Source File: RegisterType.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static RegisterType getRegisterType(@Nonnull ClassPath classPath, @Nonnull CharSequence type) {
    switch (type.charAt(0)) {
        case 'Z':
            return BOOLEAN_TYPE;
        case 'B':
            return BYTE_TYPE;
        case 'S':
            return SHORT_TYPE;
        case 'C':
            return CHAR_TYPE;
        case 'I':
            return INTEGER_TYPE;
        case 'F':
            return FLOAT_TYPE;
        case 'J':
            return LONG_LO_TYPE;
        case 'D':
            return DOUBLE_LO_TYPE;
        case 'L':
        case '[':
            return getRegisterType(REFERENCE, classPath.getClass(type));
        default:
            throw new ExceptionWithContext("Invalid type: " + type);
    }
}
 
Example 9
Source Project: ZjDroid   Source File: DexBackedDexFile.java    License: Apache License 2.0 6 votes vote down vote up
private static void verifyMagicAndByteOrder(@Nonnull byte[] buf, int offset) {
    if (!HeaderItem.verifyMagic(buf, offset)) {
        StringBuilder sb = new StringBuilder("Invalid magic value:");
        for (int i=0; i<8; i++) {
            sb.append(String.format(" %02x", buf[i]));
        }
        throw new NotADexFile(sb.toString());
    }

    int endian = HeaderItem.getEndian(buf, offset);
    if (endian == HeaderItem.BIG_ENDIAN_TAG) {
        throw new ExceptionWithContext("Big endian dex files are not currently supported");
    }

    if (endian != HeaderItem.LITTLE_ENDIAN_TAG) {
        throw new ExceptionWithContext("Invalid endian tag: 0x%x", endian);
    }
}
 
Example 10
Source Project: ZjDroid   Source File: DebugMethodItem.java    License: Apache License 2.0 6 votes vote down vote up
public static DebugMethodItem build(RegisterFormatter registerFormatter, DebugItem debugItem) {
    int codeAddress = debugItem.getCodeAddress();
    switch (debugItem.getDebugItemType()) {
        case DebugItemType.START_LOCAL:
            return new StartLocalMethodItem(codeAddress, -1, registerFormatter, (StartLocal)debugItem);
        case DebugItemType.END_LOCAL:
            return new EndLocalMethodItem(codeAddress, -1, registerFormatter, (EndLocal)debugItem);
        case DebugItemType.RESTART_LOCAL:
            return new RestartLocalMethodItem(codeAddress, -1, registerFormatter, (RestartLocal)debugItem);
        case DebugItemType.EPILOGUE_BEGIN:
            return new BeginEpilogueMethodItem(codeAddress, -4);
        case DebugItemType.PROLOGUE_END:
            return new EndPrologueMethodItem(codeAddress, -4);
        case DebugItemType.SET_SOURCE_FILE:
            return new SetSourceFileMethodItem(codeAddress, -3, (SetSourceFile)debugItem);
        case DebugItemType.LINE_NUMBER:
            return new LineNumberMethodItem(codeAddress, -2, (LineNumber)debugItem);
        default:
            throw new ExceptionWithContext("Invalid debug item type: %d", debugItem.getDebugItemType());
    }
}
 
Example 11
Source Project: atlas   Source File: DebugMethodItem.java    License: Apache License 2.0 6 votes vote down vote up
public static DebugMethodItem build(RegisterFormatter registerFormatter, DebugItem debugItem) {
    int codeAddress = debugItem.getCodeAddress();
    switch (debugItem.getDebugItemType()) {
        case DebugItemType.START_LOCAL:
            return new StartLocalMethodItem(codeAddress, -1, registerFormatter, (StartLocal)debugItem);
        case DebugItemType.END_LOCAL:
            return new EndLocalMethodItem(codeAddress, -1, registerFormatter, (EndLocal)debugItem);
        case DebugItemType.RESTART_LOCAL:
            return new RestartLocalMethodItem(codeAddress, -1, registerFormatter, (RestartLocal)debugItem);
        case DebugItemType.EPILOGUE_BEGIN:
            return new BeginEpilogueMethodItem(codeAddress, -4);
        case DebugItemType.PROLOGUE_END:
            return new EndPrologueMethodItem(codeAddress, -4);
        case DebugItemType.SET_SOURCE_FILE:
            return new SetSourceFileMethodItem(codeAddress, -3, (SetSourceFile)debugItem);
        case DebugItemType.LINE_NUMBER:
            return new LineNumberMethodItem(codeAddress, -2, (LineNumber)debugItem);
        default:
            throw new ExceptionWithContext("Invalid debug item type: %d", debugItem.getDebugItemType());
    }
}
 
Example 12
Source Project: zjdroid   Source File: ImmutableEncodedValueFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static EncodedValue defaultValueForType(String type) {
    switch (type.charAt(0)) {
        case 'Z':
            return ImmutableBooleanEncodedValue.FALSE_VALUE;
        case 'B':
            return new ImmutableByteEncodedValue((byte)0);
        case 'S':
            return new ImmutableShortEncodedValue((short)0);
        case 'C':
            return new ImmutableCharEncodedValue((char)0);
        case 'I':
            return new ImmutableIntEncodedValue(0);
        case 'J':
            return new ImmutableLongEncodedValue(0);
        case 'F':
            return new ImmutableFloatEncodedValue(0);
        case 'D':
            return new ImmutableDoubleEncodedValue(0);
        case 'L':
        case '[':
            return ImmutableNullEncodedValue.INSTANCE;
        default:
            throw new ExceptionWithContext("Unrecognized type: %s", type);
    }
}
 
Example 13
Source Project: zjdroid   Source File: ImmutableReferenceFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ImmutableReference of(Reference reference) {
    if (reference instanceof StringReference) {
        return ImmutableStringReference.of((StringReference)reference);
    }
    if (reference instanceof TypeReference) {
        return ImmutableTypeReference.of((TypeReference)reference);
    }
    if (reference instanceof FieldReference) {
        return ImmutableFieldReference.of((FieldReference)reference);
    }
    if (reference instanceof MethodReference) {
        return ImmutableMethodReference.of((MethodReference)reference);
    }
    throw new ExceptionWithContext("Invalid reference type");
}
 
Example 14
Source Project: HeyGirl   Source File: ImmutableEncodedValueFactory.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static EncodedValue defaultValueForType(String type) {
    switch (type.charAt(0)) {
        case 'Z':
            return ImmutableBooleanEncodedValue.FALSE_VALUE;
        case 'B':
            return new ImmutableByteEncodedValue((byte)0);
        case 'S':
            return new ImmutableShortEncodedValue((short)0);
        case 'C':
            return new ImmutableCharEncodedValue((char)0);
        case 'I':
            return new ImmutableIntEncodedValue(0);
        case 'J':
            return new ImmutableLongEncodedValue(0);
        case 'F':
            return new ImmutableFloatEncodedValue(0);
        case 'D':
            return new ImmutableDoubleEncodedValue(0);
        case 'L':
        case '[':
            return ImmutableNullEncodedValue.INSTANCE;
        default:
            throw new ExceptionWithContext("Unrecognized type: %s", type);
    }
}
 
Example 15
Source Project: zjdroid   Source File: DebugWriter.java    License: Apache License 2.0 6 votes vote down vote up
public void writeLineNumber(int codeAddress, int lineNumber) throws IOException {
    int lineDelta = lineNumber - currentLine;
    int addressDelta = codeAddress - currentAddress;

    if (addressDelta < 0) {
        throw new ExceptionWithContext("debug info items must have non-decreasing code addresses");
    }
    if (lineDelta < -4 || lineDelta > 10) {
        writeAdvanceLine(lineNumber);
        lineDelta = 0;
    } // no else is intentional here. we might need to advance the PC as well as the line
    if ((lineDelta < 2 && addressDelta > 16) || (lineDelta > 1 && addressDelta > 15)) {
        writeAdvancePC(codeAddress);
        addressDelta = 0;
    }

    // we need to emit the special opcode even if both lineDelta and addressDelta are 0, otherwise a positions
    // entry isn't generated
    writeSpecialOpcode(lineDelta, addressDelta);
}
 
Example 16
Source Project: zjdroid   Source File: BuilderEncodedValues.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static BuilderEncodedValue defaultValueForType(String type) {
    switch (type.charAt(0)) {
        case 'Z':
            return BuilderBooleanEncodedValue.FALSE_VALUE;
        case 'B':
            return new BuilderByteEncodedValue((byte)0);
        case 'S':
            return new BuilderShortEncodedValue((short)0);
        case 'C':
            return new BuilderCharEncodedValue((char)0);
        case 'I':
            return new BuilderIntEncodedValue(0);
        case 'J':
            return new BuilderLongEncodedValue(0);
        case 'F':
            return new BuilderFloatEncodedValue(0);
        case 'D':
            return new BuilderDoubleEncodedValue(0);
        case 'L':
        case '[':
            return BuilderNullEncodedValue.INSTANCE;
        default:
            throw new ExceptionWithContext("Unrecognized type: %s", type);
    }
}
 
Example 17
Source Project: ZjDroid   Source File: ImmutableDebugItem.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static ImmutableDebugItem of(DebugItem debugItem) {
    if (debugItem instanceof ImmutableDebugItem) {
        return (ImmutableDebugItem)debugItem;
    }
    switch (debugItem.getDebugItemType()) {
        case DebugItemType.START_LOCAL:
            return ImmutableStartLocal.of((StartLocal)debugItem);
        case DebugItemType.END_LOCAL:
            return ImmutableEndLocal.of((EndLocal)debugItem);
        case DebugItemType.RESTART_LOCAL:
            return ImmutableRestartLocal.of((RestartLocal)debugItem);
        case DebugItemType.PROLOGUE_END:
            return ImmutablePrologueEnd.of((PrologueEnd)debugItem);
        case DebugItemType.EPILOGUE_BEGIN:
            return ImmutableEpilogueBegin.of((EpilogueBegin)debugItem);
        case DebugItemType.SET_SOURCE_FILE:
            return ImmutableSetSourceFile.of((SetSourceFile)debugItem);
        case DebugItemType.LINE_NUMBER:
            return ImmutableLineNumber.of((LineNumber)debugItem);
        default:
            throw new ExceptionWithContext("Invalid debug item type: %d", debugItem.getDebugItemType());
    }
}
 
Example 18
Source Project: HeyGirl   Source File: RegisterType.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static RegisterType getWideRegisterType(@Nonnull CharSequence type, boolean firstRegister) {
    switch (type.charAt(0)) {
        case 'J':
            if (firstRegister) {
                return getRegisterType(LONG_LO, null);
            } else {
                return getRegisterType(LONG_HI, null);
            }
        case 'D':
            if (firstRegister) {
                return getRegisterType(DOUBLE_LO, null);
            } else {
                return getRegisterType(DOUBLE_HI, null);
            }
        default:
            throw new ExceptionWithContext("Cannot use this method for narrow register type: %s", type);
    }
}
 
Example 19
Source Project: zjdroid   Source File: RegisterType.java    License: Apache License 2.0 6 votes vote down vote up
@Nonnull
public static RegisterType getRegisterType(@Nonnull ClassPath classPath, @Nonnull CharSequence type) {
    switch (type.charAt(0)) {
        case 'Z':
            return BOOLEAN_TYPE;
        case 'B':
            return BYTE_TYPE;
        case 'S':
            return SHORT_TYPE;
        case 'C':
            return CHAR_TYPE;
        case 'I':
            return INTEGER_TYPE;
        case 'F':
            return FLOAT_TYPE;
        case 'J':
            return LONG_LO_TYPE;
        case 'D':
            return DOUBLE_LO_TYPE;
        case 'L':
        case '[':
            return getRegisterType(REFERENCE, classPath.getClass(type));
        default:
            throw new ExceptionWithContext("Invalid type: " + type);
    }
}
 
Example 20
Source Project: HeyGirl   Source File: ArrayProto.java    License: Apache License 2.0 6 votes vote down vote up
public ArrayProto(@Nonnull ClassPath classPath, @Nonnull String type) {
    this.classPath = classPath;
    int i=0;
    while (type.charAt(i) == '[') {
        i++;
        if (i == type.length()) {
            throw new ExceptionWithContext("Invalid array type: %s", type);
        }
    }

    if (i == 0) {
        throw new ExceptionWithContext("Invalid array type: %s", type);
    }

    dimensions = i;
    elementType = type.substring(i);
}
 
Example 21
Source Project: ZjDroid   Source File: ImmutableReferenceFactory.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull
public static ImmutableReference of(int referenceType, Reference reference) {
    switch (referenceType) {
        case ReferenceType.STRING:
            return ImmutableStringReference.of((StringReference)reference);
        case ReferenceType.TYPE:
            return ImmutableTypeReference.of((TypeReference)reference);
        case ReferenceType.FIELD:
            return ImmutableFieldReference.of((FieldReference)reference);
        case ReferenceType.METHOD:
            return ImmutableMethodReference.of((MethodReference)reference);
    }
    throw new ExceptionWithContext("Invalid reference type: %d", referenceType);
}
 
Example 22
Source Project: ZjDroid   Source File: VerificationError.java    License: Apache License 2.0 5 votes vote down vote up
public static int getVerificationError(String verificationError) {
    Integer ret = verificationErrorNames.get(verificationError);
    if (ret == null) {
        throw new ExceptionWithContext("Invalid verification error: %s", verificationError);
    }
    return ret;
}
 
Example 23
Source Project: ZjDroid   Source File: BaseOffsetPool.java    License: Apache License 2.0 5 votes vote down vote up
@Override public int getItemOffset(@Nonnull Key key) {
    Integer offset = internedItems.get(key);
    if (offset == null) {
        throw new ExceptionWithContext("Item not found.: %s", getItemString(key));
    }
    return offset;
}
 
Example 24
Source Project: HeyGirl   Source File: DexDataWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void writeShort(int value) throws IOException {
    if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
        throw new ExceptionWithContext("Short value out of range: %d", value);
    }
    write(value);
    write(value >> 8);
}
 
Example 25
Source Project: ZjDroid   Source File: StringTypeBasePool.java    License: Apache License 2.0 5 votes vote down vote up
@Override public int getItemIndex(@Nonnull CharSequence key) {
    Integer index = internedItems.get(key.toString());
    if (index == null) {
        throw new ExceptionWithContext("Item not found.: %s", key.toString());
    }
    return index;
}
 
Example 26
Source Project: ZjDroid   Source File: StringTypeBasePool.java    License: Apache License 2.0 5 votes vote down vote up
@Override public int getItemIndex(@Nonnull CharSequence key) {
    Integer index = internedItems.get(key.toString());
    if (index == null) {
        throw new ExceptionWithContext("Item not found.: %s", key.toString());
    }
    return index;
}
 
Example 27
Source Project: zjdroid   Source File: AnnotationVisibility.java    License: Apache License 2.0 5 votes vote down vote up
public static int getVisibility(String visibility) {
    visibility = visibility.toLowerCase();
    if (visibility.equals("build")) {
        return BUILD;
    }
    if (visibility.equals("runtime")) {
        return RUNTIME;
    }
    if (visibility.equals("system")) {
        return SYSTEM;
    }
    throw new ExceptionWithContext("Invalid annotation visibility: %s", visibility);
}
 
Example 28
Source Project: ZjDroid   Source File: DexDataWriter.java    License: Apache License 2.0 5 votes vote down vote up
public void writeUshort(int value) throws IOException {
    if (value < 0 || value > 0xFFFF) {
        throw new ExceptionWithContext("Unsigned short value out of range: %d", value);
    }
    write(value);
    write(value >> 8);
}
 
Example 29
Source Project: ZjDroid   Source File: BuilderClassPool.java    License: Apache License 2.0 5 votes vote down vote up
@Nonnull BuilderClassDef internClass(@Nonnull BuilderClassDef classDef) {
    BuilderClassDef prev = internedItems.put(classDef.getType(), classDef);
    if (prev != null) {
        throw new ExceptionWithContext("Class %s has already been interned", classDef.getType());
    }
    return classDef;
}
 
Example 30
Source Project: HeyGirl   Source File: DexBackedReference.java    License: Apache License 2.0 5 votes vote down vote up
public static Reference makeReference(@Nonnull DexBackedDexFile dexFile, int referenceType, int referenceIndex) {
    switch (referenceType) {
        case ReferenceType.STRING:
            return new DexBackedStringReference(dexFile, referenceIndex);
        case ReferenceType.TYPE:
            return new DexBackedTypeReference(dexFile, referenceIndex);
        case ReferenceType.METHOD:
            return new DexBackedMethodReference(dexFile, referenceIndex);
        case ReferenceType.FIELD:
            return new DexBackedFieldReference(dexFile, referenceIndex);
        default:
            throw new ExceptionWithContext("Invalid reference type: %d", referenceType);
    }
}