use of org.apache.bcel.classfile.Constant in project OpenGrok by OpenGrok.
the class JavaClassAnalyzer method getContent.
// TODO this class needs to be thread safe to avoid bug 13364, which was fixed by just updating bcel to 5.2
private void getContent(Writer out, Writer fout, JavaClass c, List<String> defs, List<String> refs, List<String> full) throws IOException {
String t;
ConstantPool cp = c.getConstantPool();
int[] v = new int[cp.getLength() + 1];
out.write(linkPath(t = c.getSourceFileName()));
defs.add(t);
refs.add(t);
fout.write(t);
out.write(EOL);
fout.write(EOL);
out.write(PACKAGE);
fout.write(PACKAGE);
out.write(linkDef(t = c.getPackageName()));
defs.add(t);
refs.add(t);
fout.write(t);
out.write(EOL);
fout.write(EOL);
String aflg;
out.write(aflg = Utility.accessToString(c.getAccessFlags(), true));
if (aflg != null) {
out.write(SPACE);
fout.write(aflg);
fout.write(SPACE);
}
v[c.getClassNameIndex()] = 1;
out.write(tagDef(t = c.getClassName()));
defs.add(t);
refs.add(t);
fout.write(t);
out.write(EXTENDS);
fout.write(EXTENDS);
v[c.getSuperclassNameIndex()] = 1;
out.write(linkDef(t = c.getSuperclassName()));
refs.add(t);
fout.write(t);
for (int i : c.getInterfaceIndices()) {
v[i] = 1;
}
String[] ins = c.getInterfaceNames();
if (ins != null && ins.length > 0) {
out.write(IMPLEMENTS);
fout.write(IMPLEMENTS);
for (String in : ins) {
out.write(linkDef(t = in));
refs.add(t);
fout.write(t);
out.write(SPACE);
fout.write(SPACE);
}
}
out.write(LCBREOL);
fout.write(LCBREOL);
for (Attribute a : c.getAttributes()) {
if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
for (Attribute ca : ((Code) a).getAttributes()) {
if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
for (LocalVariable l : ((LocalVariableTable) ca).getLocalVariableTable()) {
printLocal(out, fout, l, v, defs, refs);
}
}
}
} else if (a.getTag() == org.apache.bcel.Const.ATTR_BOOTSTRAP_METHODS) {
// TODO fill in bootstrap methods, fix the else if
} else if (a.getTag() == org.apache.bcel.Const.ATTR_SOURCE_FILE) {
v[a.getNameIndex()] = 1;
break;
}
}
String aflgs;
String fldsig;
String tdef;
for (org.apache.bcel.classfile.Field fld : c.getFields()) {
out.write(TAB);
fout.write(TAB);
aflgs = Utility.accessToString(fld.getAccessFlags());
if (aflgs != null && aflgs.length() > 0) {
out.write(aflgs);
fout.write(aflgs);
fout.write(SPACE);
out.write(SPACE);
}
fldsig = Utility.signatureToString(fld.getSignature());
out.write(fldsig);
fout.write(fldsig);
out.write(SPACE);
fout.write(SPACE);
tdef = tagDef(t = fld.getName());
out.write(tdef);
fout.write(tdef);
defs.add(t);
refs.add(t);
out.write(EOL);
fout.write(EOL);
// TODO show Attributes
}
String sig;
String msig;
String ltdef;
for (org.apache.bcel.classfile.Method m : c.getMethods()) {
out.write(TAB);
fout.write(TAB);
aflgs = Utility.accessToString(m.getAccessFlags());
if (aflgs != null && aflgs.length() > 0) {
out.write(aflgs);
fout.write(aflgs);
out.write(SPACE);
fout.write(SPACE);
}
sig = m.getSignature();
msig = Utility.methodSignatureReturnType(sig, false);
out.write(msig);
fout.write(msig);
out.write(SPACE);
fout.write(SPACE);
ltdef = tagDef(t = m.getName());
out.write(ltdef);
fout.write(ltdef);
defs.add(t);
refs.add(t);
out.write(LBRA);
fout.write(LBRA);
String[] args = Utility.methodSignatureArgumentTypes(sig, false);
for (int i = 0; i < args.length; i++) {
t = args[i];
out.write(t);
fout.write(t);
int spi = t.indexOf(SPACE);
if (spi > 0) {
refs.add(t.substring(0, spi));
defs.add(t.substring(spi + 1));
}
if (i < args.length - 1) {
out.write(COMMA);
fout.write(COMMA);
}
}
out.write(RBRA);
fout.write(RBRA);
ArrayList<LocalVariable[]> locals = new ArrayList<>();
for (Attribute a : m.getAttributes()) {
if (a.getTag() == org.apache.bcel.Const.ATTR_EXCEPTIONS) {
for (int i : ((ExceptionTable) a).getExceptionIndexTable()) {
v[i] = 1;
}
String[] exs = ((ExceptionTable) a).getExceptionNames();
if (exs != null && exs.length > 0) {
out.write(THROWS);
fout.write(THROWS);
for (String ex : exs) {
out.write(linkDef(ex));
fout.write(ex);
refs.add(ex);
out.write(SPACE);
fout.write(SPACE);
}
}
} else if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
for (Attribute ca : ((Code) a).getAttributes()) {
if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
locals.add(((LocalVariableTable) ca).getLocalVariableTable());
}
}
}
}
out.write(EOL);
fout.write(EOL);
if (!locals.isEmpty()) {
for (LocalVariable[] ls : locals) {
for (LocalVariable l : ls) {
printLocal(out, fout, l, v, defs, refs);
}
}
}
}
out.write(RCBREOL);
fout.write(RCBREOL);
for (int i = 0; i < v.length - 1; i++) {
if (v[i] != 1) {
Constant constant = cp.getConstant(i);
if (constant != null) {
full.add(constantToString(constant, cp, v));
}
}
}
}
use of org.apache.bcel.classfile.Constant in project fb-contrib by mebigfatguy.
the class SillynessPotPourri method checkEqualsStringBufferLength.
private void checkEqualsStringBufferLength() {
if (stack.getStackDepth() > 0) {
OpcodeStack.Item itm = stack.getStackItem(0);
lastIfEqWasBoolean = Values.SIG_PRIMITIVE_BOOLEAN.equals(itm.getSignature());
}
byte[] bytes = getCode().getCode();
if ((lastPCs[1] != -1) && (CodeByteUtils.getbyte(bytes, lastPCs[3]) == Const.INVOKEVIRTUAL)) {
int loadIns = CodeByteUtils.getbyte(bytes, lastPCs[2]);
if (((loadIns == Const.LDC) || (loadIns == Const.LDC_W)) && (CodeByteUtils.getbyte(bytes, lastPCs[1]) == Const.INVOKEVIRTUAL)) {
ConstantPool pool = getConstantPool();
int toStringIndex = CodeByteUtils.getshort(bytes, lastPCs[1] + 1);
Constant cmr = pool.getConstant(toStringIndex);
if (cmr instanceof ConstantMethodref) {
ConstantMethodref toStringMR = (ConstantMethodref) cmr;
String toStringCls = toStringMR.getClass(pool);
if (toStringCls.startsWith("java.lang.StringBu")) {
int consIndex = CodeByteUtils.getbyte(bytes, lastPCs[2] + 1);
Constant c = pool.getConstant(consIndex);
if ((c instanceof ConstantString) && ((ConstantString) c).getBytes(pool).isEmpty()) {
int nandtIndex = toStringMR.getNameAndTypeIndex();
ConstantNameAndType cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
if (Values.TOSTRING.equals(cnt.getName(pool))) {
int lengthIndex = CodeByteUtils.getshort(bytes, lastPCs[3] + 1);
ConstantMethodref lengthMR = (ConstantMethodref) pool.getConstant(lengthIndex);
nandtIndex = lengthMR.getNameAndTypeIndex();
cnt = (ConstantNameAndType) pool.getConstant(nandtIndex);
if ("equals".equals(cnt.getName(pool))) {
bugReporter.reportBug(new BugInstance(this, BugType.SPP_USE_STRINGBUILDER_LENGTH.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
}
}
}
}
}
}
}
}
use of org.apache.bcel.classfile.Constant in project fb-contrib by mebigfatguy.
the class RegisterUtilsTest method shouldGetParameterRegisters.
@Test(dataProvider = "parameterRegisters")
public void shouldGetParameterRegisters(int accessFlags, String signature, int[] expected) {
Method method = new Method();
method.setAccessFlags(accessFlags);
Constant[] cnst = new Constant[] { new ConstantUtf8(signature) };
ConstantPool cp = new ConstantPool(cnst) {
@Override
protected Object clone() throws CloneNotSupportedException {
throw new CloneNotSupportedException();
}
};
method.setConstantPool(cp);
method.setSignatureIndex(0);
int[] regs = RegisterUtils.getParameterRegisters(method);
assertEquals(regs, expected);
}
use of org.apache.bcel.classfile.Constant in project fb-contrib by mebigfatguy.
the class ArrayIndexOutOfBounds method sawOpcode.
/**
* overrides the visitor to look for stores to arrays that can be statically determined to be outside the bounds of the initialized array
*
* @param seen
* the currently parsed opcode
*/
@Override
public void sawOpcode(int seen) {
Integer size = null;
boolean sizeSet = false;
try {
stack.precomputation(this);
switch(seen) {
case Const.ICONST_0:
case Const.ICONST_1:
case Const.ICONST_2:
case Const.ICONST_3:
case Const.ICONST_4:
case Const.ICONST_5:
size = Integer.valueOf(seen - Const.ICONST_0);
sizeSet = true;
break;
case Const.ILOAD:
case Const.ILOAD_0:
case Const.ILOAD_1:
case Const.ILOAD_2:
case Const.ILOAD_3:
{
int reg = RegisterUtils.getLoadReg(this, seen);
if (modifyRegs.get(reg)) {
modifyRegs.clear(reg);
sizeSet = true;
}
}
break;
case Const.BIPUSH:
case Const.SIPUSH:
size = Integer.valueOf(getIntConstant());
sizeSet = true;
break;
case Const.IINC:
modifyRegs.set(getRegisterOperand());
break;
case Const.IADD:
case Const.ISUB:
case Const.IMUL:
case Const.IDIV:
case Const.F2I:
case Const.D2I:
case Const.L2I:
sizeSet = true;
break;
case Const.ISTORE:
case Const.ISTORE_0:
case Const.ISTORE_1:
case Const.ISTORE_2:
case Const.ISTORE_3:
if (stack.getStackDepth() > 0) {
OpcodeStack.Item item = stack.getStackItem(0);
if (item.getUserValue() == null) {
modifyRegs.set(getRegisterOperand());
}
}
break;
case Const.LDC:
Constant c = getConstantRefOperand();
if (c instanceof ConstantInteger) {
size = Integer.valueOf(((ConstantInteger) c).getBytes());
sizeSet = true;
}
break;
case Const.NEWARRAY:
case Const.ANEWARRAY:
if (stack.getStackDepth() >= 1) {
OpcodeStack.Item item = stack.getStackItem(0);
size = (Integer) item.getUserValue();
sizeSet = true;
}
break;
case Const.IASTORE:
case Const.LASTORE:
case Const.FASTORE:
case Const.DASTORE:
case Const.AASTORE:
case Const.BASTORE:
case Const.CASTORE:
case Const.SASTORE:
processArrayStore();
break;
case Const.IALOAD:
case Const.LALOAD:
case Const.FALOAD:
case Const.DALOAD:
case Const.AALOAD:
case Const.BALOAD:
case Const.CALOAD:
case Const.SALOAD:
processArrayLoad();
break;
case Const.ASTORE_0:
case Const.ASTORE_1:
case Const.ASTORE_2:
case Const.ASTORE_3:
case Const.ASTORE:
if (stack.getStackDepth() > 0) {
OpcodeStack.Item value = stack.getStackItem(0);
if (!value.isNull()) {
initializedRegs.set(getRegisterOperand());
}
} else {
initializedRegs.set(getRegisterOperand());
}
break;
case Const.IFEQ:
case Const.IFNE:
case Const.IFLT:
case Const.IFGE:
case Const.IFGT:
case Const.IFLE:
case Const.IF_ICMPEQ:
case Const.IF_ICMPNE:
case Const.IF_ICMPLT:
case Const.IF_ICMPGE:
case Const.IF_ICMPGT:
case Const.IF_ICMPLE:
case Const.IF_ACMPEQ:
case Const.IF_ACMPNE:
case Const.GOTO:
case Const.GOTO_W:
int branchTarget = getBranchTarget();
Iterator<Map.Entry<Integer, Integer>> it = nullStoreToLocation.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Integer, Integer> entry = it.next();
int pc = entry.getValue().intValue();
if ((branchTarget < pc) && initializedRegs.get(entry.getKey().intValue())) {
it.remove();
}
}
break;
}
} finally {
stack.sawOpcode(this, seen);
if (sizeSet && (stack.getStackDepth() >= 1)) {
OpcodeStack.Item item = stack.getStackItem(0);
item.setUserValue(size);
}
}
}
use of org.apache.bcel.classfile.Constant in project fb-contrib by mebigfatguy.
the class IncorrectInternalClassUse method visitClassContext.
/**
* implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class Const in the constant pool
*
* @param context
* the context object of the currently parsed class
*/
@Override
public void visitClassContext(ClassContext context) {
JavaClass cls = context.getJavaClass();
if (!isInternal(cls.getClassName())) {
ConstantPool pool = cls.getConstantPool();
int numItems = pool.getLength();
for (int i = 0; i < numItems; i++) {
Constant c = pool.getConstant(i);
if (c instanceof ConstantClass) {
String clsName = ((ConstantClass) c).getBytes(pool);
if (isInternal(clsName)) {
bugReporter.reportBug(new BugInstance(this, BugType.IICU_INCORRECT_INTERNAL_CLASS_USE.name(), NORMAL_PRIORITY).addClass(cls).addString(clsName));
}
}
}
}
}
Aggregations