use of org.apache.bcel.classfile.ClassParser in project OpenGrok by OpenGrok.
the class JavaClassAnalyzer method analyze.
void analyze(Document doc, InputStream in, Writer xrefOut) throws IOException {
List<String> defs = new ArrayList<>();
List<String> refs = new ArrayList<>();
List<String> full = new ArrayList<>();
StringWriter dout = new StringWriter();
StringWriter rout = new StringWriter();
StringWriter fout = new StringWriter();
/**
* The JarAnalyzer uses JavaClassAnalyzer, so if a DEFS, REFS, or FULL
* field exists already, then append to it.
*/
useExtantValue(dout, doc, QueryBuilder.DEFS);
useExtantValue(rout, doc, QueryBuilder.REFS);
useExtantValue(fout, doc, QueryBuilder.FULL);
ClassParser classparser = new ClassParser(in, doc.get(QueryBuilder.PATH));
StringWriter xout = new StringWriter();
getContent(xout, fout, classparser.parse(), defs, refs, full);
String xref = xout.toString();
if (xrefOut != null) {
xrefOut.append(xref);
try {
xrefOut.flush();
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Couldn't flush xref, will retry once added to doc", ex);
}
}
appendValues(dout, defs, "");
appendValues(rout, refs, "");
appendValues(fout, full, "// ");
/**
* Unlike other analyzers, which rely on the full content existing to be
* accessed at a file system location identified by PATH, *.class and
* *.jar files have virtual content which is stored here (Store.YES) for
* analyzer convenience.
*/
String dstr = dout.toString();
doc.add(new TextField(QueryBuilder.DEFS, dstr, Store.YES));
String rstr = rout.toString();
doc.add(new TextField(QueryBuilder.REFS, rstr, Store.YES));
String fstr = fout.toString();
doc.add(new TextField(QueryBuilder.FULL, fstr, Store.YES));
}
use of org.apache.bcel.classfile.ClassParser 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.ClassParser in project tycho by eclipse.
the class OsgiCompilerTest method assertBytecodeMajorLevel.
private void assertBytecodeMajorLevel(int majorLevel, File classFile) throws ClassFormatException, IOException {
assertTrue(classFile.canRead());
JavaClass javaClass = new ClassParser(classFile.getAbsolutePath()).parse();
assertEquals(majorLevel, javaClass.getMajor());
}
use of org.apache.bcel.classfile.ClassParser in project jop by jop-devel.
the class AppInfo method tryLoadClass.
private ClassInfo tryLoadClass(String className) throws IOException {
loadLogger.debug("Loading class " + className);
InputStream is = classPath.getInputStream(className);
JavaClass javaClass = new ClassParser(is, className).parse();
is.close();
if (javaClass.getMajor() > 50) {
// instruction (requires patching of BCEL code similar to Classpath and InstructionFinder)
throw new JavaClassFormatError("Classfiles with versions 51.0 (Java 7) and above are currently not supported!");
}
return new ClassInfo(new ClassGen(javaClass));
}
use of org.apache.bcel.classfile.ClassParser in project kanonizo by kanonizo.
the class Framework method loadClassFromFile.
private Class<?> loadClassFromFile(File file) {
Class<?> cl = null;
try {
ClassParser parser = new ClassParser(file.getAbsolutePath());
JavaClass jcl = parser.parse();
List<String> forbiddenClasses = Arrays.asList(FORBIDDEN_CLASSNAMES.split(","));
forbiddenClasses = forbiddenClasses.stream().filter(name -> !name.isEmpty()).collect(Collectors.toList());
if (forbiddenClasses.size() > 0 && forbiddenClasses.stream().anyMatch(f -> jcl.getClassName().substring(jcl.getPackageName().length() + 1).startsWith(f))) {
logger.info("Ignoring class " + jcl.getClassName() + " because it is forbidden");
return null;
} else {
cl = Class.forName(jcl.getClassName(), true, Thread.currentThread().getContextClassLoader());
}
} catch (ClassNotFoundException | NoClassDefFoundError e) {
logger.error(e);
} catch (IOException e) {
logger.error(e);
} catch (ExceptionInInitializerError e) {
logger.error(e);
}
return cl;
}
Aggregations