langtools/test/tools/javac/flow/LVTHarness.java
author vromero
Fri, 27 Jun 2014 20:32:12 +0100
changeset 25298 7940437438b9
parent 24648 3b639cd0c8bd
child 26784 6be305e56bcb
permissions -rw-r--r--
8047719: Incorrect LVT in switch statement Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     1
/*
24648
3b639cd0c8bd 8037937: javac: AssertionError during LVT generation, wrong variable ranges
vromero
parents: 21703
diff changeset
     2
 * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     4
 *
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     8
 *
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    13
 * accompanied this code).
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    14
 *
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    18
 *
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    21
 * questions.
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    22
 */
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    23
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    24
/*
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    25
 * @test
25298
7940437438b9 8047719: Incorrect LVT in switch statement
vromero
parents: 24648
diff changeset
    26
 * @bug 7047734 8027660 8037937 8047719
24648
3b639cd0c8bd 8037937: javac: AssertionError during LVT generation, wrong variable ranges
vromero
parents: 21703
diff changeset
    27
 * @summary The LVT is not generated correctly during some try/catch scenarios
21703
b12ce201d445 8027660: javac crash while creating LVT entry for a local variable defined in an inner block
vromero
parents: 20248
diff changeset
    28
 *          javac crash while creating LVT entry for a local variable defined in
b12ce201d445 8027660: javac crash while creating LVT entry for a local variable defined in an inner block
vromero
parents: 20248
diff changeset
    29
 *          an inner block
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    30
 * @library /tools/javac/lib
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    31
 * @build JavacTestingAbstractProcessor LVTHarness
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    32
 * @run main LVTHarness
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    33
 */
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    34
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    35
import java.io.File;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    36
import java.io.IOException;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    37
import java.lang.annotation.Annotation;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    38
import java.util.Set;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    39
import java.util.Arrays;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    40
import java.util.ArrayList;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    41
import java.util.Collections;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    42
import java.util.HashMap;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    43
import java.util.HashSet;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    44
import java.util.List;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    45
import java.util.Map;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    46
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    47
import javax.annotation.processing.RoundEnvironment;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    48
import javax.lang.model.element.Element;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    49
import javax.lang.model.element.TypeElement;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    50
import javax.tools.JavaCompiler;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    51
import javax.tools.JavaFileObject;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    52
import javax.tools.StandardJavaFileManager;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    53
import javax.tools.ToolProvider;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    54
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    55
import com.sun.source.util.JavacTask;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    56
import com.sun.tools.classfile.Attribute;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    57
import com.sun.tools.classfile.ClassFile;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    58
import com.sun.tools.classfile.ConstantPool;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    59
import com.sun.tools.classfile.ConstantPoolException;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    60
import com.sun.tools.classfile.Code_attribute;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    61
import com.sun.tools.classfile.ConstantPool.InvalidIndex;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    62
import com.sun.tools.classfile.ConstantPool.UnexpectedEntry;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    63
import com.sun.tools.classfile.Descriptor.InvalidDescriptor;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    64
import com.sun.tools.classfile.LocalVariableTable_attribute;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    65
import com.sun.tools.classfile.Method;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    66
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    67
import static javax.tools.StandardLocation.*;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    68
import static com.sun.tools.classfile.LocalVariableTable_attribute.Entry;
20248
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    69
import static javax.tools.JavaFileObject.Kind.SOURCE;
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    70
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    71
public class LVTHarness {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    72
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    73
    static int nerrors = 0;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    74
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    75
    static final JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    76
    static final StandardJavaFileManager fm = comp.getStandardFileManager(null, null, null);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    77
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    78
    public static void main(String[] args) throws Exception {
20248
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    79
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    80
        String testDir = System.getProperty("test.src");
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    81
        fm.setLocation(SOURCE_PATH, Arrays.asList(new File(testDir, "tests")));
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    82
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    83
        // Make sure classes are written to scratch dir.
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    84
        fm.setLocation(CLASS_OUTPUT, Arrays.asList(new File(".")));
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    85
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    86
        for (JavaFileObject jfo : fm.list(SOURCE_PATH, "", Collections.singleton(SOURCE), true)) {
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    87
            new LVTHarness(jfo).check();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    88
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    89
        if (nerrors > 0) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    90
            throw new AssertionError("Errors were found");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    91
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    92
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    93
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    94
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    95
    JavaFileObject jfo;
20248
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
    96
    Map<ElementKey, AliveRanges> aliveRangeMap = new HashMap<>();
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    97
    Set<String> declaredKeys = new HashSet<>();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    98
    List<ElementKey> seenAliveRanges = new ArrayList<>();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
    99
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   100
    protected LVTHarness(JavaFileObject jfo) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   101
        this.jfo = jfo;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   102
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   103
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   104
    protected void check() throws Exception {
20248
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   105
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   106
        JavacTask ct = (JavacTask) comp.getTask(null, fm, null, Arrays.asList("-g"),
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   107
                                                null, Arrays.asList(jfo));
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   108
        System.err.println("compiling code " + jfo);
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   109
        ct.setProcessors(Collections.singleton(new AliveRangeFinder()));
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   110
        if (!ct.call()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   111
            throw new AssertionError("Error during compilation");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   112
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   113
20248
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   114
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   115
        File javaFile = new File(jfo.getName());
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   116
        File classFile = new File(javaFile.getName().replace(".java", ".class"));
0c72984b7a4d 8024988: javac, LVT test harness should generate tests .class files in the scratch folder
alundblad
parents: 19941
diff changeset
   117
        checkClassFile(classFile);
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   118
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   119
        //check all candidates have been used up
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   120
        for (Map.Entry<ElementKey, AliveRanges> entry : aliveRangeMap.entrySet()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   121
            if (!seenAliveRanges.contains(entry.getKey())) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   122
                error("Redundant @AliveRanges annotation on method " +
24648
3b639cd0c8bd 8037937: javac: AssertionError during LVT generation, wrong variable ranges
vromero
parents: 21703
diff changeset
   123
                        entry.getKey().elem + " with key " + entry.getKey());
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   124
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   125
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   126
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   127
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   128
    void checkClassFile(File file)
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   129
            throws IOException, ConstantPoolException, InvalidDescriptor {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   130
        ClassFile classFile = ClassFile.read(file);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   131
        ConstantPool constantPool = classFile.constant_pool;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   132
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   133
        //lets get all the methods in the class file.
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   134
        for (Method method : classFile.methods) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   135
            for (ElementKey elementKey: aliveRangeMap.keySet()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   136
                String methodDesc = method.getName(constantPool) +
24648
3b639cd0c8bd 8037937: javac: AssertionError during LVT generation, wrong variable ranges
vromero
parents: 21703
diff changeset
   137
                        method.descriptor.getParameterTypes(constantPool).replace(" ", "");
19941
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   138
                if (methodDesc.equals(elementKey.elem.toString())) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   139
                    checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   140
                    seenAliveRanges.add(elementKey);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   141
                }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   142
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   143
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   144
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   145
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   146
    void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   147
            throws InvalidIndex, UnexpectedEntry {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   148
        Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   149
        LocalVariableTable_attribute lvt =
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   150
            (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   151
        List<String> infoFromRanges = convertToStringList(ranges);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   152
        List<String> infoFromLVT = convertToStringList(constantPool, lvt);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   153
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   154
        // infoFromRanges most be contained in infoFromLVT
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   155
        int i = 0;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   156
        int j = 0;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   157
        while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   158
            int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   159
            if (comparison == 0) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   160
                i++; j++;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   161
            } else if (comparison > 0) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   162
                j++;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   163
            } else {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   164
                break;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   165
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   166
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   167
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   168
        if (i < infoFromRanges.size()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   169
            error(infoFromLVT, infoFromRanges);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   170
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   171
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   172
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   173
    List<String> convertToStringList(AliveRanges ranges) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   174
        List<String> result = new ArrayList<>();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   175
        for (Annotation anno : ranges.value()) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   176
            AliveRange range = (AliveRange)anno;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   177
            String str = formatLocalVariableData(range.varName(),
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   178
                    range.bytecodeStart(), range.bytecodeLength());
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   179
            result.add(str);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   180
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   181
        Collections.sort(result);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   182
        return result;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   183
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   184
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   185
    List<String> convertToStringList(ConstantPool constantPool,
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   186
            LocalVariableTable_attribute lvt) throws InvalidIndex, UnexpectedEntry {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   187
        List<String> result = new ArrayList<>();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   188
        for (Entry entry : lvt.local_variable_table) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   189
            String str = formatLocalVariableData(constantPool.getUTF8Value(entry.name_index),
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   190
                    entry.start_pc, entry.length);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   191
            result.add(str);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   192
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   193
        Collections.sort(result);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   194
        return result;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   195
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   196
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   197
    String formatLocalVariableData(String varName, int start, int length) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   198
        StringBuilder sb = new StringBuilder()
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   199
                    .append("var name: ").append(varName)
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   200
                    .append(" start: ").append(start)
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   201
                    .append(" length: ").append(length);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   202
        return sb.toString();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   203
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   204
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   205
    protected void error(List<String> infoFromLVT, List<String> infoFromRanges) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   206
        nerrors++;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   207
        System.err.printf("Error occurred while checking file: %s\n", jfo.getName());
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   208
        System.err.println("The range info from the annotations is");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   209
        printStringListToErrOutput(infoFromRanges);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   210
        System.err.println();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   211
        System.err.println("And the range info from the class file is");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   212
        printStringListToErrOutput(infoFromLVT);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   213
        System.err.println();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   214
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   215
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   216
    void printStringListToErrOutput(List<String> list) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   217
        for (String s : list) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   218
            System.err.println("\t" + s);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   219
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   220
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   221
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   222
    protected void error(String msg) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   223
        nerrors++;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   224
        System.err.printf("Error occurred while checking file: %s\nreason: %s\n",
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   225
                jfo.getName(), msg);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   226
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   227
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   228
    class AliveRangeFinder extends JavacTestingAbstractProcessor {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   229
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   230
        @Override
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   231
        public boolean process(Set<? extends TypeElement> annotations,
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   232
            RoundEnvironment roundEnv) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   233
            if (roundEnv.processingOver())
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   234
                return true;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   235
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   236
            TypeElement aliveRangeAnno = elements.getTypeElement("AliveRanges");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   237
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   238
            if (!annotations.contains(aliveRangeAnno)) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   239
                error("no @AliveRanges annotation found in test class");
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   240
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   241
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   242
            for (Element elem: roundEnv.getElementsAnnotatedWith(aliveRangeAnno)) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   243
                Annotation annotation = elem.getAnnotation(AliveRanges.class);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   244
                aliveRangeMap.put(new ElementKey(elem), (AliveRanges)annotation);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   245
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   246
            return true;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   247
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   248
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   249
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   250
    class ElementKey {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   251
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   252
        String key;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   253
        Element elem;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   254
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   255
        public ElementKey(Element elem) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   256
            this.elem = elem;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   257
            this.key = computeKey(elem);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   258
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   259
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   260
        @Override
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   261
        public boolean equals(Object obj) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   262
            if (obj instanceof ElementKey) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   263
                ElementKey other = (ElementKey)obj;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   264
                return other.key.equals(key);
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   265
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   266
            return false;
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   267
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   268
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   269
        @Override
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   270
        public int hashCode() {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   271
            return key.hashCode();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   272
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   273
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   274
        String computeKey(Element e) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   275
            StringBuilder buf = new StringBuilder();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   276
            while (e != null) {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   277
                buf.append(e.toString());
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   278
                e = e.getEnclosingElement();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   279
            }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   280
            buf.append(jfo.getName());
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   281
            return buf.toString();
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   282
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   283
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   284
        @Override
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   285
        public String toString() {
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   286
            return "Key{" + key + "}";
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   287
        }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   288
    }
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   289
8b91e8eb2d20 7047734: javac, the LVT is not generated correctly in several scenarios
vromero
parents:
diff changeset
   290
}