test/tools/javac/flow/LVTHarness.java

Mon, 23 Sep 2013 10:10:07 +0200

author
alundblad
date
Mon, 23 Sep 2013 10:10:07 +0200
changeset 2046
1fe358ea75ff
parent 2027
4932bb04c4b8
child 2178
cc80c03c41e4
permissions
-rw-r--r--

8024988: javac, LVT test harness should generate tests .class files in the scratch folder
Summary: Set the CLASS_OUTPUT location to the scratch directory. Changed the argument to checkClassFile accordingly.
Reviewed-by: jjg, vromero

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

mercurial