From: <fwi...@us...> - 2008-07-26 01:27:27
|
Revision: 4999 http://jython.svn.sourceforge.net/jython/?rev=4999&view=rev Author: fwierzbicki Date: 2008-07-26 01:27:25 +0000 (Sat, 26 Jul 2008) Log Message: ----------- Re-enabled APIVersion checking in imp.java. Used ASM to read and write APIVersion as an annotation. Incremented APIVersion from 12 to 13. Modified Paths: -------------- branches/asm/src/org/python/compiler/APIVersion.java branches/asm/src/org/python/compiler/ClassFile.java branches/asm/src/org/python/compiler/Module.java branches/asm/src/org/python/core/imp.java Added Paths: ----------- branches/asm/src/org/python/core/APIReader.java Modified: branches/asm/src/org/python/compiler/APIVersion.java =================================================================== --- branches/asm/src/org/python/compiler/APIVersion.java 2008-07-25 06:07:24 UTC (rev 4998) +++ branches/asm/src/org/python/compiler/APIVersion.java 2008-07-26 01:27:25 UTC (rev 4999) @@ -1,21 +1,13 @@ -// Copyright (c) Corporation for National Research Initiatives - +/* + * Copyright (c) 2008 Jython Developers + * Licensed to PSF under a Contributor Agreement. + */ package org.python.compiler; -import java.io.DataOutputStream; -import java.io.IOException; -public class APIVersion { - int attName; - int version; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; - public APIVersion(int version) throws IOException { - //FJW attName = pool.UTF8("org.python.APIVersion"); - //FJW this.version = version; - } - - public void write(DataOutputStream stream) throws IOException { - //FJW stream.writeShort(attName); - //FJW stream.writeInt(4); - //FJW stream.writeInt(version); - } +@Retention(RetentionPolicy.RUNTIME) +public @interface APIVersion { + int value(); } Modified: branches/asm/src/org/python/compiler/ClassFile.java =================================================================== --- branches/asm/src/org/python/compiler/ClassFile.java 2008-07-25 06:07:24 UTC (rev 4998) +++ branches/asm/src/org/python/compiler/ClassFile.java 2008-07-26 01:27:25 UTC (rev 4999) @@ -10,6 +10,7 @@ import java.util.Collections; import java.util.List; +import org.python.objectweb.asm.AnnotationVisitor; import org.python.objectweb.asm.Attribute; import org.python.objectweb.asm.ClassWriter; import org.python.objectweb.asm.FieldVisitor; @@ -26,7 +27,6 @@ String[] interfaces; List<MethodVisitor> methodVisitors; List<FieldVisitor> fieldVisitors; - List<Attribute> attributes; public static String fixName(String n) { if (n.indexOf('.') == -1) @@ -53,7 +53,6 @@ methodVisitors = Collections.synchronizedList(new ArrayList()); fieldVisitors = Collections.synchronizedList(new ArrayList()); - attributes = Collections.synchronizedList(new ArrayList()); } public void setSource(String name) { @@ -67,7 +66,6 @@ interfaces = new_interfaces; } - //FIXME: Should really return a MethodVisitor public Code addMethod(String name, String type, int access) throws IOException { @@ -84,14 +82,6 @@ fieldVisitors.add(fv); } - public void endAttributes() - throws IOException - { - for (Attribute attr : attributes) { - cw.visitAttribute(attr); - } - } - public void endFields() throws IOException { @@ -110,17 +100,16 @@ } } - public void addAttribute(Attribute attr) throws IOException { - //FIXME: Do nothing for now. - //attributes.add(attr); - } - public void write(OutputStream stream) throws IOException { cw.visit(Opcodes.V1_5, Opcodes.ACC_PUBLIC + Opcodes.ACC_SUPER, this.name, null, this.superclass, interfaces); + AnnotationVisitor av = cw.visitAnnotation("Lorg/python/compiler/APIVersion;", true); + //XXX: should imp.java really house this value or should imp.java point into org.python.compiler? + av.visit("value", new Integer(org.python.core.imp.APIVersion)); + av.visitEnd(); + if (sfilename != null) { cw.visitSource(sfilename, null); } - endAttributes(); endFields(); endMethods(); Modified: branches/asm/src/org/python/compiler/Module.java =================================================================== --- branches/asm/src/org/python/compiler/Module.java 2008-07-25 06:07:24 UTC (rev 4998) +++ branches/asm/src/org/python/compiler/Module.java 2008-07-26 01:27:25 UTC (rev 4999) @@ -574,8 +574,6 @@ if (sfilename != null) { classfile.setSource(sfilename); } - //FIXME: switch to asm style. - //classfile.addAttribute(new APIVersion(org.python.core.imp.APIVersion)); classfile.write(stream); } Added: branches/asm/src/org/python/core/APIReader.java =================================================================== --- branches/asm/src/org/python/core/APIReader.java (rev 0) +++ branches/asm/src/org/python/core/APIReader.java 2008-07-26 01:27:25 UTC (rev 4999) @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2008 Jython Developers + * Licensed to PSF under a Contributor Agreement. + */ +package org.python.core; + +import org.python.objectweb.asm.AnnotationVisitor; +import org.python.objectweb.asm.ClassReader; +import org.python.objectweb.asm.commons.EmptyVisitor; + +import java.io.InputStream; +import java.io.IOException; + +/** + * This class reads a classfile from a byte array and pulls out the value of + * the class annotation for APIVersion, which can then be retrieved by a call + * to getVersion(). + * + * Hopefully the use of ClassReader in this implementation is not too + * expensive. I suspect it is not since EmptyVisitor is just a bag of empty + * methods so shouldn't cost too much. If it turns out to cost too much, we + * will want to implement a special purpose ClassReader that only reads out the + * APIVersion annotation I think. + */ +public class APIReader extends EmptyVisitor { + + private boolean nextVisitIsVersion = false; + + private int version = -1; + + public APIReader(byte[] data) throws IOException { + ClassReader r = new ClassReader(data); + r.accept(this, 0); + } + + public AnnotationVisitor visitAnnotation(String desc, boolean visible) { + nextVisitIsVersion = desc.equals("Lorg/python/compiler/APIVersion;"); + return this; + } + + public void visit(String name, Object value) { + if (nextVisitIsVersion) { + version = (Integer)value; + nextVisitIsVersion = false; + } + } + + public int getVersion() { + return version; + } +} Modified: branches/asm/src/org/python/core/imp.java =================================================================== --- branches/asm/src/org/python/core/imp.java 2008-07-25 06:07:24 UTC (rev 4998) +++ branches/asm/src/org/python/core/imp.java 2008-07-26 01:27:25 UTC (rev 4999) @@ -22,7 +22,7 @@ private static final String UNKNOWN_SOURCEFILE = "<unknown>"; - public static final int APIVersion = 12; + public static final int APIVersion = 13; /** A non-empty fromlist for __import__'ing sub-modules. */ private static final PyObject nonEmptyFromlist = new PyTuple(Py.newString("__doc__")); @@ -113,11 +113,13 @@ byte[] data = readBytes(fp); int n = data.length; - //Need to find another way to check the api version -- probably using - //an Annotation instead of an Attribute makes sense. - /* - int api = (data[n - 4] << 24) + (data[n - 3] << 16) - + (data[n - 2] << 8) + data[n - 1]; + int api; + try { + APIReader ar = new APIReader(data); + api = ar.getVersion(); + } catch (IOException i) { + api = -1; + } if (api != APIVersion) { if (testing) { return null; @@ -126,7 +128,6 @@ + APIVersion + ") in: " + name); } } - */ return data; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |