1010 try (Writer out = Files.newBufferedWriter(file)) { |
1011 try (Writer out = Files.newBufferedWriter(file)) { |
1011 out.append(content); |
1012 out.append(content); |
1012 } |
1013 } |
1013 } |
1014 } |
1014 |
1015 |
|
1016 @Test |
|
1017 public void testUnboundLookup(Path base) throws Exception { |
|
1018 Path src = base.resolve("src"); |
|
1019 |
|
1020 tb.writeJavaFiles(src, |
|
1021 "package impl.conflict.src; public class Impl { }"); |
|
1022 |
|
1023 Path moduleSrc = base.resolve("module-src"); |
|
1024 Path m1 = moduleSrc.resolve("m1x"); |
|
1025 Path m2 = moduleSrc.resolve("m2x"); |
|
1026 |
|
1027 Path classes = base.resolve("classes"); |
|
1028 Path cpClasses = base.resolve("cpClasses"); |
|
1029 |
|
1030 Files.createDirectories(classes); |
|
1031 Files.createDirectories(cpClasses); |
|
1032 |
|
1033 tb.writeJavaFiles(m1, |
|
1034 "module m1x { }", |
|
1035 "package impl1; public class Impl { }", |
|
1036 "package impl.conflict.module; class Impl { }", |
|
1037 "package impl.conflict.clazz; public class pkg { public static class I { } }", |
|
1038 "package impl.conflict.src; public class Impl { }"); |
|
1039 |
|
1040 tb.writeJavaFiles(m2, |
|
1041 "module m2x { }", |
|
1042 "package impl2; public class Impl { }", |
|
1043 "package impl.conflict.module; class Impl { }", |
|
1044 "package impl.conflict; public class clazz { public static class pkg { } }"); |
|
1045 |
|
1046 //from source: |
|
1047 new JavacTask(tb) |
|
1048 .options("--module-source-path", moduleSrc.toString(), |
|
1049 "--source-path", src.toString(), |
|
1050 "-processorpath", System.getProperty("test.class.path"), |
|
1051 "-processor", UnboundLookup.class.getName()) |
|
1052 .outdir(classes) |
|
1053 .files(findJavaFiles(moduleSrc)) |
|
1054 .run() |
|
1055 .writeAll(); |
|
1056 |
|
1057 new JavacTask(tb) |
|
1058 .options("--source-path", src.toString()) |
|
1059 .outdir(cpClasses) |
|
1060 .files(findJavaFiles(src)) |
|
1061 .run() |
|
1062 .writeAll(); |
|
1063 |
|
1064 //from classfiles: |
|
1065 new JavacTask(tb) |
|
1066 .options("--module-path", classes.toString(), |
|
1067 "--class-path", cpClasses.toString(), |
|
1068 "--add-modules", "m1x,m2x", |
|
1069 "-processorpath", System.getProperty("test.class.path"), |
|
1070 "-processor", UnboundLookup.class.getName(), |
|
1071 "-proc:only") |
|
1072 .classes("java.lang.Object") |
|
1073 .run() |
|
1074 .writeAll(); |
|
1075 |
|
1076 //source 8: |
|
1077 new JavacTask(tb) |
|
1078 .options("--source-path", src.toString(), |
|
1079 "-source", "8", |
|
1080 "-processorpath", System.getProperty("test.class.path"), |
|
1081 "-processor", UnboundLookup8.class.getName()) |
|
1082 .outdir(cpClasses) |
|
1083 .files(findJavaFiles(src)) |
|
1084 .run() |
|
1085 .writeAll(); |
|
1086 |
|
1087 } |
|
1088 |
|
1089 @SupportedAnnotationTypes("*") |
|
1090 public static final class UnboundLookup extends AbstractProcessor { |
|
1091 |
|
1092 @Override |
|
1093 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
1094 assertTypeElementExists("impl1.Impl", "m1x"); |
|
1095 assertPackageElementExists("impl1", "m1x"); |
|
1096 assertTypeElementExists("impl2.Impl", "m2x"); |
|
1097 assertTypeElementExists("impl.conflict.clazz.pkg.I", "m1x"); |
|
1098 assertTypeElementExists("impl.conflict.clazz", "m2x"); |
|
1099 assertPackageElementExists("impl.conflict.clazz", "m1x"); |
|
1100 assertPackageElementExists("impl2", "m2x"); |
|
1101 assertTypeElementNotFound("impl.conflict.module.Impl"); |
|
1102 assertPackageElementNotFound("impl.conflict.module"); |
|
1103 assertTypeElementNotFound("impl.conflict.src.Impl"); |
|
1104 assertPackageElementNotFound("impl.conflict.src"); |
|
1105 assertTypeElementNotFound("impl.conflict.clazz.pkg"); |
|
1106 |
|
1107 return false; |
|
1108 } |
|
1109 |
|
1110 private void assertTypeElementExists(String name, String expectedModule) { |
|
1111 assertElementExists(name, "class", processingEnv.getElementUtils() :: getTypeElement, expectedModule); |
|
1112 } |
|
1113 |
|
1114 private void assertPackageElementExists(String name, String expectedModule) { |
|
1115 assertElementExists(name, "package", processingEnv.getElementUtils() :: getPackageElement, expectedModule); |
|
1116 } |
|
1117 |
|
1118 private void assertElementExists(String name, String type, Function<String, Element> getter, String expectedModule) { |
|
1119 Element clazz = getter.apply(name); |
|
1120 |
|
1121 if (clazz == null) { |
|
1122 throw new AssertionError("No " + name + " " + type + " found."); |
|
1123 } |
|
1124 |
|
1125 ModuleElement mod = processingEnv.getElementUtils().getModuleOf(clazz); |
|
1126 |
|
1127 if (!mod.getQualifiedName().contentEquals(expectedModule)) { |
|
1128 throw new AssertionError(name + " found in an unexpected module: " + mod.getQualifiedName()); |
|
1129 } |
|
1130 } |
|
1131 |
|
1132 private void assertTypeElementNotFound(String name) { |
|
1133 assertElementNotFound(name, processingEnv.getElementUtils() :: getTypeElement); |
|
1134 } |
|
1135 |
|
1136 private void assertPackageElementNotFound(String name) { |
|
1137 assertElementNotFound(name, processingEnv.getElementUtils() :: getPackageElement); |
|
1138 } |
|
1139 |
|
1140 private void assertElementNotFound(String name, Function<String, Element> getter) { |
|
1141 Element found = getter.apply(name); |
|
1142 |
|
1143 if (found != null) { |
|
1144 fail("Element found unexpectedly: " + found); |
|
1145 } |
|
1146 } |
|
1147 |
|
1148 @Override |
|
1149 public SourceVersion getSupportedSourceVersion() { |
|
1150 return SourceVersion.latest(); |
|
1151 } |
|
1152 |
|
1153 } |
|
1154 |
|
1155 @SupportedAnnotationTypes("*") |
|
1156 public static final class UnboundLookup8 extends AbstractProcessor { |
|
1157 |
|
1158 @Override |
|
1159 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
|
1160 if (processingEnv.getElementUtils().getTypeElement("impl.conflict.src.Impl") == null) { |
|
1161 throw new AssertionError("impl.conflict.src.Impl."); |
|
1162 } |
|
1163 |
|
1164 if (processingEnv.getElementUtils().getModuleElement("java.base") != null) { |
|
1165 throw new AssertionError("getModuleElement != null for -source 8"); |
|
1166 } |
|
1167 |
|
1168 return false; |
|
1169 } |
|
1170 |
|
1171 @Override |
|
1172 public SourceVersion getSupportedSourceVersion() { |
|
1173 return SourceVersion.latest(); |
|
1174 } |
|
1175 |
|
1176 } |
|
1177 |
1015 private static void assertNonNull(String msg, Object val) { |
1178 private static void assertNonNull(String msg, Object val) { |
1016 if (val == null) { |
1179 if (val == null) { |
1017 throw new AssertionError(msg); |
1180 throw new AssertionError(msg); |
1018 } |
1181 } |
1019 } |
1182 } |