test/tools/javac/processing/model/element/TestNames.java

Wed, 02 Mar 2011 21:13:55 -0800

author
jjg
date
Wed, 02 Mar 2011 21:13:55 -0800
changeset 904
4baab658f357
parent 699
d2aaaec153e8
child 1466
b52a38d4536c
permissions
-rw-r--r--

6639645: Modeling type implementing missing interfaces
Reviewed-by: darcy, mcimadamore

duke@1 1 /*
darcy@699 2 * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
ohair@554 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 20 * or visit www.oracle.com if you need additional information or have any
ohair@554 21 * questions.
duke@1 22 */
duke@1 23
duke@1 24 /*
duke@1 25 * @test
duke@1 26 * @bug 6380016
duke@1 27 * @summary Test that the constraints guaranteed by the Filer and maintained
duke@1 28 * @author Joseph D. Darcy
darcy@699 29 * @library ../../../lib
darcy@699 30 * @build JavacTestingAbstractProcessor TestNames
duke@1 31 * @compile -processor TestNames -proc:only TestNames.java
duke@1 32 */
duke@1 33
duke@1 34 import java.util.Set;
duke@1 35 import javax.annotation.processing.*;
duke@1 36 import javax.lang.model.SourceVersion;
duke@1 37 import static javax.lang.model.SourceVersion.*;
duke@1 38 import javax.lang.model.element.*;
duke@1 39 import javax.lang.model.util.*;
duke@1 40 import static javax.lang.model.util.ElementFilter.*;
duke@1 41 import static javax.tools.Diagnostic.Kind.*;
duke@1 42 import static javax.tools.StandardLocation.*;
duke@1 43
duke@1 44 import java.io.*;
duke@1 45
duke@1 46 /**
duke@1 47 * Basic tests of semantics of javax.lang.model.element.Name
duke@1 48 */
darcy@699 49 public class TestNames extends JavacTestingAbstractProcessor {
duke@1 50 private int round = 0;
duke@1 51
duke@1 52 String stringStringName = "java.lang.String";
duke@1 53 Name stringName = null;
duke@1 54
duke@1 55 public boolean process(Set<? extends TypeElement> annotations,
duke@1 56 RoundEnvironment roundEnv) {
duke@1 57 round++;
duke@1 58 if (!roundEnv.processingOver()) {
duke@1 59 boolean failed = false;
duke@1 60
duke@1 61 switch(round) {
duke@1 62 case 1:
duke@1 63
duke@1 64 TypeElement stringMirror = eltUtils.getTypeElement(stringStringName);
duke@1 65 stringName = stringMirror.getQualifiedName();
duke@1 66 Name stringPseudoName = Pseudonym.getName(stringName.toString());
duke@1 67
duke@1 68
duke@1 69 if (stringName.equals(stringPseudoName))
duke@1 70 failed = true;
duke@1 71 if (!stringName.contentEquals(stringStringName))
duke@1 72 failed = true;
duke@1 73 if (!stringName.contentEquals(stringPseudoName))
duke@1 74 failed = true;
duke@1 75
duke@1 76
duke@1 77 try {
duke@1 78 // Force another round with a new context
duke@1 79 PrintWriter pw = new PrintWriter(filer.createSourceFile("Foo").openWriter());
duke@1 80 pw.println("public class Foo {}");
duke@1 81 pw.close();
duke@1 82 } catch (IOException ioe) {
duke@1 83 throw new RuntimeException();
duke@1 84 }
duke@1 85 break;
duke@1 86
duke@1 87 case 2:
duke@1 88 Name stringStringAsName = eltUtils.getName(stringStringName);
duke@1 89 TypeElement stringMirror2 = eltUtils.getTypeElement(stringStringName);
duke@1 90 Name stringName2 = stringMirror2.getQualifiedName();
duke@1 91
duke@1 92 if (stringStringAsName != stringName ||
duke@1 93 stringName != stringName2)
duke@1 94 failed = true;
duke@1 95 break;
duke@1 96
duke@1 97 default:
duke@1 98 throw new RuntimeException("Unexpected round " + round);
duke@1 99 }
duke@1 100
duke@1 101 if (failed)
duke@1 102 throw new RuntimeException("Invalid name equality checks.");
duke@1 103 }
duke@1 104 return true;
duke@1 105 }
duke@1 106
duke@1 107 private static class Pseudonym implements Name {
duke@1 108 private String name;
duke@1 109
duke@1 110 private Pseudonym(String name) {
duke@1 111 this.name = name;
duke@1 112 }
duke@1 113
duke@1 114 public static Pseudonym getName(String name) {
duke@1 115 return new Pseudonym(name);
duke@1 116 }
duke@1 117
duke@1 118 public boolean contentEquals(CharSequence cs) {
duke@1 119 return name.contentEquals(cs);
duke@1 120 }
duke@1 121
duke@1 122 public char charAt(int index) {
duke@1 123 return name.charAt(index);
duke@1 124 }
duke@1 125
duke@1 126 public int length() {
duke@1 127 return name.length();
duke@1 128 }
duke@1 129
duke@1 130 public CharSequence subSequence(int start, int end) {
duke@1 131 return name.subSequence(start, end);
duke@1 132 }
duke@1 133
duke@1 134 public String toString() {
duke@1 135 return name;
duke@1 136 }
duke@1 137 }
duke@1 138 }

mercurial