use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class UnusedMethodCheck method leaveSet.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void leaveSet(Set aJavaClasses) {
final Iterator it = aJavaClasses.iterator();
while (it.hasNext()) {
final JavaClass javaClass = (JavaClass) it.next();
final String className = javaClass.getClassName();
final JavaClassDefinition classDef = findJavaClassDef(javaClass);
final MethodDefinition[] methodDefs = classDef.getMethodDefs();
for (int i = 0; i < methodDefs.length; i++) {
if (!classDef.hasReference(methodDefs[i], getReferenceDAO())) {
final Method method = methodDefs[i].getMethod();
if (!ignore(className, method)) {
log(javaClass, 0, "unused.method", new Object[] { methodDefs[i] });
}
}
}
}
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class JavaClassDefinition method hasReference.
/**
* Determines whether there is reference to a given Method in this JavaClass
* definition or a definition in a superclass.
* @param aMethodDef the Method to check.
* @param aReferenceDAO reference DAO.
* @return true if there is a reference to the method of aMethodDef in
* this JavaClass or a superclass.
*/
public boolean hasReference(MethodDefinition aMethodDef, ReferenceDAO aReferenceDAO) {
final String methodName = aMethodDef.getName();
final Type[] argTypes = aMethodDef.getArgumentTypes();
// search the inheritance hierarchy
JavaClass currentJavaClass = getJavaClass();
while (currentJavaClass != null) {
final JavaClassDefinition javaClassDef = aReferenceDAO.findJavaClassDef(currentJavaClass);
if (javaClassDef != null) {
final MethodDefinition methodDef = javaClassDef.findNarrowestMethod(getJavaClass().getClassName(), methodName, argTypes);
if ((methodDef != null) && (methodDef.hasReference(getJavaClass()))) {
return true;
}
}
currentJavaClass = currentJavaClass.getSuperClass();
}
return false;
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class ReferenceDAO method findFieldDef.
/**
* Finds the definition of the field of a field reference.
* @param aFieldRef the reference to a field.
* @return the definition of the field for aFieldRef.
*/
public FieldDefinition findFieldDef(FieldReference aFieldRef) {
final String className = aFieldRef.getClassName();
JavaClass javaClass = Repository.lookupClass(className);
final String fieldName = aFieldRef.getName();
// search up the class hierarchy for the class containing the
// method definition.
FieldDefinition result = null;
while ((javaClass != null) && (result == null)) {
final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass);
if (javaClassDef != null) {
result = javaClassDef.findFieldDef(fieldName);
}
// search the parent
javaClass = javaClass.getSuperClass();
}
return result;
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class ReferenceDAO method addFieldReference.
/**
* Adds a reference to a field.
* @param aFieldRef the field reference.
*/
public void addFieldReference(FieldReference aFieldRef) {
final String className = aFieldRef.getClassName();
JavaClass javaClass = Repository.lookupClass(className);
final String fieldName = aFieldRef.getName();
// search up the class hierarchy for the class containing the
// method definition.
FieldDefinition fieldDef = null;
while ((javaClass != null) && (fieldDef == null)) {
final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass);
if (javaClassDef != null) {
fieldDef = javaClassDef.findFieldDef(fieldName);
if (fieldDef != null) {
fieldDef.addReference(aFieldRef);
}
}
// search the parent
javaClass = javaClass.getSuperClass();
}
}
use of org.apache.bcel.classfile.JavaClass in project android-classyshark by google.
the class RootBuilder method fillFromJar.
private ClassNode fillFromJar(File file) {
ClassNode rootNode = new ClassNode(file.getName());
try {
JarFile theJar = new JarFile(file);
Enumeration<? extends JarEntry> en = theJar.entries();
while (en.hasMoreElements()) {
JarEntry entry = en.nextElement();
if (entry.getName().endsWith(".class")) {
ClassParser cp = new ClassParser(theJar.getInputStream(entry), entry.getName());
JavaClass jc = cp.parse();
ClassInfo classInfo = new ClassInfo(jc.getClassName(), jc.getMethods().length);
rootNode.add(classInfo);
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + file + ". " + e.getMessage());
e.printStackTrace(System.err);
}
return rootNode;
}
Aggregations