use of org.apache.bcel.classfile.JavaClass in project qpid-broker-j by apache.
the class BrokerTestHelper method createSubClassByteCode.
private static byte[] createSubClassByteCode(final String className, final Class<?>[] interfaces) {
String[] ifnames = new String[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
ifnames[i] = interfaces[i].getName();
}
ClassGen classGen = new ClassGen(className, "java.lang.Object", "<generated>", ACC_PUBLIC | ACC_SUPER | ACC_INTERFACE, ifnames);
JavaClass javaClass = classGen.getJavaClass();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
javaClass.dump(out);
return out.toByteArray();
} catch (IOException ioex) {
throw new IllegalStateException("Could not write to a ByteArrayOutputStream - should not happen", ioex);
}
}
use of org.apache.bcel.classfile.JavaClass in project ant by apache.
the class JavaClassHelper method getConstants.
/**
* Get the constants declared in a file as name=value
*
* @param bytes the class as a array of bytes
* @return a StringBuffer contains the name=value pairs
* @exception IOException if an error occurs
*/
public static StringBuffer getConstants(final byte[] bytes) throws IOException {
final StringBuffer sb = new StringBuffer();
final ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
final ClassParser parser = new ClassParser(bis, "");
final JavaClass javaClass = parser.parse();
final Field[] fields = javaClass.getFields();
for (final Field field : fields) {
if (field != null) {
final ConstantValue cv = field.getConstantValue();
if (cv != null) {
String cvs = cv.toString();
// Remove start and end quotes if field is a String
if (cvs.startsWith("\"") && cvs.endsWith("\"")) {
cvs = cvs.substring(1, cvs.length() - 1);
}
sb.append(field.getName());
sb.append('=');
sb.append(cvs);
sb.append(LS);
}
}
}
return sb;
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class HiddenInheritedFieldCheck method visitObject.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void visitObject(Object aJavaClass) {
final JavaClass javaClass = (JavaClass) aJavaClass;
final String className = javaClass.getClassName();
final JavaClass[] superClasses = javaClass.getSuperClasses();
final Field[] fields = javaClass.getFields();
// Check all fields
for (int i = 0; i < fields.length; i++) {
final Field field = fields[i];
// Go through all superclasses
for (int j = 0; j < superClasses.length; j++) {
final JavaClass superClass = superClasses[j];
final String superClassName = superClass.getClassName();
final Field[] superClassFields = superClass.getFields();
// Go through the filds of the superclasses
for (int k = 0; k < superClassFields.length; k++) {
final Field superClassField = superClassFields[k];
if (!superClassField.isPrivate() && superClassField.getName().equals(field.getName()) && !ignore(className, field)) {
log(javaClass, 0, "hidden.inherited.field", new Object[] { fields[i], superClassName });
}
}
}
}
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class HiddenStaticMethodCheck method visitObject.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void visitObject(Object aJavaClass) {
final JavaClass javaClass = (JavaClass) aJavaClass;
final String className = javaClass.getClassName();
final JavaClass[] superClasses = javaClass.getSuperClasses();
final Method[] methods = javaClass.getMethods();
// Check all methods
for (int i = 0; i < methods.length; i++) {
final Method method = methods[i];
// Check that the method is a possible match
if (!method.isPrivate() && method.isStatic()) {
// Go through all their superclasses
for (int j = 0; j < superClasses.length; j++) {
final JavaClass superClass = superClasses[j];
final String superClassName = superClass.getClassName();
final Method[] superClassMethods = superClass.getMethods();
// Go through the methods of the superclasses
for (int k = 0; k < superClassMethods.length; k++) {
final Method superClassMethod = superClassMethods[k];
if (superClassMethod.getName().equals(method.getName()) && !ignore(className, method)) {
Type[] methodTypes = method.getArgumentTypes();
Type[] superTypes = superClassMethod.getArgumentTypes();
if (methodTypes.length == superTypes.length) {
boolean match = true;
for (int arg = 0; arg < methodTypes.length; arg++) {
if (!methodTypes[arg].equals(superTypes[arg])) {
match = false;
}
}
// Same method parameters
if (match) {
log(javaClass, 0, "hidden.static.method", new Object[] { method, superClassName });
}
}
}
}
}
}
}
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class UnreadFieldCheck 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 FieldDefinition[] fieldDefs = classDef.getFieldDefs();
for (int i = 0; i < fieldDefs.length; i++) {
if (fieldDefs[i].getReadReferenceCount() == 0) {
final Field field = fieldDefs[i].getField();
if (!field.isFinal() && (!ignore(className, field))) {
log(javaClass, 0, "unread.field", new Object[] { fieldDefs[i] });
}
}
}
}
}
Aggregations