com.android.dx.cf.iface.ParseException Java Examples

The following examples show how to use com.android.dx.cf.iface.ParseException. 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 File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a parameter annotation attribute.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the parsed list of lists of annotations
 */
public AnnotationsList parseParameterAttribute(
        AnnotationVisibility visibility) {
    AnnotationsList result;

    try {
        result = parseAnnotationsList(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #2
Source File: AnnotationParser.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation attribute, per se.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotations read from the attribute
 * data
 */
public Annotations parseAnnotationAttribute(
        AnnotationVisibility visibility) {
    Annotations result;

    try {
        result = parseAnnotations(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #3
Source File: AnnotationParser.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a parameter annotation attribute.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the parsed list of lists of annotations
 */
public AnnotationsList parseParameterAttribute(
        AnnotationVisibility visibility) {
    AnnotationsList result;

    try {
        result = parseAnnotationsList(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #4
Source File: AnnotationParser.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation value ({@code element_value}) attribute.
 *
 * @return {@code non-null;} the parsed constant value
 */
public Constant parseValueAttribute() {
    Constant result;

    try {
        result = parseValue();

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #5
Source File: Main.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
@Override
public void onException(Exception ex) {
    if (ex instanceof StopProcessing) {
        throw (StopProcessing) ex;
    } else if (ex instanceof SimException) {
        context.err.println("\nEXCEPTION FROM SIMULATION:");
        context.err.println(ex.getMessage() + "\n");
        context.err.println(((SimException) ex).getContext());
    } else if (ex instanceof ParseException) {
        context.err.println("\nPARSE ERROR:");
        ParseException parseException = (ParseException) ex;
        if (args.debug) {
            parseException.printStackTrace(context.err);
        } else {
            parseException.printContext(context.err);
        }
    } else {
        context.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
        ex.printStackTrace(context.err);
    }
    errors.incrementAndGet();
}
 
Example #6
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation attribute, per se.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotations read from the attribute
 * data
 */
public Annotations parseAnnotationAttribute(
        AnnotationVisibility visibility) {
    Annotations result;

    try {
        result = parseAnnotations(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #7
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a parameter annotation attribute.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the parsed list of lists of annotations
 */
public AnnotationsList parseParameterAttribute(
        AnnotationVisibility visibility) {
    AnnotationsList result;

    try {
        result = parseAnnotationsList(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #8
Source File: AnnotationParser.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation value ({@code element_value}) attribute.
 *
 * @return {@code non-null;} the parsed constant value
 */
public Constant parseValueAttribute() {
    Constant result;

    try {
        result = parseValue();

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #9
Source File: Main.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void onException(Exception ex) {
    if (ex instanceof StopProcessing) {
        throw (StopProcessing) ex;
    } else if (ex instanceof SimException) {
        context.err.println("\nEXCEPTION FROM SIMULATION:");
        context.err.println(ex.getMessage() + "\n");
        context.err.println(((SimException) ex).getContext());
    } else if (ex instanceof ParseException) {
        context.err.println("\nPARSE ERROR:");
        ParseException parseException = (ParseException) ex;
        if (args.debug) {
            parseException.printStackTrace(context.err);
        } else {
            parseException.printContext(context.err);
        }
    } else {
        context.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
        ex.printStackTrace(context.err);
    }
    errors.incrementAndGet();
}
 
Example #10
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation attribute, per se.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotations read from the attribute
 * data
 */
public Annotations parseAnnotationAttribute(
        AnnotationVisibility visibility) {
    Annotations result;

    try {
        result = parseAnnotations(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #11
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a parameter annotation attribute.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the parsed list of lists of annotations
 */
public AnnotationsList parseParameterAttribute(
        AnnotationVisibility visibility) {
    AnnotationsList result;

    try {
        result = parseAnnotationsList(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #12
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation value ({@code element_value}) attribute.
 *
 * @return {@code non-null;} the parsed constant value
 */
public Constant parseValueAttribute() {
    Constant result;

    try {
        result = parseValue();

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #13
Source File: Main.java    From Box with Apache License 2.0 6 votes vote down vote up
@Override
public void onException(Exception ex) {
    if (ex instanceof StopProcessing) {
        throw (StopProcessing) ex;
    } else if (ex instanceof SimException) {
        context.err.println("\nEXCEPTION FROM SIMULATION:");
        context.err.println(ex.getMessage() + "\n");
        context.err.println(((SimException) ex).getContext());
    } else if (ex instanceof ParseException) {
        context.err.println("\nPARSE ERROR:");
        ParseException parseException = (ParseException) ex;
        if (args.debug) {
            parseException.printStackTrace(context.err);
        } else {
            parseException.printContext(context.err);
        }
    } else {
        context.err.println("\nUNEXPECTED TOP-LEVEL EXCEPTION:");
        ex.printStackTrace(context.err);
    }
    errors.incrementAndGet();
}
 
Example #14
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation value ({@code element_value}) attribute.
 *
 * @return {@code non-null;} the parsed constant value
 */
public Constant parseValueAttribute() {
    Constant result;

    try {
        result = parseValue();

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #15
Source File: AnnotationParser.java    From Box with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an annotation attribute, per se.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotations read from the attribute
 * data
 */
public Annotations parseAnnotationAttribute(
        AnnotationVisibility visibility) {
    Annotations result;

    try {
        result = parseAnnotations(visibility);

        if (input.available() != 0) {
            throw new ParseException("extra data in attribute");
        }
    } catch (IOException ex) {
        // ByteArray.MyDataInputStream should never throw.
        throw new RuntimeException("shouldn't happen", ex);
    }

    return result;
}
 
Example #16
Source File: Main.java    From Box with Apache License 2.0 5 votes vote down vote up
private ClassDefItem translateClass(byte[] bytes, DirectClassFile cf) {
    try {
        return CfTranslator.translate(context, cf, bytes, args.cfOptions,
                args.dexOptions, outputDex);
    } catch (ParseException ex) {
        context.err.println("\ntrouble processing:");
        if (args.debug) {
            ex.printStackTrace(context.err);
        } else {
            ex.printContext(context.err);
        }
    }
    errors.incrementAndGet();
    return null;
}
 
Example #17
Source File: Main.java    From buck with Apache License 2.0 5 votes vote down vote up
private ClassDefItem translateClass(byte[] bytes, DirectClassFile cf) {
    try {
        return CfTranslator.translate(context, cf, bytes, args.cfOptions,
            args.dexOptions, outputDex);
    } catch (ParseException ex) {
        context.err.println("\ntrouble processing:");
        if (args.debug) {
            ex.printStackTrace(context.err);
        } else {
            ex.printContext(context.err);
        }
    }
    errors.incrementAndGet();
    return null;
}
 
Example #18
Source File: ConstantPoolParser.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);

    at += 3; // Skip to the data.

    ByteArray ubytes = bytes.slice(at, at + length);

    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
 
Example #19
Source File: Dexifier.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addToDexFile(DexFile dexFile, String name, byte[] bytes) {
  DirectClassFile cf = new DirectClassFile(bytes, name, cfOptions.strictNameCheck);
  cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
  cf.getMagic();

  int numMethodIds = dexFile.getMethodIds().items().size();
  int numFieldIds = dexFile.getFieldIds().items().size();
  int constantPoolSize = cf.getConstantPool().size();

  int maxMethodIdsInDex = numMethodIds + constantPoolSize + cf.getMethods().size() +
      MAX_METHOD_ADDED_DURING_DEX_CREATION;
  int maxFieldIdsInDex = numFieldIds + constantPoolSize + cf.getFields().size() +
      MAX_FIELD_ADDED_DURING_DEX_CREATION;

  if ((dexFile.getClassDefs().items().size() > 0)
      && ((maxMethodIdsInDex > MAX_NUMBER_OF_IDX_PER_DEX) ||
          (maxFieldIdsInDex > MAX_NUMBER_OF_IDX_PER_DEX))) {
    throw new RuntimeException("TODO multi dex!");
  }

  try {
    ClassDefItem clazz = CfTranslator.translate(cf, bytes, cfOptions , dexOptions, dexFile);
    synchronized (dexFile) {
      dexFile.add(clazz);
    }
  } catch (ParseException ex) {
    Main.exit("Parse error, " + name, ex);
  }
}
 
Example #20
Source File: Main.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
private ClassDefItem translateClass(byte[] bytes, DirectClassFile cf) {
    try {
        return CfTranslator.translate(context, cf, bytes, args.cfOptions,
                args.dexOptions, outputDex);
    } catch (ParseException ex) {
        context.err.println("\ntrouble processing:");
        if (args.debug) {
            ex.printStackTrace(context.err);
        } else {
            ex.printContext(context.err);
        }
    }
    errors.incrementAndGet();
    return null;
}
 
Example #21
Source File: ConstantPoolParser.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);

    at += 3; // Skip to the data.

    ByteArray ubytes = bytes.slice(at, at + length);

    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
 
Example #22
Source File: Main.java    From Box with Apache License 2.0 5 votes vote down vote up
private ClassDefItem translateClass(byte[] bytes, DirectClassFile cf) {
    try {
        return CfTranslator.translate(context, cf, bytes, args.cfOptions,
                args.dexOptions, outputDex);
    } catch (ParseException ex) {
        context.err.println("\ntrouble processing:");
        if (args.debug) {
            ex.printStackTrace(context.err);
        } else {
            ex.printContext(context.err);
        }
    }
    errors.incrementAndGet();
    return null;
}
 
Example #23
Source File: ConstantPoolParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);

    at += 3; // Skip to the data.

    ByteArray ubytes = bytes.slice(at, at + length);

    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
 
Example #24
Source File: ConstantPoolParser.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Parses a utf8 constant.
 *
 * @param at offset to the start of the constant (where the tag byte is)
 * @return {@code non-null;} the parsed value
 */
private CstString parseUtf8(int at) {
    int length = bytes.getUnsignedShort(at + 1);

    at += 3; // Skip to the data.

    ByteArray ubytes = bytes.slice(at, at + length);

    try {
        return new CstString(ubytes);
    } catch (IllegalArgumentException ex) {
        // Translate the exception
        throw new ParseException(ex);
    }
}
 
Example #25
Source File: ConstantPoolParser.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Populates {@link #offsets} and also completely parse utf8 constants.
 */
private void determineOffsets() {
    int at = 10; // offset from the start of the file to the first cst
    int lastCategory;

    for (int i = 1; i < offsets.length; i += lastCategory) {
        offsets[i] = at;
        int tag = bytes.getUnsignedByte(at);
        try {
            switch (tag) {
                case CONSTANT_Integer:
                case CONSTANT_Float:
                case CONSTANT_Fieldref:
                case CONSTANT_Methodref:
                case CONSTANT_InterfaceMethodref:
                case CONSTANT_NameAndType: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                case CONSTANT_Long:
                case CONSTANT_Double: {
                    lastCategory = 2;
                    at += 9;
                    break;
                }
                case CONSTANT_Class:
                case CONSTANT_String: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_Utf8: {
                    lastCategory = 1;
                    at += bytes.getUnsignedShort(at + 1) + 3;
                    break;
                }
                case CONSTANT_MethodHandle: {
                    lastCategory = 1;
                    at += 4;
                    break;
                }
                case CONSTANT_MethodType: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_InvokeDynamic: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                default: {
                    throw new ParseException("unknown tag byte: " + Hex.u1(tag));
                }
            }
        } catch (ParseException ex) {
            ex.addContext("...while preparsing cst " + Hex.u2(i) + " at offset " + Hex.u4(at));
            throw ex;
        }
    }

    endOffset = at;
}
 
Example #26
Source File: ConstantPoolParser.java    From buck with Apache License 2.0 4 votes vote down vote up
/**
 * Populates {@link #offsets} and also completely parse utf8 constants.
 */
private void determineOffsets() {
    int at = 10; // offset from the start of the file to the first cst
    int lastCategory;

    for (int i = 1; i < offsets.length; i += lastCategory) {
        offsets[i] = at;
        int tag = bytes.getUnsignedByte(at);
        try {
            switch (tag) {
                case CONSTANT_Integer:
                case CONSTANT_Float:
                case CONSTANT_Fieldref:
                case CONSTANT_Methodref:
                case CONSTANT_InterfaceMethodref:
                case CONSTANT_NameAndType: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                case CONSTANT_Long:
                case CONSTANT_Double: {
                    lastCategory = 2;
                    at += 9;
                    break;
                }
                case CONSTANT_Class:
                case CONSTANT_String: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_Utf8: {
                    lastCategory = 1;
                    at += bytes.getUnsignedShort(at + 1) + 3;
                    break;
                }
                case CONSTANT_MethodHandle: {
                    throw new ParseException("MethodHandle not supported");
                }
                case CONSTANT_MethodType: {
                    throw new ParseException("MethodType not supported");
                }
                case CONSTANT_InvokeDynamic: {
                    throw new ParseException("InvokeDynamic not supported");
                }
                default: {
                    throw new ParseException("unknown tag byte: " + Hex.u1(tag));
                }
            }
        } catch (ParseException ex) {
            ex.addContext("...while preparsing cst " + Hex.u2(i) + " at offset " + Hex.u4(at));
            throw ex;
        }
    }

    endOffset = at;
}
 
Example #27
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 4 votes vote down vote up
private BootstrapMethodsList parseBootstrapMethods(ByteArray bytes, ConstantPool constantPool,
        CstType declaringClass, int numMethods, int offset, int length, ParseObserver observer)
    throws ParseException {
    BootstrapMethodsList methods = new BootstrapMethodsList(numMethods);
    for (int methodIndex = 0; methodIndex < numMethods; ++methodIndex) {
        if (length < 4) {
            throwTruncated();
        }

        int methodRef = bytes.getUnsignedShort(offset);
        int numArguments = bytes.getUnsignedShort(offset + 2);

        if (observer != null) {
            observer.parsed(bytes, offset, 2, "bootstrap_method_ref: " + Hex.u2(methodRef));
            observer.parsed(bytes, offset + 2, 2,
                            "num_bootstrap_arguments: " + Hex.u2(numArguments));
        }

        offset += 4;
        length -= 4;
        if (length < numArguments * 2) {
            throwTruncated();
        }

        BootstrapMethodArgumentsList arguments = new BootstrapMethodArgumentsList(numArguments);
        for (int argIndex = 0; argIndex < numArguments; ++argIndex, offset += 2, length -= 2) {
            int argumentRef = bytes.getUnsignedShort(offset);
            if (observer != null) {
                observer.parsed(bytes, offset, 2,
                                "bootstrap_arguments[" + argIndex + "]" + Hex.u2(argumentRef));
            }
            arguments.set(argIndex, constantPool.get(argumentRef));
        }
        arguments.setImmutable();
        Constant cstMethodRef = constantPool.get(methodRef);
        methods.set(methodIndex, declaringClass, (CstMethodHandle) cstMethodRef, arguments);
    }
    methods.setImmutable();

    if (length != 0) {
        throwBadLength(length);
    }

    return methods;
}
 
Example #28
Source File: ConstantPoolParser.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Populates {@link #offsets} and also completely parse utf8 constants.
 */
private void determineOffsets() {
    int at = 10; // offset from the start of the file to the first cst
    int lastCategory;

    for (int i = 1; i < offsets.length; i += lastCategory) {
        offsets[i] = at;
        int tag = bytes.getUnsignedByte(at);
        try {
            switch (tag) {
                case CONSTANT_Integer:
                case CONSTANT_Float:
                case CONSTANT_Fieldref:
                case CONSTANT_Methodref:
                case CONSTANT_InterfaceMethodref:
                case CONSTANT_NameAndType: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                case CONSTANT_Long:
                case CONSTANT_Double: {
                    lastCategory = 2;
                    at += 9;
                    break;
                }
                case CONSTANT_Class:
                case CONSTANT_String: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_Utf8: {
                    lastCategory = 1;
                    at += bytes.getUnsignedShort(at + 1) + 3;
                    break;
                }
                case CONSTANT_MethodHandle: {
                    lastCategory = 1;
                    at += 4;
                    break;
                }
                case CONSTANT_MethodType: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_InvokeDynamic: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                default: {
                    throw new ParseException("unknown tag byte: " + Hex.u1(tag));
                }
            }
        } catch (ParseException ex) {
            ex.addContext("...while preparsing cst " + Hex.u2(i) + " at offset " + Hex.u4(at));
            throw ex;
        }
    }

    endOffset = at;
}
 
Example #29
Source File: ConstantPoolParser.java    From J2ME-Loader with Apache License 2.0 4 votes vote down vote up
/**
 * Populates {@link #offsets} and also completely parse utf8 constants.
 */
private void determineOffsets() {
    int at = 10; // offset from the start of the file to the first cst
    int lastCategory;

    for (int i = 1; i < offsets.length; i += lastCategory) {
        offsets[i] = at;
        int tag = bytes.getUnsignedByte(at);
        try {
            switch (tag) {
                case CONSTANT_Integer:
                case CONSTANT_Float:
                case CONSTANT_Fieldref:
                case CONSTANT_Methodref:
                case CONSTANT_InterfaceMethodref:
                case CONSTANT_NameAndType: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                case CONSTANT_Long:
                case CONSTANT_Double: {
                    lastCategory = 2;
                    at += 9;
                    break;
                }
                case CONSTANT_Class:
                case CONSTANT_String: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_Utf8: {
                    lastCategory = 1;
                    at += bytes.getUnsignedShort(at + 1) + 3;
                    break;
                }
                case CONSTANT_MethodHandle: {
                    lastCategory = 1;
                    at += 4;
                    break;
                }
                case CONSTANT_MethodType: {
                    lastCategory = 1;
                    at += 3;
                    break;
                }
                case CONSTANT_InvokeDynamic: {
                    lastCategory = 1;
                    at += 5;
                    break;
                }
                default: {
                    throw new ParseException("unknown tag byte: " + Hex.u1(tag));
                }
            }
        } catch (ParseException ex) {
            ex.addContext("...while preparsing cst " + Hex.u2(i) + " at offset " + Hex.u4(at));
            throw ex;
        }
    }

    endOffset = at;
}
 
Example #30
Source File: StdAttributeFactory.java    From Box with Apache License 2.0 4 votes vote down vote up
private BootstrapMethodsList parseBootstrapMethods(ByteArray bytes, ConstantPool constantPool,
        CstType declaringClass, int numMethods, int offset, int length, ParseObserver observer)
    throws ParseException {
    BootstrapMethodsList methods = new BootstrapMethodsList(numMethods);
    for (int methodIndex = 0; methodIndex < numMethods; ++methodIndex) {
        if (length < 4) {
            throwTruncated();
        }

        int methodRef = bytes.getUnsignedShort(offset);
        int numArguments = bytes.getUnsignedShort(offset + 2);

        if (observer != null) {
            observer.parsed(bytes, offset, 2, "bootstrap_method_ref: " + Hex.u2(methodRef));
            observer.parsed(bytes, offset + 2, 2,
                            "num_bootstrap_arguments: " + Hex.u2(numArguments));
        }

        offset += 4;
        length -= 4;
        if (length < numArguments * 2) {
            throwTruncated();
        }

        BootstrapMethodArgumentsList arguments = new BootstrapMethodArgumentsList(numArguments);
        for (int argIndex = 0; argIndex < numArguments; ++argIndex, offset += 2, length -= 2) {
            int argumentRef = bytes.getUnsignedShort(offset);
            if (observer != null) {
                observer.parsed(bytes, offset, 2,
                                "bootstrap_arguments[" + argIndex + "]" + Hex.u2(argumentRef));
            }
            arguments.set(argIndex, constantPool.get(argumentRef));
        }
        arguments.setImmutable();
        Constant cstMethodRef = constantPool.get(methodRef);
        methods.set(methodIndex, declaringClass, (CstMethodHandle) cstMethodRef, arguments);
    }
    methods.setImmutable();

    if (length != 0) {
        throwBadLength(length);
    }

    return methods;
}