8080842: Using Lambda Expression with name clash results in ClassFormatError

Fri, 29 May 2015 10:15:36 +0530

author
sadayapalam
date
Fri, 29 May 2015 10:15:36 +0530
changeset 2812
9ec429ab0e7e
parent 2811
610ec7dcf431
child 2813
d94fe2d29b1e

8080842: Using Lambda Expression with name clash results in ClassFormatError
Summary: Ensure ScopeImpl can cope properly with remove when a field and method share the name
Reviewed-by: mcimadamore, jlahoda

src/share/classes/com/sun/tools/javac/code/Scope.java file | annotate | diff | comparison | revisions
test/tools/javac/scope/RemoveSymbolTest.java file | annotate | diff | comparison | revisions
test/tools/javac/scope/RemoveSymbolUnitTest.java file | annotate | diff | comparison | revisions
     1.1 --- a/src/share/classes/com/sun/tools/javac/code/Scope.java	Mon May 11 13:28:14 2015 +0530
     1.2 +++ b/src/share/classes/com/sun/tools/javac/code/Scope.java	Fri May 29 10:15:36 2015 +0530
     1.3 @@ -1,5 +1,5 @@
     1.4  /*
     1.5 - * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * Copyright (c) 1999, 2015, Oracle and/or its affiliates. All rights reserved.
     1.7   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.8   *
     1.9   * This code is free software; you can redistribute it and/or modify it
    1.10 @@ -241,12 +241,16 @@
    1.11          listeners = listeners.prepend(sl);
    1.12      }
    1.13  
    1.14 -    /** Remove symbol from this scope.  Used when an inner class
    1.15 -     *  attribute tells us that the class isn't a package member.
    1.16 +    /** Remove symbol from this scope.
    1.17       */
    1.18 -    public void remove(Symbol sym) {
    1.19 +    public void remove(final Symbol sym) {
    1.20          Assert.check(shared == 0);
    1.21 -        Entry e = lookup(sym.name);
    1.22 +        Entry e = lookup(sym.name, new Filter<Symbol>() {
    1.23 +            @Override
    1.24 +            public boolean accepts(Symbol candidate) {
    1.25 +                return candidate == sym;
    1.26 +            }
    1.27 +        });
    1.28          if (e.scope == null) return;
    1.29  
    1.30          // remove e from table and shadowed list;
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/tools/javac/scope/RemoveSymbolTest.java	Fri May 29 10:15:36 2015 +0530
     2.3 @@ -0,0 +1,77 @@
     2.4 +/*
     2.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     2.7 + *
     2.8 + * This code is free software; you can redistribute it and/or modify it
     2.9 + * under the terms of the GNU General Public License version 2 only, as
    2.10 + * published by the Free Software Foundation.
    2.11 + *
    2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    2.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    2.15 + * version 2 for more details (a copy is included in the LICENSE file that
    2.16 + * accompanied this code).
    2.17 + *
    2.18 + * You should have received a copy of the GNU General Public License version
    2.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    2.21 + *
    2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    2.23 + * or visit www.oracle.com if you need additional information or have any
    2.24 + * questions.
    2.25 + */
    2.26 +
    2.27 +/*
    2.28 + * @test
    2.29 + * @bug 8080842
    2.30 + * @summary Ensure Scope impl can cope with remove() when a field and method share the name.
    2.31 + * @run main RemoveSymbolTest
    2.32 + */
    2.33 +
    2.34 +import java.util.Iterator;
    2.35 +import java.util.LinkedList;
    2.36 +
    2.37 +public class RemoveSymbolTest<W> implements Iterable<W> {
    2.38 +    static class Widget {
    2.39 +        private String name;
    2.40 +        Widget(String s) { name = s; }
    2.41 +        @Override public String toString() { return name; }
    2.42 +    }
    2.43 +
    2.44 +    private LinkedList<W> data;
    2.45 +    // Instantiate an Iterable instance using a Lambda expression.
    2.46 +    // Causes ClassFormatError if a local variable of type Widget is named after one of the methods.
    2.47 +    private final Iterable<W> myIterator1 = () -> new Iterator<W>() {
    2.48 +        private W hasNext = null;
    2.49 +        private int index = 0;
    2.50 +        @Override public boolean hasNext() { return index < data.size(); }
    2.51 +        @Override public W next() { return data.get(index++); }
    2.52 +    };
    2.53 +
    2.54 +    // Instantiate an Iterable instance using an anonymous class.
    2.55 +    // Always works fine regardless of the name of the local variable.
    2.56 +    private final Iterable<W> myIterator2 =
    2.57 +        new Iterable<W>() {
    2.58 +        @Override
    2.59 +        public Iterator<W> iterator() {
    2.60 +            return new Iterator<W>() {
    2.61 +                private W hasNext = null;
    2.62 +                private int index = 0;
    2.63 +                @Override public boolean hasNext() { return index < data.size(); }
    2.64 +                @Override public W next() { return data.get(index++); }
    2.65 +            };
    2.66 +        }
    2.67 +    };
    2.68 +    public RemoveSymbolTest() { data = new LinkedList<>(); }
    2.69 +    public void add(W e) { data.add(e); }
    2.70 +    @Override public String toString() { return data.toString(); }
    2.71 +    @Override public Iterator<W> iterator() { return myIterator1.iterator(); }
    2.72 +    public static void main(String[] args) {
    2.73 +        RemoveSymbolTest<Widget> widgets = new RemoveSymbolTest<>();
    2.74 +        widgets.add(new Widget("W1"));
    2.75 +        widgets.add(new Widget("W2"));
    2.76 +        widgets.add(new Widget("W3"));
    2.77 +        System.out.println(".foreach() call: ");
    2.78 +        widgets.forEach(w -> System.out.println(w + " "));
    2.79 +    }
    2.80 +}
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/test/tools/javac/scope/RemoveSymbolUnitTest.java	Fri May 29 10:15:36 2015 +0530
     3.3 @@ -0,0 +1,95 @@
     3.4 +/*
     3.5 + * Copyright (c) 2015 Oracle and/or its affiliates. All rights reserved.
     3.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3.7 + *
     3.8 + * This code is free software; you can redistribute it and/or modify it
     3.9 + * under the terms of the GNU General Public License version 2 only, as
    3.10 + * published by the Free Software Foundation.
    3.11 + *
    3.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    3.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    3.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    3.15 + * version 2 for more details (a copy is included in the LICENSE file that
    3.16 + * accompanied this code).
    3.17 + *
    3.18 + * You should have received a copy of the GNU General Public License version
    3.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    3.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    3.21 + *
    3.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    3.23 + * or visit www.oracle.com if you need additional information or have any
    3.24 + * questions.
    3.25 + */
    3.26 +
    3.27 +/*
    3.28 + * @test
    3.29 + * @bug 8080842
    3.30 + * @summary Ensure Scope impl can cope with remove() when a field and method share the name.
    3.31 + */
    3.32 +
    3.33 +import com.sun.tools.javac.util.*;
    3.34 +import com.sun.tools.javac.code.*;
    3.35 +import com.sun.tools.javac.code.Scope.*;
    3.36 +import com.sun.tools.javac.code.Symbol.*;
    3.37 +import com.sun.tools.javac.file.JavacFileManager;
    3.38 +
    3.39 +public class RemoveSymbolUnitTest {
    3.40 +
    3.41 +    Context context;
    3.42 +    Names names;
    3.43 +    Symtab symtab;
    3.44 +
    3.45 +    public static void main(String... args) throws Exception {
    3.46 +        new RemoveSymbolUnitTest().run();
    3.47 +    }
    3.48 +
    3.49 +    public void run() {
    3.50 +        context = new Context();
    3.51 +        JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
    3.52 +        names = Names.instance(context);
    3.53 +        symtab = Symtab.instance(context);
    3.54 +
    3.55 +        Name hasNext =  names.fromString("hasNext");
    3.56 +        ClassSymbol clazz = new ClassSymbol(0,
    3.57 +                                            names.fromString("X"),
    3.58 +                                            Type.noType,
    3.59 +                                            symtab.unnamedPackage);
    3.60 +
    3.61 +        VarSymbol v = new VarSymbol(0, hasNext, Type.noType, clazz);
    3.62 +        MethodSymbol m = new MethodSymbol(0, hasNext, Type.noType, clazz);
    3.63 +
    3.64 +        // Try enter and remove in different shuffled combinations.
    3.65 +        // working with fresh scope each time.
    3.66 +        Scope cs = new Scope(clazz);
    3.67 +        cs.enter(v);
    3.68 +        cs.enter(m);
    3.69 +        cs.remove(v);
    3.70 +        Symbol s = cs.lookup(hasNext).sym;
    3.71 +        if (s != m)
    3.72 +            throw new AssertionError("Wrong symbol");
    3.73 +
    3.74 +        cs = new Scope(clazz);
    3.75 +        cs.enter(m);
    3.76 +        cs.enter(v);
    3.77 +        cs.remove(v);
    3.78 +        s = cs.lookup(hasNext).sym;
    3.79 +        if (s != m)
    3.80 +            throw new AssertionError("Wrong symbol");
    3.81 +
    3.82 +        cs = new Scope(clazz);
    3.83 +        cs.enter(v);
    3.84 +        cs.enter(m);
    3.85 +        cs.remove(m);
    3.86 +        s = cs.lookup(hasNext).sym;
    3.87 +        if (s != v)
    3.88 +            throw new AssertionError("Wrong symbol");
    3.89 +
    3.90 +        cs = new Scope(clazz);
    3.91 +        cs.enter(m);
    3.92 +        cs.enter(v);
    3.93 +        cs.remove(m);
    3.94 +        s = cs.lookup(hasNext).sym;
    3.95 +        if (s != v)
    3.96 +            throw new AssertionError("Wrong symbol");
    3.97 +    }
    3.98 +}

mercurial