01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: */package org.objectweb.speedo.genclass.merger;
04:
05: import org.objectweb.asm.Attribute;
06: import org.objectweb.asm.ClassVisitor;
07: import org.objectweb.asm.CodeVisitor;
08: import org.objectweb.asm.Constants;
09: import org.objectweb.speedo.generation.enhancer.common.CodeRenamer;
10: import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
11: import org.objectweb.speedo.lib.Personality;
12: import org.objectweb.util.monolog.api.BasicLevel;
13: import org.objectweb.util.monolog.api.Logger;
14:
15: import java.util.Arrays;
16:
17: /**
18: * Writes the non abstract methods and the fields of a class.
19: */
20: public class GenClassAdapter extends LoggedClassAdapter {
21: private final GCInfo gc;
22: private boolean visitConstructor = true;
23:
24: public GenClassAdapter(ClassVisitor classVisitor, Logger logger,
25: GCInfo gc, Personality p) {
26: super (classVisitor, p, logger);
27: this .gc = gc;
28: }
29:
30: public boolean isVisitConstructor() {
31: return visitConstructor;
32: }
33:
34: public void setVisitConstructor(boolean v) {
35: this .visitConstructor = v;
36: }
37:
38: /**
39: * Makes the classes no more abstract
40: */
41: public void visit(final int version, final int access,
42: final String name, final String super name,
43: final String[] interfaces, final String sourceFile) {
44: if (logger.isLoggable(BasicLevel.DEBUG)) {
45: StringBuffer sb = new StringBuffer("\tVisit class: ");
46: sb.append(name).append("=>").append(gc.classToWrite);
47: sb.append(" extends ").append(super name).append("=>")
48: .append(gc.getSuperName());
49: if (interfaces != null && interfaces.length > 0) {
50: sb.append(" implements ").append(
51: Arrays.asList(interfaces));
52: }
53: logger.log(BasicLevel.DEBUG, sb.toString());
54: }
55: super .cv.visit(version, access - Constants.ACC_ABSTRACT,
56: gc.classToWrite, gc.getSuperName(), interfaces,
57: sourceFile);
58: }
59:
60: /**
61: * Add only non abstract method.
62: */
63: public CodeVisitor visitMethod(final int access, final String name,
64: final String desc, final String[] exceptions,
65: final Attribute attrs) {
66:
67: if ((access & Constants.ACC_ABSTRACT) == 0 //non abastract method
68: && (visitConstructor || !"<init>".equals(name)) // permit to forget constructors
69: ) {
70: logger.log(BasicLevel.DEBUG, "\t\tVisit the method: "
71: + name + desc);
72: CodeVisitor mv = super .cv.visitMethod(access, name, desc,
73: exceptions, attrs);
74: mv = new CodeRenamer(mv, gc.firstClass, gc.classToWrite);
75: mv = new CodeRenamer(mv, gc.secondClass, gc.classToWrite);
76: return mv;
77: } else {
78: return null;
79: }
80: }
81: }
|