use of com.jopdesign.common.ClassInfo in project jop by jop-devel.
the class UsedCodeFinder method visitReferences.
private void visitReferences(Set<String> refs) {
for (String id : refs) {
// The member IDs returned by the reference finder use a syntax which is
// always unique, so if in doubt, we need to interpret it as a classname, not a fieldname
MemberID sig = MemberID.parse(id, false);
// find/load the corresponding classInfo
ClassInfo cls = getClassInfo(sig);
// class has been excluded from loading, skip this class
if (cls == null) {
continue;
}
// referenced class is used, visit it if it has not yet been visited, but skip its class members
// Note that this class might be different than the class containing the class member
markUsedMembers(cls, false);
// check if this id specifies a class member (or just a class, in this case we are done)
if (sig.hasMethodSignature()) {
// It's a method! mark the method as used (implementations are marked later)
MethodRef ref = appInfo.getMethodRef(sig);
MethodInfo method = ref.getMethodInfo();
// we keep the declarations. We mark it without following it and make it abstract later.
if (method != null) {
setMark(method, false);
}
} else if (sig.hasMemberName()) {
// It's a field! No need to look in subclasses, fields are not virtual
FieldRef ref = appInfo.getFieldRef(sig);
FieldInfo field = ref.getFieldInfo();
if (field != null) {
markUsedMembers(field);
}
}
}
}
use of com.jopdesign.common.ClassInfo 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 com.jopdesign.common.ClassInfo in project jop by jop-devel.
the class ClinitOrder method findOrder.
/**
* Find a 'correct' oder for the static <clinit>.
* Throws an error on cyclic dependencies.
*
* @return the ordered list of classes
* @throws JavaClassFormatError if a cyclic dependency has been found.
*/
public List<ClassInfo> findOrder() {
printDependency(false);
Set<ClassInfo> cliSet = clinit.keySet();
List<ClassInfo> order = new LinkedList<ClassInfo>();
int maxIter = cliSet.size();
// maximum loop bound detects cyclic dependency
for (int i = 0; i < maxIter && cliSet.size() != 0; ++i) {
Iterator itCliSet = cliSet.iterator();
while (itCliSet.hasNext()) {
ClassInfo clinf = (ClassInfo) itCliSet.next();
Set<ClassInfo> depends = clinit.get(clinf);
if (depends.size() == 0) {
order.add(clinf);
// element (a leave in the dependent tree
for (ClassInfo clinfInner : clinit.keySet()) {
Set<ClassInfo> dep = clinit.get(clinfInner);
dep.remove(clinf);
}
itCliSet.remove();
}
}
}
if (cliSet.size() != 0) {
printDependency(true);
throw new JavaClassFormatError("Cyclic dependency in <clinit>");
}
return order;
}
use of com.jopdesign.common.ClassInfo in project jop by jop-devel.
the class ExampleTool method doSomething.
public void doSomething(Config config) {
AppInfo appInfo = AppInfo.getSingleton();
// access and modify some classes
System.out.println("field of main class: " + manager.getMyField(appInfo.getMainMethod().getClassInfo()));
for (ClassInfo root : appInfo.getRootClasses()) {
System.out.println("field of root: " + manager.getMyField(root));
}
// create a new class and new methods
try {
ClassInfo newCls = appInfo.createClass("MyTest", appInfo.getClassRef("java.lang.Object"), false);
} catch (NamingConflictException e) {
e.printStackTrace();
}
}
use of com.jopdesign.common.ClassInfo in project jop by jop-devel.
the class TypeGraph method assignRelativeNumbering.
private int assignRelativeNumbering(ClassInfo node, Map<ClassInfo, Pair<Integer, Integer>> rn, int low) {
int next = low;
for (DefaultEdge subEdge : outgoingEdgesOf(node)) {
ClassInfo sub = getEdgeTarget(subEdge);
if (sub.isInterface())
continue;
next = assignRelativeNumbering(sub, rn, next + 1);
}
rn.put(node, new Pair<Integer, Integer>(low, next));
return next;
}
Aggregations