package slangc.bytecode; public class StackMachineAdapter implements StackMachineVisitor { protected final StackMachineVisitor target; protected final ReferenceFactory referenceFactory; public StackMachineAdapter(StackMachineVisitor target, ReferenceFactory referenceFactory) { this.target = target; this.referenceFactory = referenceFactory; } //@Override public StackMachineVisitor.FieldReference getFieldReference(StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference type, String name) { return referenceFactory.getFieldReference(this, owner, isStatic, type, name); } //@Override public StackMachineVisitor.MethodReference getMethodReference(StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference returnType, String name, StackMachineVisitor.TypeReference[] argumentTypes) { return referenceFactory.getMethodReference(this, owner, isStatic, returnType, name, argumentTypes); } //@Override public StackMachineVisitor.TypeReference voidTypeReference() { return referenceFactory.voidTypeReference(this); } //@Override public StackMachineVisitor.TypeReference getTypeReference(String packageName, String typeName) { return referenceFactory.getTypeReference(this, packageName, typeName); } //@Override public void finish() { target.finish(); } /** Reference construction is separated out since it may be handled very differently in different scenarios. * See DefaultReferenceFactory for a simple "pass-through" implementation. Other adapters may want to keep a * description of the references instead and then reconstruct references as they're used, or may want to pass * some of them through but also need to cache some information. */ public static interface ReferenceFactory { public StackMachineVisitor.FieldReference toTarget(StackMachineVisitor.FieldReference localReference); public StackMachineVisitor.MethodReference toTarget(StackMachineVisitor.MethodReference localReference); public StackMachineVisitor.TypeReference toTarget(StackMachineVisitor.TypeReference localReference); public StackMachineVisitor.TypeReference[] toTarget(StackMachineVisitor.TypeReference[] localReferences); public StackMachineVisitor.FieldReference getFieldReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference type, String name); public StackMachineVisitor.MethodReference getMethodReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference returnType, String name, StackMachineVisitor.TypeReference[] argumentTypes); public StackMachineVisitor.TypeReference voidTypeReference(StackMachineVisitor.StackMachineAdapter adapter); public StackMachineVisitor.TypeReference getTypeReference(StackMachineVisitor.StackMachineAdapter adapter, String packageName, String typeName); } public static class DefaultReferenceFactory implements ReferenceFactory { //@Override public StackMachineVisitor.FieldReference toTarget(StackMachineVisitor.FieldReference localReference) { return localReference; } //@Override public StackMachineVisitor.MethodReference toTarget(StackMachineVisitor.MethodReference localReference) { return localReference; } //@Override public StackMachineVisitor.TypeReference toTarget(StackMachineVisitor.TypeReference localReference) { return localReference; } //@Override public StackMachineVisitor.TypeReference[] toTarget(StackMachineVisitor.TypeReference[] localReferences) { return localReferences; } //@Override public StackMachineVisitor.FieldReference getFieldReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference type, String name) { return adapter.target.getFieldReference(owner, isStatic, type, name); } //@Override public StackMachineVisitor.MethodReference getMethodReference(StackMachineAdapter adapter, StackMachineVisitor.TypeReference owner, boolean isStatic, StackMachineVisitor.TypeReference returnType, String name, StackMachineVisitor.TypeReference[] argumentTypes) { return adapter.target.getMethodReference(owner, isStatic, returnType, name, argumentTypes); } //@Override public StackMachineVisitor.TypeReference voidTypeReference(StackMachineAdapter adapter) { return adapter.target.voidTypeReference(); } //@Override public StackMachineVisitor.TypeReference getTypeReference(StackMachineAdapter adapter, String packageName, String typeName) { return adapter.target.getTypeReference(packageName, typeName); } } public static class SystemAdapter extends StackMachineAdapter implements SystemVisitor { public SystemAdapter(StackMachineVisitor target, ReferenceFactory referenceFactory) { super(target, referenceFactory); } //@Override public StackMachineVisitor.TypeVisitor beginType(StackMachineVisitor.TypeReference reference) { ((StackMachineVisitor.SystemVisitor)target).beginType(reference); return null; } } public static class AdapterWithAttributes extends StackMachineAdapter implements StackMachineVisitor.VisitorWithAttributes { public AdapterWithAttributes(StackMachineVisitor.VisitorWithAttributes target, ReferenceFactory referenceFactory) { super(target, referenceFactory); } //@Override public void setProtectionType(StackMachineVisitor.ProtectionType t) { ((StackMachineVisitor.VisitorWithAttributes)target).setProtectionType(t); } } public static class TypeAdapter extends AdapterWithAttributes implements StackMachineVisitor.TypeVisitor { public TypeAdapter(StackMachineVisitor.TypeVisitor target, ReferenceFactory referenceFactory) { super(target, referenceFactory); } //@Override public void setSourceFile(String filename) { ((StackMachineVisitor.TypeVisitor)target).setSourceFile(filename); } //@Override public void setBaseType(StackMachineVisitor.TypeReference reference) { ((StackMachineVisitor.TypeVisitor)target).setBaseType(referenceFactory.toTarget(reference)); } //@Override public void setInterfaceTypes(StackMachineVisitor.TypeReference[] interfaces) { ((StackMachineVisitor.TypeVisitor)target).setInterfaceTypes(referenceFactory.toTarget(interfaces)); } //@Override public void setOuterType(StackMachineVisitor.TypeReference reference) { ((StackMachineVisitor.TypeVisitor)target).setOuterType(referenceFactory.toTarget(reference)); } //@Override public StackMachineVisitor.FieldVisitor beginField(StackMachineVisitor.FieldReference reference) { // TODO Auto-generated method stub return null; } //@Override public StackMachineVisitor.MethodVisitor beginMethod(StackMachineVisitor.MethodReference reference) { // TODO Auto-generated method stub return null; } } }