Java Code Examples for org.apache.bcel.classfile.ConstantString

The following examples show how to use org.apache.bcel.classfile.ConstantString. 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: uyuni   Source File: FindStrings.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Main class, to find strings in class files.
 * @param args Arguments to program.
 */
public static void main(String[] args) {
    try {
        JavaClass clazz = Repository.lookupClass(args[0]);
        ConstantPool cp = clazz.getConstantPool();
        Constant[] consts = cp.getConstantPool();


        for (int i = 0; i < consts.length; i++) {

            if (consts[i] instanceof ConstantString) {
                System.out.println("Found String: " +
                        ((ConstantString)consts[i]).getBytes(cp));
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 2
@Override
public void visit(ConstantValue s) {
    if (!visitingField()) {
        return;
    }
    int i = s.getConstantValueIndex();
    Constant c = getConstantPool().getConstant(i);
    if (c instanceof ConstantString) {
        String value = ((ConstantString) c).getBytes(getConstantPool());
        if (value.length() < SIZE_OF_HUGE_CONSTANT) {
            return;
        }
        String key = getStringKey(value);
        definition.put(key, XFactory.createXField(this));
        stringSize.put(key, value.length());
    }

}
 
Example 3
Source Project: spotbugs   Source File: OpcodeStack.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
private void pushByConstant(DismantleBytecode dbc, Constant c) {

        if (c instanceof ConstantClass) {
            push(new Item("Ljava/lang/Class;", ((ConstantClass) c).getConstantValue(dbc.getConstantPool())));
        } else if (c instanceof ConstantInteger) {
            push(new Item("I", Integer.valueOf(((ConstantInteger) c).getBytes())));
        } else if (c instanceof ConstantString) {
            int s = ((ConstantString) c).getStringIndex();
            push(new Item("Ljava/lang/String;", getStringFromIndex(dbc, s)));
        } else if (c instanceof ConstantFloat) {
            push(new Item("F", Float.valueOf(((ConstantFloat) c).getBytes())));
        } else if (c instanceof ConstantDouble) {
            push(new Item("D", Double.valueOf(((ConstantDouble) c).getBytes())));
        } else if (c instanceof ConstantLong) {
            push(new Item("J", Long.valueOf(((ConstantLong) c).getBytes())));
        } else {
            throw new UnsupportedOperationException("StaticConstant type not expected");
        }
    }
 
Example 4
Source Project: spacewalk   Source File: FindStrings.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Main class, to find strings in class files.
 * @param args Arguments to program.
 */
public static void main(String[] args) {
    try {
        JavaClass clazz = Repository.lookupClass(args[0]);
        ConstantPool cp = clazz.getConstantPool();
        Constant[] consts = cp.getConstantPool();


        for (int i = 0; i < consts.length; i++) {

            if (consts[i] instanceof ConstantString) {
                System.out.println("Found String: " +
                        ((ConstantString)consts[i]).getBytes(cp));
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Source Project: commons-bcel   Source File: ConstantPoolGen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Add a new String constant to the ConstantPool, if it is not already in there.
 *
 * @param str String to add
 * @return index of entry
 */
public int addString( final String str ) {
    int ret;
    if ((ret = lookupString(str)) != -1) {
        return ret; // Already in CP
    }
    final int utf8 = addUtf8(str);
    adjustSize();
    final ConstantString s = new ConstantString(utf8);
    ret = index;
    constants[index++] = s;
    if (!stringTable.containsKey(str)) {
        stringTable.put(str, new Index(ret));
    }
    return ret;
}
 
Example 6
Source Project: commons-bcel   Source File: Pass3aVerifier.java    License: Apache License 2.0 6 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
// LDC and LDC_W (LDC_W is a subclass of LDC in BCEL's model)
@Override
public void visitLDC(final LDC ldc) {
    indexValid(ldc, ldc.getIndex());
    final Constant c = constantPoolGen.getConstant(ldc.getIndex());
    if (c instanceof ConstantClass) {
      addMessage("Operand of LDC or LDC_W is CONSTANT_Class '"+c+"' - this is only supported in JDK 1.5 and higher.");
    }
    else{
      if (! ( (c instanceof ConstantInteger)    ||
              (c instanceof ConstantFloat)         ||
        (c instanceof ConstantString) ) ) {
    constraintViolated(ldc,
        "Operand of LDC or LDC_W must be one of CONSTANT_Integer, CONSTANT_Float or CONSTANT_String, but is '"+c+"'.");
      }
    }
}
 
Example 7
Source Project: commons-bcel   Source File: Pass2Verifier.java    License: Apache License 2.0 6 votes vote down vote up
private CPESSC_Visitor(final JavaClass _jc) {
    jc = _jc;
    cp = _jc.getConstantPool();
    cplen = cp.getLength();

    CONST_Class = ConstantClass.class;
    /*
    CONST_Fieldref = ConstantFieldref.class;
    CONST_Methodref = ConstantMethodref.class;
    CONST_InterfaceMethodref = ConstantInterfaceMethodref.class;
    */
    CONST_String = ConstantString.class;
    CONST_Integer = ConstantInteger.class;
    CONST_Float = ConstantFloat.class;
    CONST_Long = ConstantLong.class;
    CONST_Double = ConstantDouble.class;
    CONST_NameAndType = ConstantNameAndType.class;
    CONST_Utf8 = ConstantUtf8.class;

    carrier = new DescendingVisitor(_jc, this);
    carrier.visit();
}
 
Example 8
Source Project: commons-bcel   Source File: ExecutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */
@Override
public void visitLDC(final LDC o) {
    final Constant c = cpg.getConstant(o.getIndex());
    if (c instanceof ConstantInteger) {
        stack().push(Type.INT);
    }
    if (c instanceof ConstantFloat) {
        stack().push(Type.FLOAT);
    }
    if (c instanceof ConstantString) {
        stack().push(Type.STRING);
    }
    if (c instanceof ConstantClass) {
        stack().push(Type.CLASS);
    }
}
 
Example 9
Source Project: commons-bcel   Source File: ExecutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */
public void visitLDC_W(final LDC_W o) {
    final Constant c = cpg.getConstant(o.getIndex());
    if (c instanceof ConstantInteger) {
        stack().push(Type.INT);
    }
    if (c instanceof ConstantFloat) {
        stack().push(Type.FLOAT);
    }
    if (c instanceof ConstantString) {
        stack().push(Type.STRING);
    }
    if (c instanceof ConstantClass) {
        stack().push(Type.CLASS);
    }
}
 
Example 10
Source Project: ApkToolPlus   Source File: ExecutionVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ 
public void visitLDC(LDC o){
	Constant c = cpg.getConstant(o.getIndex());
	if (c instanceof ConstantInteger){
		stack().push(Type.INT);
	}
	if (c instanceof ConstantFloat){
		stack().push(Type.FLOAT);
	}
	if (c instanceof ConstantString){
		stack().push(Type.STRING);
	}
}
 
Example 11
Source Project: ApkToolPlus   Source File: ExecutionVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ 
public void visitLDC_W(LDC_W o){
	Constant c = cpg.getConstant(o.getIndex());
	if (c instanceof ConstantInteger){
		stack().push(Type.INT);
	}
	if (c instanceof ConstantFloat){
		stack().push(Type.FLOAT);
	}
	if (c instanceof ConstantString){
		stack().push(Type.STRING);
	}
}
 
Example 12
Source Project: ApkToolPlus   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
public void visitLDC(LDC o){
	// visitCPInstruction is called first.
	
	Constant c = cpg.getConstant(o.getIndex());
	if 	(!	(	( c instanceof ConstantInteger) ||
						( c instanceof ConstantFloat	)	||
						( c instanceof ConstantString )	)	){
		constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
	}
}
 
Example 13
Source Project: ApkToolPlus   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
public void visitLDC_W(LDC_W o){
	// visitCPInstruction is called first.
	
	Constant c = cpg.getConstant(o.getIndex());
	if 	(!	(	( c instanceof ConstantInteger) ||
						( c instanceof ConstantFloat	)	||
						( c instanceof ConstantString )	)	){
		constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
	}
}
 
Example 14
public void printOpCode(int seen) {
    System.out.print("  " + this.getClass().getSimpleName() + ": [" + formatter.format(getPC()) + "]  " + Const.getOpcodeName(seen));
    if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKESPECIAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESTATIC)) {
        System.out.print("   " + getClassConstantOperand() + "." + getNameConstantOperand() + " " + getSigConstantOperand());
    } else if (seen == Const.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) {
        Constant c = getConstantRefOperand();
        if (c instanceof ConstantString) {
            System.out.print("   \"" + getStringConstantOperand() + "\"");
        } else if (c instanceof ConstantClass) {
            System.out.print("   " + getClassConstantOperand());
        } else {
            System.out.print("   " + c);
        }
    } else if ((seen == Const.ALOAD) || (seen == Const.ASTORE)) {
        System.out.print("   " + getRegisterOperand());
    } else if ((seen == Const.GOTO) || (seen == Const.GOTO_W) || (seen == Const.IF_ACMPEQ) || (seen == Const.IF_ACMPNE)
            || (seen == Const.IF_ICMPEQ)
            || (seen == Const.IF_ICMPGE) || (seen == Const.IF_ICMPGT) || (seen == Const.IF_ICMPLE) || (seen == Const.IF_ICMPLT)
            || (seen == Const.IF_ICMPNE) || (seen == Const.IFEQ) || (seen == Const.IFGE) || (seen == Const.IFGT) || (seen == Const.IFLE)
            || (seen == Const.IFLT)
            || (seen == Const.IFNE) || (seen == Const.IFNONNULL) || (seen == Const.IFNULL)) {
        System.out.print("   " + getBranchTarget());
    } else if ((seen == Const.NEW) || (seen == Const.INSTANCEOF)) {
        System.out.print("   " + getClassConstantOperand());
    } else if ((seen == Const.TABLESWITCH) || (seen == Const.LOOKUPSWITCH)) {
        System.out.print("    [");
        int switchPC = getPC();
        int[] offsets = getSwitchOffsets();
        for (int offset : offsets) {
            System.out.print((switchPC + offset) + ",");
        }
        System.out.print((switchPC + getDefaultSwitchOffset()) + "]");
    }

    System.out.println();
}
 
Example 15
@Override
public void visit(ConstantString s) {
    String value = s.getBytes(getConstantPool());
    if (value.length() < SIZE_OF_HUGE_CONSTANT) {
        return;
    }
    String key = getStringKey(value);
    SortedSet<String> set = map.computeIfAbsent(key, k -> new TreeSet<>());
    set.add(getDottedClassName());
}
 
Example 16
Source Project: commons-bcel   Source File: Pass2Verifier.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantString(final ConstantString obj) {
    if (obj.getTag() != Const.CONSTANT_String) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    checkIndex(obj, obj.getStringIndex(), CONST_Utf8);
}
 
Example 17
Source Project: commons-bcel   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitLDC(final LDC o) {
    // visitCPInstruction is called first.

    final Constant c = cpg.getConstant(o.getIndex());
    if     (!    (    ( c instanceof ConstantInteger) ||
                ( c instanceof ConstantFloat    )    ||
                ( c instanceof ConstantString    )    ||
                ( c instanceof ConstantClass    ) )    ) {
        constraintViolated(o,
            "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
             c + "'.");
    }
}
 
Example 18
Source Project: commons-bcel   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
public void visitLDC_W(final LDC_W o) {
    // visitCPInstruction is called first.

    final Constant c = cpg.getConstant(o.getIndex());
    if     (!    (    ( c instanceof ConstantInteger) ||
                ( c instanceof ConstantFloat    )    ||
                ( c instanceof ConstantString    )    ||
                ( c instanceof ConstantClass    ) )    ) {
        constraintViolated(o,
            "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
             c + "'.");
    }
}
 
Example 19
public void visit(ConstantString obj) {
    visit((Constant) obj);
}
 
Example 20
@Override
public void visitConstantString(ConstantString obj) {
    visit(obj);
}
 
Example 21
@Override
public void sawOpcode(int seen) {
    if (reportedForThisClass) {
        return;
    }

    switch (seen) {
    case Const.LDC:
        Constant constantValue = getConstantRefOperand();
        if (constantValue instanceof ConstantClass) {
            sawGetClass = -100;
        } else if (constantValue instanceof ConstantString) {
            stringConstant = ((ConstantString) constantValue).getBytes(getConstantPool());
        }
        break;

    case Const.ALOAD_0:
        state = 1;
        break;
    case Const.INVOKEVIRTUAL:
        if ("java/lang/Class".equals(getClassConstantOperand())
                && ("getResource".equals(getNameConstantOperand()) || "getResourceAsStream".equals(getNameConstantOperand()))
                && sawGetClass + 10 >= getPC()) {
            int priority = NORMAL_PRIORITY;
            if (prevOpcode == Const.LDC && stringConstant != null && stringConstant.length() > 0 && stringConstant.charAt(0) == '/') {
                priority = LOW_PRIORITY;
            } else {
                priority = adjustPriority(priority);
            }
            bugReporter.reportBug(new BugInstance(this, "UI_INHERITANCE_UNSAFE_GETRESOURCE", priority)
                    .addClassAndMethod(this).addSourceLine(this));
            reportedForThisClass = true;

        } else if (state == 1 && !methodIsStatic && !classIsFinal && classIsVisibleToOtherPackages
                && "getClass".equals(getNameConstantOperand()) && "()Ljava/lang/Class;".equals(getSigConstantOperand())) {
            sawGetClass = getPC();
        }
        state = 0;
        break;
    default:
        state = 0;
        break;
    }
    if (seen != Const.LDC) {
        stringConstant = null;
    }
    prevOpcode = seen;

}
 
Example 22
Source Project: commons-bcel   Source File: StringRepresentation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantString(final ConstantString obj) {
    tostring = toString(obj);
}
 
Example 23
Source Project: commons-bcel   Source File: CounterVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantString(final ConstantString obj)
{
    constantStringCount++;
}