use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.
the class ClassWriter method write.
public void write(String writeDir) throws IOException {
AppInfo appInfo = AppInfo.getSingleton();
if (logger.isInfoEnabled()) {
logger.info("Start writing classes to '" + writeDir + "' ..");
}
File classDir = new File(writeDir);
if (classDir.exists()) {
if (classDir.isFile()) {
throw new IOException("Output directory '" + classDir + "' is a file.");
}
} else if (!classDir.mkdirs()) {
throw new IOException("Could not create output directory " + classDir);
}
for (ClassInfo cls : appInfo.getClassInfos()) {
if (logger.isDebugEnabled()) {
logger.debug("Writing class: " + cls.getClassName());
}
JavaClass jc = cls.compile();
String filename = classDir + File.separator + cls.getClassName().replace(".", File.separator) + ".class";
File file = new File(filename);
String parent = file.getParent();
if (parent != null) {
File pDir = new File(parent);
// noinspection ResultOfMethodCallIgnored
pDir.mkdirs();
}
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
DataOutputStream stream = new DataOutputStream(out);
jc.dump(stream);
stream.close();
}
if (logger.isInfoEnabled()) {
logger.info(appInfo.getClassInfos().size() + " classes written.");
}
}
use of org.apache.bcel.classfile.JavaClass in project tycho by eclipse.
the class ExecutionEnvironmentTest method testCompilerSourceTargetConfigurationViaManifest.
@Test
public void testCompilerSourceTargetConfigurationViaManifest() throws Exception {
Verifier verifier = getVerifier("TYCHO476", false);
verifier.executeGoal("compile");
// compile only succeeds with source level 1.6 which
// is configured indirectly via Bundle-RequiredExecutionEnvironment: JavaSE-1.6
verifier.verifyErrorFreeLog();
File classFile = new File(verifier.getBasedir(), "target/classes/TestRunnable.class");
Assert.assertTrue(classFile.canRead());
JavaClass javaClass = new ClassParser(classFile.getAbsolutePath()).parse();
// bytecode major level 50 == target 1.6
Assert.assertEquals(50, javaClass.getMajor());
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class ClassFileSetCheck method visitSet.
/**
* @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
*/
public void visitSet(Set aSet) {
// register the JavaClasses in the Repository
Repository.clearCache();
Iterator it = aSet.iterator();
while (it.hasNext()) {
final JavaClass javaClass = (JavaClass) it.next();
Repository.addClass(javaClass);
}
// visit the visitors
it = getObjectSetVisitors().iterator();
while (it.hasNext()) {
final IObjectSetVisitor visitor = (IObjectSetVisitor) it.next();
visitor.visitSet(aSet);
}
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class ClassFileSetCheck method process.
/**
* @see com.puppycrawl.tools.checkstyle.api.FileSetCheck
*/
public void process(File[] aFiles) {
registerVisitors();
// get all the JavaClasses in the files
final Set javaClasses = extractJavaClasses(aFiles);
visitSet(javaClasses);
// walk each Java class parse tree
final JavaClassWalker walker = new JavaClassWalker();
walker.setVisitor(getTreeVisitor());
final Iterator it = javaClasses.iterator();
while (it.hasNext()) {
final JavaClass clazz = (JavaClass) it.next();
visitObject(clazz);
walker.walk(clazz);
leaveObject(clazz);
}
leaveSet(javaClasses);
fireErrors();
}
use of org.apache.bcel.classfile.JavaClass in project contribution by checkstyle.
the class ClassFileSetCheck method extractJavaClasses.
/**
* Extracts the JavaClasses from .class, .zip, and .jar files.
* @param aFile the file to extract from.
* @return the set of JavaClasses from aFile.
* @throws IOException if there is an error.
*/
private Set extractJavaClasses(File aFile) throws IOException {
final Set result = new HashSet();
final String fileName = aFile.getPath();
if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
final ZipFile zipFile = new ZipFile(fileName);
final Enumeration entries = zipFile.entries();
while (entries.hasMoreElements()) {
final ZipEntry entry = (ZipEntry) entries.nextElement();
final String entryName = entry.getName();
if (entryName.endsWith(".class")) {
final InputStream in = zipFile.getInputStream(entry);
final JavaClass javaClass = new ClassParser(in, entryName).parse();
result.add(javaClass);
}
}
} else {
final JavaClass javaClass = new ClassParser(fileName).parse();
result.add(javaClass);
}
return result;
}
Aggregations