use of org.apache.bcel.classfile.Code 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.Code in project fb-contrib by mebigfatguy.
the class CyclomaticComplexity method visitMethod.
/**
* overrides the visitor to navigate the basic block list to count branches
*
* @param obj
* the method of the currently parsed method
*/
@Override
public void visitMethod(final Method obj) {
try {
if (obj.isSynthetic()) {
return;
}
Code code = obj.getCode();
if (code == null) {
return;
}
// bother
if (code.getCode().length < (2 * reportLimit)) {
return;
}
BitSet exceptionNodeTargets = new BitSet();
CFG cfg = classContext.getCFG(obj);
int branches = 0;
Iterator<BasicBlock> bbi = cfg.blockIterator();
while (bbi.hasNext()) {
BasicBlock bb = bbi.next();
Iterator<Edge> iei = cfg.outgoingEdgeIterator(bb);
int lastSwitchTargetBlockLabel = Integer.MIN_VALUE;
while (iei.hasNext()) {
Edge e = iei.next();
int edgeType = e.getType();
if ((edgeType != EdgeTypes.FALL_THROUGH_EDGE) && (edgeType != EdgeTypes.RETURN_EDGE) && (edgeType != EdgeTypes.UNKNOWN_EDGE)) {
if ((edgeType == EdgeTypes.UNHANDLED_EXCEPTION_EDGE) || (edgeType == EdgeTypes.HANDLED_EXCEPTION_EDGE)) {
int nodeTarget = e.getTarget().getLabel();
if (!exceptionNodeTargets.get(nodeTarget)) {
exceptionNodeTargets.set(nodeTarget);
branches++;
}
} else if ((edgeType == EdgeTypes.SWITCH_EDGE) || (edgeType == EdgeTypes.SWITCH_DEFAULT_EDGE)) {
int nodeTarget = e.getTarget().getLabel();
if (nodeTarget != lastSwitchTargetBlockLabel) {
branches++;
}
lastSwitchTargetBlockLabel = nodeTarget;
} else {
branches++;
}
}
}
}
if (branches > reportLimit) {
int priority = (branches > (reportLimit * 2) ? HIGH_PRIORITY : NORMAL_PRIORITY);
BugInstance bug = new BugInstance(this, BugType.CC_CYCLOMATIC_COMPLEXITY.name(), priority).addClass(this).addMethod(this).addSourceLine(classContext, this, 0).addInt(branches);
bugReporter.reportBug(bug);
}
} catch (CFGBuilderException cbe) {
bugReporter.logError("Failure examining basic blocks for method " + classContext.getJavaClass().getClassName() + '.' + obj.getName() + " in Cyclomatic Complexity detector", cbe);
}
}
use of org.apache.bcel.classfile.Code in project fb-contrib by mebigfatguy.
the class SluggishGui method visitAfter.
/**
* overrides the visitor to visit all of the collected listener methods
*
* @param obj
* the context object of the currently parsed class
*/
@Override
public void visitAfter(JavaClass obj) {
isListenerMethod = true;
for (Code l : listenerCode.keySet()) {
try {
super.visitCode(l);
} catch (StopOpcodeParsingException e) {
// method already reported
}
}
super.visitAfter(obj);
}
use of org.apache.bcel.classfile.Code in project fb-contrib by mebigfatguy.
the class TailRecursion method visitMethod.
/**
* implements the visitor to figure the pc where the method call must occur depending on whether the method returns a value, or not.
*
* @param obj
* the context object of the currently parsed method
*/
@Override
public void visitMethod(Method obj) {
Code c = obj.getCode();
if (c != null) {
byte[] opcodes = c.getCode();
if (opcodes != null) {
trPCPos = opcodes.length - 1;
if (!obj.getSignature().endsWith(Values.SIG_VOID)) {
trPCPos -= 1;
}
trPCPos -= TAILRECURSIONFUDGE;
possibleTailRecursion = true;
isStatic = obj.isStatic();
stack.resetForMethodEntry(this);
super.visitMethod(obj);
}
}
}
use of org.apache.bcel.classfile.Code 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.typeSignatureToString(fld.getSignature(), true);
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));
}
}
}
}
Aggregations