Tuesday, March 10, 2015

ClassConstraintException

com.sun.org.apache.bcel.internal.verifier.exc.ClassConstraintException

ClassConstraintException is described in the javadoc comments as:

Instances of this class are thrown by BCEL's class file verifier 'JustIce' when a class file to verify does not pass the verification pass 2 as described in the Java Virtual Machine specification, 2nd edition.
version: $Id: ClassConstraintException.java,v 1.1.1.1 2001/10/29 20:00:33 jvanzyl Exp $ author: Enver Haase

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

com.sun.org.apache.bcel.internal.verifier.statics.Pass2Verifier.every_class_has_an_accessible_superclass()

/**
  * Ensures that every class has a super class and that
  * <B>final</B> classes are not subclassed.
  * This means, the class this Pass2Verifier operates
  * on has proper super classes (transitively) up to
  * java.lang.Object.
  * The reason for really loading (and Pass1-verifying)
  * all of those classes here is that we need them in
  * Pass2 anyway to verify no final methods are overridden
  * (that could be declared anywhere in the ancestor hierarchy).
  *
  * @throws ClassConstraintException otherwise.
  */
private void every_class_has_an_accessible_superclass() {
    HashSet hs = new HashSet();
    JavaClass jc = Repository.lookupClass(myOwner.getClassName());
    int supidx = -1;
    while (supidx != 0) {
        supidx = jc.getSuperclassNameIndex();
        if (supidx == 0) {
            if (jc != Repository.lookupClass(Type.OBJECT.getClassName())) {
                throw new ClassConstraintException('Superclass of '' + jc.getClassName() + '' missing but not ' + Type.OBJECT.getClassName() + ' itself!');
            }
        } else {
            String supername = jc.getSuperclassName();
            if (!hs.add(supername)) {
                throw new ClassConstraintException('Circular superclass hierarchy detected.');
            }
            Verifier v = VerifierFactory.getVerifier(supername);
            VerificationResult vr = v.doPass1();
            if (vr != VerificationResult.VR_OK) {
                throw new ClassConstraintException('Could not load in ancestor class '' + supername + ''.');
            }
            jc = Repository.lookupClass(supername);
            if (jc.isFinal()) {
                throw new ClassConstraintException('Ancestor class '' + supername + '' has the FINAL access modifier and must therefore not be subclassed.');
            }
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.org.apache.bcel.internal.verifier.statics.Pass3aVerifier.delayedPass2Checks()

/**
  * These are the checks that could be done in pass 2 but are delayed to pass 3
  * for performance reasons. Also, these checks need access to the code array
  * of the Code attribute of a Method so it's okay to perform them here.
  * Also see the description of the do_verify() method.
  *
  * @throws ClassConstraintException if the verification fails.
  * @see #do_verify()
  */
private void delayedPass2Checks() {
    int[] instructionPositions = instructionList.getInstructionPositions();
    int codeLength = code.getCode().length;
    LineNumberTable lnt = code.getLineNumberTable();
    if (lnt != null) {
        LineNumber[] lineNumbers = lnt.getLineNumberTable();
        IntList offsets = new IntList();
        lineNumber_loop: for (int i = 0; i < lineNumbers.length; i++) {
            for (int j = 0; j < instructionPositions.length; j++) {
                int offset = lineNumbers[i].getStartPC();
                if (instructionPositions[j] == offset) {
                    if (offsets.contains(offset)) {
                        addMessage('LineNumberTable attribute '' + code.getLineNumberTable() + '' refers to the same code offset ('' + offset + '') more than once which is violating the semantics [but is sometimes produced by IBM's 'jikes' compiler].');
                    } else {
                        offsets.add(offset);
                    }
                    continue lineNumber_loop;
                }
            }
            throw new ClassConstraintException('Code attribute '' + code + '' has a LineNumberTable attribute '' + code.getLineNumberTable() + '' referring to a code offset ('' + lineNumbers[i].getStartPC() + '') that does not exist.');
        }
    }
    Attribute[] atts = code.getAttributes();
    for (int a = 0; a < atts.length; a++) {
        if (atts[a] instanceof LocalVariableTable) {
            LocalVariableTable lvt = (LocalVariableTable) atts[a];
            if (lvt != null) {
                LocalVariable[] localVariables = lvt.getLocalVariableTable();
                for (int i = 0; i < localVariables.length; i++) {
                    int startpc = localVariables[i].getStartPC();
                    int length = localVariables[i].getLength();
                    if (!contains(instructionPositions, startpc)) {
                        throw new ClassConstraintException('Code attribute '' + code + '' has a LocalVariableTable attribute '' + code.getLocalVariableTable() + '' referring to a code offset ('' + startpc + '') that does not exist.');
                    }
                    if ((!contains(instructionPositions, startpc + length)) && (startpc + length != codeLength)) {
                        throw new ClassConstraintException('Code attribute '' + code + '' has a LocalVariableTable attribute '' + code.getLocalVariableTable() + '' referring to a code offset start_pc+length ('' + (startpc + length) + '') that does not exist.');
                    }
                }
            }
        }
    }
    CodeException[] exceptionTable = code.getExceptionTable();
    for (int i = 0; i < exceptionTable.length; i++) {
        int startpc = exceptionTable[i].getStartPC();
        int endpc = exceptionTable[i].getEndPC();
        int handlerpc = exceptionTable[i].getHandlerPC();
        if (startpc >= endpc) {
            throw new ClassConstraintException('Code attribute '' + code + '' has an exception_table entry '' + exceptionTable[i] + '' that has its start_pc ('' + startpc + '') not smaller than its end_pc ('' + endpc + '').');
        }
        if (!contains(instructionPositions, startpc)) {
            throw new ClassConstraintException('Code attribute '' + code + '' has an exception_table entry '' + exceptionTable[i] + '' that has a non-existant bytecode offset as its start_pc ('' + startpc + '').');
        }
        if ((!contains(instructionPositions, endpc)) && (endpc != codeLength)) {
            throw new ClassConstraintException('Code attribute '' + code + '' has an exception_table entry '' + exceptionTable[i] + '' that has a non-existant bytecode offset as its end_pc ('' + startpc + '') [that is also not equal to code_length ('' + codeLength + '')].');
        }
        if (!contains(instructionPositions, handlerpc)) {
            throw new ClassConstraintException('Code attribute '' + code + '' has an exception_table entry '' + exceptionTable[i] + '' that has a non-existant bytecode offset as its handler_pc ('' + handlerpc + '').');
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.org.apache.bcel.internal.verifier.statics.Pass2Verifier.final_methods_are_not_overridden()

/**
  * Ensures that <B>final</B> methods are not overridden.
  * <B>Precondition to run this method:
  * constant_pool_entries_satisfy_static_constraints() and
  * every_class_has_an_accessible_superclass() have to be invoked before
  * (in that order).</B>
  *
  * @throws ClassConstraintException otherwise.
  * @see #constant_pool_entries_satisfy_static_constraints()
  * @see #every_class_has_an_accessible_superclass()
  */
private void final_methods_are_not_overridden() {
    HashMap hashmap = new HashMap();
    JavaClass jc = Repository.lookupClass(myOwner.getClassName());
    int supidx = -1;
    while (supidx != 0) {
        supidx = jc.getSuperclassNameIndex();
        ConstantPoolGen cpg = new ConstantPoolGen(jc.getConstantPool());
        Method[] methods = jc.getMethods();
        for (int i = 0; i < methods.length; i++) {
            String name_and_sig = (methods[i].getName() + methods[i].getSignature());
            if (hashmap.containsKey(name_and_sig)) {
                if (methods[i].isFinal()) {
                    throw new ClassConstraintException('Method '' + name_and_sig + '' in class '' + hashmap.get(name_and_sig) + '' overrides the final (not-overridable) definition in class '' + jc.getClassName() + ''.');
                } else {
                    if (!methods[i].isStatic()) {
                        hashmap.put(name_and_sig, jc.getClassName());
                    }
                }
            } else {
                if (!methods[i].isStatic()) {
                    hashmap.put(name_and_sig, jc.getClassName());
                }
            }
        }
        jc = Repository.lookupClass(jc.getSuperclassName());
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment