1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
package org.python.expose.generate;
import junit.framework.TestCase;
import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;
import org.objectweb.asm.commons.EmptyVisitor;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
public class ExposeMethodFinderTest extends TestCase implements Opcodes, PyTypes {
private ExposedMethodFinder makeFinder(int access, String descriptor, String methodName) {
return new ExposedMethodFinder("simpleexposed",
Type.getType(SimpleExposed.class),
access,
methodName,
descriptor,
null,
new EmptyVisitor()) {
@Override
public void handleResult(InstanceMethodExposer exposer) {
resultantMethExp = exposer;
}
@Override
public void handleNewExposer(Exposer exposer) {
resultantNewExp = exposer;
}
@Override
public void exposeAsDeleteDescriptor(String descName) {
deleteName = descName;
}
@Override
public void exposeAsGetDescriptor(String descName, String doc) {
getName = descName;
}
@Override
public void exposeAsSetDescriptor(String descName) {
setName = descName;
}
@Override
public void handleResult(ClassMethodExposer exposer) {}
};
}
private void checkSet(String typeThatShouldBeSet) {
checkSet(METHOD, typeThatShouldBeSet, resultantMethExp);
checkSet(NEW, typeThatShouldBeSet, resultantNewExp);
checkSet(GET, typeThatShouldBeSet, getName);
checkSet(SET, typeThatShouldBeSet, setName);
checkSet(DELETE, typeThatShouldBeSet, deleteName);
}
private void checkSet(String type, String typeThatShouldBeSet, Object actualValue) {
if(type.equals(typeThatShouldBeSet)) {
assertNotNull(type + " should've been set after these operations", actualValue);
} else {
assertNull(type + " shouldn't have been set by these operations", actualValue);
}
}
public void testExposedNew() {
ExposedMethodFinder visitor = makeFinder(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
NewExposer.NEW_DESCRIPTOR, "new_");
visitor.visitAnnotation(EXPOSED_NEW.getDescriptor(), true);
visitor.visitEnd();
checkSet(NEW);
}
public void testSimpleExposedMethod() {
makeAndVisit(EXPOSED_METHOD, METHOD, null);
assertEquals(1, resultantMethExp.getNames().length);
assertEquals("simpleMethod", resultantMethExp.getNames()[0]);
}
private void makeAndVisit(Type annotationToVisit,
String typeThatShouldBeSet,
String arrayToVisit,
String... vals) {
ExposedMethodFinder visitor = makeFinder(Opcodes.ACC_PRIVATE, "()V", "simpleMethod");
AnnotationVisitor methVisitor = visitor.visitAnnotation(annotationToVisit.getDescriptor(),
true);
if(arrayToVisit != null) {
AnnotationVisitor arrayVisitor = methVisitor.visitArray(arrayToVisit);
for(String val : vals) {
arrayVisitor.visit("doesn't matter in asm", val);
}
arrayVisitor.visitEnd();
methVisitor.visitEnd();
}
visitor.visitEnd();
checkSet(typeThatShouldBeSet);
}
public void testNamesExposedMethod() {
makeAndVisit(EXPOSED_METHOD, METHOD, "names", "first", "second");
assertEquals(0, resultantMethExp.defaults.length);
assertEquals(2, resultantMethExp.getNames().length);
assertEquals("first", resultantMethExp.getNames()[0]);
assertEquals("second", resultantMethExp.getNames()[1]);
}
public void testDefaultsExposedMethod() {
makeAndVisit(EXPOSED_METHOD, METHOD, "defaults", "Py.None");
assertEquals(1, resultantMethExp.defaults.length);
assertEquals("Py.None", resultantMethExp.defaults[0]);
}
public void testDelDescriptor() {
makeAndVisitDescr("simpleMethod", EXPOSED_DELETE, DELETE);
assertEquals("simpleMethod", deleteName);
}
public void testSetDescriptor() {
makeAndVisitDescr("simpleMethod", EXPOSED_SET, SET);
assertEquals("simpleMethod", setName);
}
public void testGetDescriptor() {
makeAndVisitDescr("getVal", EXPOSED_GET, GET);
assertEquals("getVal", getName);
}
public void testNamedGetDescriptor() {
makeAndVisitDescr("getVal", EXPOSED_GET, GET, "val");
assertEquals("val", getName);
}
public void testNamedSetDescriptor() {
makeAndVisitDescr("setVal", EXPOSED_SET, SET, "val");
assertEquals("val", setName);
}
public void testNamedDelDescriptor() {
makeAndVisitDescr("delVal", EXPOSED_DELETE, DELETE, "val");
assertEquals("val", deleteName);
}
private void makeAndVisitDescr(String methodName, Type annotationToVisit, String typeThatShouldBeSet) {
makeAndVisitDescr(methodName, annotationToVisit, typeThatShouldBeSet, null);
}
private void makeAndVisitDescr(String methodName, Type annotationToVisit, String typeThatShouldBeSet, String name) {
ExposedMethodFinder visitor = makeFinder(Opcodes.ACC_PRIVATE, "()V", methodName);
AnnotationVisitor methVisitor = visitor.visitAnnotation(annotationToVisit.getDescriptor(),
true);
if(name != null) {
methVisitor.visit("name", name);
}
methVisitor.visitEnd();
visitor.visitEnd();
checkSet(typeThatShouldBeSet);
}
private static final String METHOD = "method";
private static final String NEW = "new";
private static final String GET = "get";
private static final String SET = "set";
private static final String DELETE = "del";
private InstanceMethodExposer resultantMethExp;
private Exposer resultantNewExp;
private String deleteName, getName, setName;
}
|