From: <cg...@us...> - 2008-10-20 06:29:58
|
Revision: 5479 http://jython.svn.sourceforge.net/jython/?rev=5479&view=rev Author: cgroves Date: 2008-10-20 06:29:50 +0000 (Mon, 20 Oct 2008) Log Message: ----------- The pain of not having the generic inferring statics from Google collections finally got to the point where I felt the need to whip up version for Jython. Add Generic with static methods to make Lists, Maps and Sets that infer their generic types from the generics of whatever they're being assigned to. I went for brevity in naming the methods as I felt they'd be idiomatic enough in Jython to read well, but could be convinced that more descriptive names - newArrayList() instead of list() for example - would be better. Modified Paths: -------------- trunk/jython/src/org/python/compiler/AdapterMaker.java trunk/jython/src/org/python/compiler/ProxyMaker.java trunk/jython/src/org/python/core/BytecodeLoader.java trunk/jython/src/org/python/core/PyType.java trunk/jython/src/org/python/core/PyUnicode.java trunk/jython/src/org/python/expose/generate/ExposeTask.java trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java trunk/jython/src/org/python/expose/generate/TypeExposer.java trunk/jython/src/org/python/modules/_functools/PyPartial.java trunk/jython/src/org/python/util/NameUnionAntType.java trunk/jython/src/org/python/util/jython.java Added Paths: ----------- trunk/jython/src/org/python/util/Generic.java Modified: trunk/jython/src/org/python/compiler/AdapterMaker.java =================================================================== --- trunk/jython/src/org/python/compiler/AdapterMaker.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/compiler/AdapterMaker.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -8,6 +8,7 @@ import org.python.objectweb.asm.Label; import org.python.objectweb.asm.Opcodes; +import org.python.util.Generic; public class AdapterMaker extends ProxyMaker @@ -17,7 +18,7 @@ } public void build() throws Exception { - names = new HashSet<String>(); + names = Generic.set(); int access = Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNCHRONIZED; classfile = new ClassFile(myClass, "java/lang/Object", access); Modified: trunk/jython/src/org/python/compiler/ProxyMaker.java =================================================================== --- trunk/jython/src/org/python/compiler/ProxyMaker.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/compiler/ProxyMaker.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -8,14 +8,13 @@ import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import org.python.core.Py; import org.python.objectweb.asm.Label; import org.python.objectweb.asm.Opcodes; +import org.python.util.Generic; public class ProxyMaker implements ClassConstants, Opcodes { @@ -34,7 +33,7 @@ public static Map<Class<?>, Integer> types=fillTypes(); public static Map<Class<?>, Integer> fillTypes() { - Map<Class<?>, Integer> typeMap = new HashMap<Class<?>, Integer>(); + Map<Class<?>, Integer> typeMap = Generic.map(); typeMap.put(Boolean.TYPE, tBoolean); typeMap.put(Byte.TYPE, tByte); typeMap.put(Short.TYPE, tShort); @@ -57,7 +56,7 @@ Class<?> superclass; Class<?>[] interfaces; Set<String> names; - Set<String> supernames = new HashSet<String>(); + Set<String> supernames = Generic.set(); public ClassFile classfile; public String myClass; public boolean isAdapter=false; @@ -123,8 +122,8 @@ public static String makeSignature(Class<?>[] sig, Class<?> ret) { StringBuffer buf=new StringBuffer(); buf.append("("); - for (int i=0; i<sig.length; i++) { - buf.append(mapType(sig[i])); + for (Class<?> element : sig) { + buf.append(mapType(element)); } buf.append(")"); buf.append(mapType(ret)); @@ -315,7 +314,7 @@ Class<?>[] exceptions) throws Exception { Label start = null; Label end = null; - + String jcallName = "_jcall"; int instLocal = 0; @@ -374,7 +373,7 @@ boolean throwableFound = false; Label handlerStart = null; - for (int i = 0; i < exceptions.length; i++) { + for (Class<?> exception : exceptions) { handlerStart = new Label(); code.label(handlerStart); int excLocal = code.getLocal("java/lang/Throwable"); @@ -383,11 +382,11 @@ code.aload(excLocal); code.athrow(); - code.visitTryCatchBlock(start, end, handlerStart, mapClass(exceptions[i])); + code.visitTryCatchBlock(start, end, handlerStart, mapClass(exception)); doNullReturn(code, ret); code.freeLocal(excLocal); - if (exceptions[i] == Throwable.class) + if (exception == Throwable.class) throwableFound = true; } @@ -471,8 +470,8 @@ StringBuffer buf = new StringBuffer(m.getName()); buf.append(":"); Class<?>[] params = m.getParameterTypes(); - for (int i = 0; i < params.length; i++) { - buf.append(params[i].getName()); + for (Class<?> param : params) { + buf.append(param.getName()); buf.append(","); } return buf.toString(); @@ -480,8 +479,7 @@ protected void addMethods(Class<?> c, Set<String> t) throws Exception { Method[] methods = c.getDeclaredMethods(); - for (int i=0; i<methods.length; i++) { - Method method = methods[i]; + for (Method method : methods) { String s = methodString(method); if (t.contains(s)) continue; @@ -499,13 +497,13 @@ if (Modifier.isProtected(access)) { access = (access & ~Modifier.PROTECTED) | Modifier.PUBLIC; if (Modifier.isFinal(access)) { - addSuperMethod(methods[i], access); + addSuperMethod(method, access); continue; } } else if (Modifier.isFinal(access)) { continue; } - addMethod(methods[i], access); + addMethod(method, access); } Class<?> sc = c.getSuperclass(); @@ -513,8 +511,8 @@ addMethods(sc, t); Class<?>[] ifaces = c.getInterfaces(); - for (int j=0; j<ifaces.length; j++) { - addMethods(ifaces[j], t); + for (Class<?> iface : ifaces) { + addMethods(iface, t); } } @@ -530,15 +528,15 @@ public void addConstructors(Class<?> c) throws Exception { Constructor<?>[] constructors = c.getDeclaredConstructors(); String name = mapClass(c); - for (int i = 0; i < constructors.length; i++) { - int access = constructors[i].getModifiers(); + for (Constructor<?> constructor : constructors) { + int access = constructor.getModifiers(); if (Modifier.isPrivate(access)) continue; if (Modifier.isNative(access)) access = access & ~Modifier.NATIVE; if (Modifier.isProtected(access)) access = access & ~Modifier.PROTECTED | Modifier.PUBLIC; - Class<?>[] parameters = constructors[i].getParameterTypes(); + Class<?>[] parameters = constructor.getParameterTypes(); String sig = makeSignature(parameters, Void.TYPE); addConstructor(name, parameters, Void.TYPE, sig, access); } @@ -666,7 +664,7 @@ } public void build() throws Exception { - names = new HashSet<String>(); + names = Generic.set(); int access = superclass.getModifiers(); if ((access & Modifier.FINAL) != 0) { throw new InstantiationException("can't subclass final class"); @@ -678,17 +676,17 @@ addConstructors(superclass); classfile.addInterface("org/python/core/PyProxy"); - Set<String> seenmethods = new HashSet<String>(); + Set<String> seenmethods = Generic.set(); addMethods(superclass, seenmethods); - for (int i=0; i<interfaces.length; i++) { - if (interfaces[i].isAssignableFrom(superclass)) { + for (Class<?> iface : interfaces) { + if (iface.isAssignableFrom(superclass)) { Py.writeWarning("compiler", "discarding redundant interface: "+ - interfaces[i].getName()); + iface.getName()); continue; } - classfile.addInterface(mapClass(interfaces[i])); - addMethods(interfaces[i], seenmethods); + classfile.addInterface(mapClass(iface)); + addMethods(iface, seenmethods); } doConstants(); addClassDictInit(); Modified: trunk/jython/src/org/python/core/BytecodeLoader.java =================================================================== --- trunk/jython/src/org/python/core/BytecodeLoader.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/core/BytecodeLoader.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -2,10 +2,10 @@ package org.python.core; import java.security.SecureClassLoader; -import java.util.ArrayList; import java.util.List; import org.python.objectweb.asm.ClassReader; +import org.python.util.Generic; /** * Utility class for loading of compiled python modules and java classes defined in python modules. @@ -14,7 +14,7 @@ /** * Turn the java byte code in data into a java class. - * + * * @param name * the name of the class * @param data @@ -24,9 +24,9 @@ */ public static Class<?> makeClass(String name, byte[] data, Class<?>... referents) { Loader loader = new Loader(); - for (int i = 0; i < referents.length; i++) { + for (Class<?> referent : referents) { try { - ClassLoader cur = referents[i].getClassLoader(); + ClassLoader cur = referent.getClassLoader(); if (cur != null) { loader.addParent(cur); } @@ -37,7 +37,7 @@ /** * Turn the java byte code in data into a java class. - * + * * @param name * the name of the class * @param referents @@ -54,7 +54,7 @@ /** * Turn the java byte code for a compiled python module into a java class. - * + * * @param name * the name of the class * @param data @@ -73,7 +73,7 @@ public static class Loader extends SecureClassLoader { - private List<ClassLoader> parents = new ArrayList<ClassLoader>(); + private List<ClassLoader> parents = Generic.list(); public Loader() { parents.add(imp.getSyspathJavaLoader()); @@ -85,6 +85,7 @@ } } + @Override protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { Class<?> c = findLoadedClass(name); if (c != null) { Modified: trunk/jython/src/org/python/core/PyType.java =================================================================== --- trunk/jython/src/org/python/core/PyType.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/core/PyType.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -8,12 +8,12 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Modifier; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import org.python.core.util.StringUtil; import org.python.expose.ExposeAsSuperclass; @@ -24,6 +24,7 @@ import org.python.expose.ExposedSet; import org.python.expose.ExposedType; import org.python.expose.TypeBuilder; +import org.python.util.Generic; /** * The Python Type object implementation. @@ -76,7 +77,7 @@ private int numSlots; private ReferenceQueue<PyType> subclasses_refq = new ReferenceQueue<PyType>(); - private HashSet<WeakReference<PyType>> subclasses = new HashSet<WeakReference<PyType>>(); + private Set<WeakReference<PyType>> subclasses = Generic.set(); private final static Class<?>[] O = {PyObject.class}; private final static Class<?>[] OO = {PyObject.class, PyObject.class}; @@ -548,7 +549,7 @@ PyObject[] savedBases = bases; PyType savedBase = base; PyObject[] savedMro = mro; - List<Object> savedSubMros = new ArrayList<Object>(); + List<Object> savedSubMros = Generic.list(); try { bases = newBases; base = newBase; @@ -695,7 +696,7 @@ } } - private static void fill_classic_mro(ArrayList<PyObject> acc, PyClass classic_cl) { + private static void fill_classic_mro(List<PyObject> acc, PyClass classic_cl) { if (!acc.contains(classic_cl)) { acc.add(classic_cl); } @@ -706,7 +707,7 @@ } private static PyObject[] classic_mro(PyClass classic_cl) { - ArrayList<PyObject> acc = new ArrayList<PyObject>(); + List<PyObject> acc = Generic.list(); fill_classic_mro(acc, classic_cl); return acc.toArray(new PyObject[acc.size()]); } @@ -785,7 +786,7 @@ to_merge[n] = bases; remain[n] = 0; - ArrayList<PyObject> acc = new ArrayList<PyObject>(); + List<PyObject> acc = Generic.list(); acc.add(this); int empty_cnt = 0; @@ -1050,7 +1051,7 @@ public static void addBuilder(Class<?> forClass, TypeBuilder builder) { if (classToBuilder == null) { - classToBuilder = new HashMap<Class<?>, TypeBuilder>(); + classToBuilder = Generic.map(); } classToBuilder.put(forClass, builder); @@ -1099,7 +1100,7 @@ public static synchronized PyType fromClass(Class<?> c) { if (class_to_type == null) { - class_to_type = new HashMap<Class<?>, PyType>(); + class_to_type = Generic.map(); addFromClass(PyType.class); } PyType type = class_to_type.get(c); Modified: trunk/jython/src/org/python/core/PyUnicode.java =================================================================== --- trunk/jython/src/org/python/core/PyUnicode.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/core/PyUnicode.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -1,18 +1,18 @@ package org.python.core; -import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + import org.python.expose.ExposedMethod; import org.python.expose.ExposedNew; import org.python.expose.ExposedType; import org.python.expose.MethodType; import org.python.modules._codecs; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Set; +import org.python.util.Generic; /** * a builtin python unicode string. @@ -39,9 +39,9 @@ public PyUnicode(String string, boolean isBasic) { this(TYPE, string); - plane = isBasic ? Plane.BASIC : Plane.UNKNOWN; + plane = isBasic ? Plane.BASIC : Plane.UNKNOWN; } - + public PyUnicode(PyType subtype, String string) { super(subtype, string); } @@ -77,7 +77,7 @@ } return buffer; } - + PyUnicode(Iterator<Integer> iter) { this(fromCodePoints(iter)); } @@ -96,18 +96,18 @@ } return codePoints; } - + // modified to know something about codepoints; we just need to return the // corresponding substring; darn UTF16! // TODO: we could avoid doing this unnecessary copy @Override public String substring(int start, int end) { if (isBasicPlane()) { - return super.substring(start, end); + return super.substring(start, end); } return new PyUnicode(newSubsequenceIterator(start, end, 1)).string; } - + /** * Creates a PyUnicode from an already interned String. Just means it won't * be reinterned if used in a place that requires interned Strings. @@ -132,9 +132,9 @@ // public boolean isBasicPlane() { // return false; // } - + // END RETAIN - + public int getCodePointCount() { if (codePointCount >= 0) { return codePointCount; @@ -201,7 +201,7 @@ protected PyString createInstance(String str, boolean isBasic) { return new PyUnicode(str, isBasic); } - + @Override public PyObject __mod__(PyObject other) { return unicode___mod__(other); @@ -304,7 +304,6 @@ while (i > 0) { int W1 = string.charAt(k); if (W1 >= 0xD800 && W1 < 0xDC00) { - int W2 = string.charAt(k + 1); k += 2; } else { k += 1; @@ -524,7 +523,7 @@ public StripIterator(PyUnicode sep, Iterator<Integer> iter) { this.iter = iter; if (sep != null) { - Set<Integer> sepSet = new HashSet<Integer>(); + Set<Integer> sepSet = Generic.set(); for (Iterator<Integer> sepIter = sep.newSubsequenceIterator(); sepIter.hasNext();) { sepSet.add(sepIter.next()); } @@ -732,7 +731,7 @@ private class ReversedIterator<T> implements Iterator { - private final List<T> reversed = new ArrayList<T>(); + private final List<T> reversed = Generic.list(); private final Iterator<T> iter; ReversedIterator(Iterator<T> iter) { @@ -807,7 +806,7 @@ super(maxsplit); this.sep = sep; } - + public PyUnicode next() { StringBuilder buffer = new StringBuilder(); @@ -857,7 +856,7 @@ return new SepSplitIterator(sep, maxsplit); } } - + @ExposedMethod final PyTuple unicode_rpartition(PyObject sep) { return unicodeRpartition(sep); @@ -882,7 +881,7 @@ return str_rsplit(null, maxsplit); } } - + @ExposedMethod(defaults = "false") final PyList unicode_splitlines(boolean keepends) { if (isBasicPlane()) { @@ -957,8 +956,8 @@ throw Py.TypeError(function + "() argument 2 must be char, not str"); } return fillchar.codePointAt(0); - } - + } + @ExposedMethod(defaults="null") final PyObject unicode_ljust(int width, String padding) { int n = width - getCodePointCount(); @@ -1065,20 +1064,20 @@ if (isBasicPlane() && newPiece.isBasicPlane() && oldPiece.isBasicPlane()) { return replace(oldPiece, newPiece, maxsplit); } - + StringBuilder buffer = new StringBuilder(); - + if (oldPiece.getCodePointCount() == 0) { Iterator<Integer> iter = newSubsequenceIterator(); for (int i = 1; (maxsplit == -1 || i < maxsplit) && iter.hasNext(); i++) { if (i == 1) { buffer.append(newPiece.string); } - buffer.appendCodePoint((Integer) iter.next()); + buffer.appendCodePoint(iter.next()); buffer.append(newPiece.string); } while (iter.hasNext()) { - buffer.appendCodePoint((Integer) iter.next()); + buffer.appendCodePoint(iter.next()); } return new PyUnicode(buffer); } else { @@ -1311,7 +1310,7 @@ public Iterator<Integer> iterator() { return newSubsequenceIterator(); } - + @ExposedMethod final String unicode_toString() { return toString(); Modified: trunk/jython/src/org/python/expose/generate/ExposeTask.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposeTask.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/expose/generate/ExposeTask.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -4,7 +4,6 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; -import java.util.HashSet; import java.util.Set; import org.apache.tools.ant.BuildException; @@ -13,6 +12,7 @@ import org.apache.tools.ant.util.GlobPatternMapper; import org.apache.tools.ant.util.SourceFileScanner; import org.python.objectweb.asm.ClassWriter; +import org.python.util.Generic; public class ExposeTask extends MatchingTask { @@ -20,7 +20,7 @@ private File destDir; - private Set<File> toExpose = new HashSet<File>(); + private Set<File> toExpose = Generic.set(); /** * Set the source directories to find the class files to be exposed. @@ -42,7 +42,7 @@ /** * Set the destination directory into which the Java source files should be compiled. - * + * * @param destDir * the destination director */ @@ -52,7 +52,7 @@ /** * Gets the destination directory into which the java source files should be compiled. - * + * * @return the destination directory */ public File getDestdir() { Modified: trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/expose/generate/ExposedMethodFinder.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -1,14 +1,14 @@ package org.python.expose.generate; -import java.util.ArrayList; import java.util.List; +import org.python.expose.MethodType; import org.python.objectweb.asm.AnnotationVisitor; import org.python.objectweb.asm.MethodAdapter; import org.python.objectweb.asm.MethodVisitor; import org.python.objectweb.asm.Opcodes; import org.python.objectweb.asm.Type; -import org.python.expose.MethodType; +import org.python.util.Generic; /** * Visits a method passing all calls through to its delegate. If an ExposedNew or ExposedMethod @@ -131,7 +131,7 @@ public abstract void handleResult(String[] result); - List<String> vals = new ArrayList<String>(); + List<String> vals = Generic.list(); } class ExposedMethodVisitor extends RestrictiveAnnotationVisitor { Modified: trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java =================================================================== --- trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/expose/generate/ExposedTypeProcessor.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -2,9 +2,7 @@ import java.io.IOException; import java.io.InputStream; -import java.util.ArrayList; import java.util.Collection; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -19,6 +17,7 @@ import org.python.objectweb.asm.MethodVisitor; import org.python.objectweb.asm.Opcodes; import org.python.objectweb.asm.Type; +import org.python.util.Generic; /** * Processes the bytecode of a Java class that has the {@link ExposedType} annotation on it and @@ -26,9 +25,9 @@ */ public class ExposedTypeProcessor implements Opcodes, PyTypes { - private List<MethodExposer> methodExposers = new ArrayList<MethodExposer>(); + private List<MethodExposer> methodExposers = Generic.list(); - private Map<String, DescriptorExposer> descExposers = new HashMap<String, DescriptorExposer>(); + private Map<String, DescriptorExposer> descExposers = Generic.map(); private Exposer newExposer; @@ -276,7 +275,7 @@ @Override public void handleResult(ClassMethodExposer exposer) { methodExposers.add(exposer); - + } }; } Modified: trunk/jython/src/org/python/expose/generate/TypeExposer.java =================================================================== --- trunk/jython/src/org/python/expose/generate/TypeExposer.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/expose/generate/TypeExposer.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -1,14 +1,14 @@ package org.python.expose.generate; import java.util.Collection; -import java.util.HashSet; import java.util.Set; -import org.python.objectweb.asm.Type; import org.python.core.BytecodeLoader; import org.python.expose.BaseTypeBuilder; import org.python.expose.ExposedType; import org.python.expose.TypeBuilder; +import org.python.objectweb.asm.Type; +import org.python.util.Generic; /** * Generates a subclass of TypeBuilder to expose a class with the {@link ExposedType} annotation as @@ -42,7 +42,7 @@ this.name = name; this.methods = methods; this.descriptors = descriptors; - Set<String> names = new HashSet<String>(); + Set<String> names = Generic.set(); for(DescriptorExposer exposer : descriptors) { if(!names.add(exposer.getName())) { throwDupe(exposer.getName()); Modified: trunk/jython/src/org/python/modules/_functools/PyPartial.java =================================================================== --- trunk/jython/src/org/python/modules/_functools/PyPartial.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/modules/_functools/PyPartial.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -1,14 +1,12 @@ /* Copyright (c) Jython Developers */ package org.python.modules._functools; -import java.util.HashMap; import java.util.Map; import org.python.core.Py; import org.python.core.PyDictionary; import org.python.core.PyNewWrapper; import org.python.core.PyObject; -import org.python.core.PyString; import org.python.core.PyStringMap; import org.python.core.PyTuple; import org.python.core.PyType; @@ -17,6 +15,7 @@ import org.python.expose.ExposedNew; import org.python.expose.ExposedSet; import org.python.expose.ExposedType; +import org.python.util.Generic; @ExposedType(name = "_functools.partial") public class PyPartial extends PyObject { @@ -93,7 +92,7 @@ kwAppl = this.keywords; } else { // first merge keywords to determine the keyword count - HashMap<String, PyObject> merged = new HashMap<String, PyObject>(); + Map<String, PyObject> merged = Generic.map(); int i; for (i = 0; i < this.keywords.length; i++) { String keyword = this.keywords[i]; @@ -149,12 +148,14 @@ return kwDict; } + @Override @ExposedGet(name = "__dict__") public PyObject getDict() { ensureDict(); return __dict__; } + @Override @ExposedSet(name = "__dict__") public void setDict(PyObject val) { if (!(val instanceof PyStringMap) && !(val instanceof PyDictionary)) { Added: trunk/jython/src/org/python/util/Generic.java =================================================================== --- trunk/jython/src/org/python/util/Generic.java (rev 0) +++ trunk/jython/src/org/python/util/Generic.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -0,0 +1,37 @@ +package org.python.util; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * Static methods to make instances of collections with their generic types inferred from what + * they're being assigned to. The idea is stolen from <code>Sets</code>, <code>Lists</code> and + * <code>Maps</code> from <a href="http://code.google.com/p/google-collections/">Google + * Collections</a>. + */ +public class Generic { + /** + * Makes a List with its generic type inferred from whatever its being assigned to. + */ + public static <T> List<T> list() { + return new ArrayList<T>(); + } + + /** + * Makes a Map using generic types inferred from whatever this is being assigned to. + */ + public static <K, V> Map<K, V> map() { + return new HashMap<K, V>(); + } + + /** + * Makes a Set using the generic type inferred from whatever this is being assigned to. + */ + public static <T> Set<T> set() { + return new HashSet<T>(); + } +} Modified: trunk/jython/src/org/python/util/NameUnionAntType.java =================================================================== --- trunk/jython/src/org/python/util/NameUnionAntType.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/util/NameUnionAntType.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -20,7 +20,7 @@ protected Collection<Resource> getCollection() { List<ResourceCollection> collections = getResourceCollections(); // preserve order-encountered using a list; keep track of the items with a set - Set<String> seenNames = new HashSet<String>(); + Set<String> seenNames = Generic.set(); List<Resource> union = new ArrayList(); for (ResourceCollection rc : collections) { for (Iterator<Resource> resources = rc.iterator(); resources.hasNext();) { Modified: trunk/jython/src/org/python/util/jython.java =================================================================== --- trunk/jython/src/org/python/util/jython.java 2008-10-20 02:03:44 UTC (rev 5478) +++ trunk/jython/src/org/python/util/jython.java 2008-10-20 06:29:50 UTC (rev 5479) @@ -6,8 +6,6 @@ import java.io.FileInputStream; import java.io.InputStream; import java.nio.charset.Charset; -import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -32,10 +30,10 @@ private static final String COPYRIGHT = "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information."; - static final String usageHeader = + static final String usageHeader = "usage: jython [option] ... [-c cmd | -m mod | file | -] [arg] ...\n"; - private static final String usage = usageHeader + + private static final String usage = usageHeader + "Options and arguments:\n" + //(and corresponding environment variables):\n" + "-c cmd : program passed in as string (terminates option list)\n" + //"-d : debug output from parser (also PYTHONDEBUG=x)\n" + @@ -84,15 +82,15 @@ throw Py.ValueError("jar file missing '__run__.py'"); PyStringMap locals = new PyStringMap(); - - // Stripping the stuff before the last File.separator fixes Bug - // #931129 by keeping illegal characters out of the generated - // proxy class name + + // Stripping the stuff before the last File.separator fixes Bug + // #931129 by keeping illegal characters out of the generated + // proxy class name int beginIndex; if ((beginIndex = filename.lastIndexOf(File.separator)) != -1) { filename = filename.substring(beginIndex + 1); } - + locals.__setitem__("__name__", new PyString(filename)); locals.__setitem__("zipfile", Py.java2py(zip)); @@ -153,7 +151,7 @@ systemState.ps1 = systemState.ps2 = new PyString(); } } - + // Print banner and copyright information (or not) if (opts.interactive && opts.notice && !opts.runModule) { System.err.println(InteractiveConsole.getDefaultBanner()); @@ -331,13 +329,13 @@ { public String filename; public boolean jar, interactive, notice; - public boolean runModule; + public boolean runModule; public boolean fixInteractive; public boolean help, version; public String[] argv; public java.util.Properties properties; public String command; - public List<String> warnoptions = new ArrayList<String>(); + public List<String> warnoptions = Generic.list(); public String encoding; public String division; public String moduleName; @@ -455,7 +453,7 @@ interactive = false; } - index++; + index++; int n = args.length-index+1; argv = new String[n]; argv[0] = moduleName; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |