|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2024 IBM Corporation. |
| 3 | + * |
| 4 | + * This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: EPL-2.0 |
| 10 | + * |
| 11 | + * Contributors: |
| 12 | + * IBM Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | + |
| 15 | +package org.eclipse.jdt.compiler.apt.tests.processors.elements; |
| 16 | + |
| 17 | +import java.io.IOException; |
| 18 | +import java.lang.reflect.InvocationTargetException; |
| 19 | +import java.lang.reflect.Method; |
| 20 | + import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Map; |
| 23 | +import java.util.Set; |
| 24 | + |
| 25 | +import javax.annotation.processing.Filer; |
| 26 | +import javax.annotation.processing.Messager; |
| 27 | +import javax.annotation.processing.ProcessingEnvironment; |
| 28 | +import javax.annotation.processing.RoundEnvironment; |
| 29 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 30 | +import javax.annotation.processing.SupportedSourceVersion; |
| 31 | +import javax.lang.model.SourceVersion; |
| 32 | +import javax.lang.model.element.Element; |
| 33 | +import javax.lang.model.element.Modifier; |
| 34 | +import javax.lang.model.element.ModuleElement; |
| 35 | +import javax.lang.model.element.PackageElement; |
| 36 | +import javax.lang.model.element.TypeElement; |
| 37 | +import javax.lang.model.element.VariableElement; |
| 38 | + |
| 39 | +import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; |
| 40 | + |
| 41 | +/** |
| 42 | + * A processor that explores the java 13 specific elements and validates the lambda and |
| 43 | + * type annotated elements. To enable this processor, add |
| 44 | + * -Aorg.eclipse.jdt.compiler.apt.tests.processors.elements.Java11ElementProcessor to the command line. |
| 45 | + * @since 3.14 |
| 46 | + */ |
| 47 | +@SupportedAnnotationTypes("*") |
| 48 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 49 | +public class Java22ElementProcessor extends BaseProcessor { |
| 50 | + boolean reportSuccessAlready = true; |
| 51 | + RoundEnvironment roundEnv = null; |
| 52 | + Messager _messager = null; |
| 53 | + Filer _filer = null; |
| 54 | + boolean isBinaryMode = false; |
| 55 | + String mode; |
| 56 | + @Override |
| 57 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 58 | + super.init(processingEnv); |
| 59 | + _elementUtils = processingEnv.getElementUtils(); |
| 60 | + _messager = processingEnv.getMessager(); |
| 61 | + _filer = processingEnv.getFiler(); |
| 62 | + } |
| 63 | + // Always return false from this processor, because it supports "*". |
| 64 | + // The return value does not signify success or failure! |
| 65 | + @Override |
| 66 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 67 | + if (roundEnv.processingOver()) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + this.roundEnv = roundEnv; |
| 72 | + Map<String, String> options = processingEnv.getOptions(); |
| 73 | + if (!options.containsKey(this.getClass().getName())) { |
| 74 | + // Disable this processor unless we are intentionally performing the test. |
| 75 | + return false; |
| 76 | + } else { |
| 77 | + try { |
| 78 | + if (options.containsKey("binary")) { |
| 79 | + this.isBinaryMode = true; |
| 80 | + this.mode = "binary"; |
| 81 | + } else { |
| 82 | + this.mode = "source"; |
| 83 | + } |
| 84 | + if (!invokeTestMethods(options)) { |
| 85 | + testAll(); |
| 86 | + } |
| 87 | + if (this.reportSuccessAlready) { |
| 88 | + super.reportSuccess(); |
| 89 | + } |
| 90 | + } catch (AssertionFailedError e) { |
| 91 | + super.reportError(getExceptionStackTrace(e)); |
| 92 | + } catch (Throwable e) { |
| 93 | + e.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + private boolean invokeTestMethods(Map<String, String> options) throws Throwable { |
| 100 | + Method testMethod = null; |
| 101 | + Set<String> keys = options.keySet(); |
| 102 | + boolean testsFound = false; |
| 103 | + for (String option : keys) { |
| 104 | + if (option.startsWith("test")) { |
| 105 | + try { |
| 106 | + testMethod = this.getClass().getDeclaredMethod(option, new Class[0]); |
| 107 | + if (testMethod != null) { |
| 108 | + testsFound = true; |
| 109 | + testMethod.invoke(this, new Object[0]); |
| 110 | + } |
| 111 | + } catch (InvocationTargetException e) { |
| 112 | + throw e.getCause(); |
| 113 | + } catch (Exception e) { |
| 114 | + super.reportError(getExceptionStackTrace(e)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + return testsFound; |
| 119 | + } |
| 120 | + |
| 121 | + public void testAll() throws AssertionFailedError, IOException { |
| 122 | + testGetEnumConstantBody01(); |
| 123 | + testGetEnumConstantBody02(); |
| 124 | + } |
| 125 | + public void testGetEnumConstantBody01() throws IOException { |
| 126 | + Set<? extends Element> rootElements = this.roundEnv.getRootElements(); |
| 127 | + TypeElement elem = find(rootElements, "EnumColor"); |
| 128 | + assertNotNull("TypeElement for enum should not be null", elem); |
| 129 | + List<? extends Element> members = _elementUtils.getAllMembers(elem); |
| 130 | + VariableElement blue = null, red = null; |
| 131 | + for (Element member : members) { |
| 132 | + if ("BLUE".equals(member.getSimpleName().toString())) { |
| 133 | + blue = (VariableElement) member; |
| 134 | + } else if ("RED".equals(member.getSimpleName().toString())) { |
| 135 | + red = (VariableElement) member; |
| 136 | + } |
| 137 | + } |
| 138 | + assertNotNull("enum constant should not be null", blue); |
| 139 | + assertNotNull("enum constant should not be null", red); |
| 140 | + TypeElement enumConstantBody = _elementUtils.getEnumConstantBody(blue); |
| 141 | + assertNotNull("constant body should not be null", enumConstantBody); |
| 142 | + enumConstantBody = _elementUtils.getEnumConstantBody(red); |
| 143 | + assertNotNull("constant body should not be null", enumConstantBody); |
| 144 | + } |
| 145 | + public void testGetEnumConstantBody02() throws IOException { |
| 146 | + Set<? extends Element> rootElements = this.roundEnv.getRootElements(); |
| 147 | + TypeElement elem = find(rootElements, "EnumShape"); |
| 148 | + assertNotNull("TypeElement for enum should not be null", elem); |
| 149 | + List<? extends Element> members = _elementUtils.getAllMembers(elem); |
| 150 | + VariableElement squ = null, cir = null; |
| 151 | + for (Element member : members) { |
| 152 | + if ("SQU".equals(member.getSimpleName().toString())) { |
| 153 | + squ = (VariableElement) member; |
| 154 | + } else if ("CIR".equals(member.getSimpleName().toString())) { |
| 155 | + cir = (VariableElement) member; |
| 156 | + } |
| 157 | + } |
| 158 | + assertNotNull("enum constant should not be null", squ); |
| 159 | + assertNotNull("enum constant should not be null", cir); |
| 160 | + TypeElement enumConstantBody = _elementUtils.getEnumConstantBody(squ); |
| 161 | + assertNull("constant body should be null", enumConstantBody); |
| 162 | + enumConstantBody = _elementUtils.getEnumConstantBody(cir); |
| 163 | + assertNull("constant body should be null", enumConstantBody); |
| 164 | + } |
| 165 | + private TypeElement find(Set<? extends Element> elements, String name) { |
| 166 | + for (Element element : elements) { |
| 167 | + if (name.equals(element.getSimpleName().toString())) { |
| 168 | + return (TypeElement) element; |
| 169 | + } |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | + @Override |
| 174 | + public void reportError(String msg) { |
| 175 | + throw new AssertionFailedError(msg + " [mode = " + this.mode + "]"); |
| 176 | + } |
| 177 | + private String getExceptionStackTrace(Throwable t) { |
| 178 | + StringBuilder buf = new StringBuilder(t.getMessage()); |
| 179 | + StackTraceElement[] traces = t.getStackTrace(); |
| 180 | + for (int i = 0; i < traces.length; i++) { |
| 181 | + StackTraceElement trace = traces[i]; |
| 182 | + buf.append("\n\tat " + trace); |
| 183 | + if (i == 12) |
| 184 | + break; // Don't dump all stacks |
| 185 | + } |
| 186 | + return buf.toString(); |
| 187 | + } |
| 188 | + protected String getElementsAsString(List<? extends Element> list) { |
| 189 | + StringBuilder builder = new StringBuilder("["); |
| 190 | + for (Element element : list) { |
| 191 | + if (element instanceof PackageElement) { |
| 192 | + builder.append(((PackageElement) element).getQualifiedName()); |
| 193 | + } else if (element instanceof ModuleElement) { |
| 194 | + builder.append(((ModuleElement) element).getQualifiedName()); |
| 195 | + } else if (element instanceof TypeElement) { |
| 196 | + builder.append(((TypeElement) element).getQualifiedName()); |
| 197 | + } else { |
| 198 | + builder.append(element.getSimpleName()); |
| 199 | + } |
| 200 | + builder.append(", "); |
| 201 | + } |
| 202 | + builder.append("]"); |
| 203 | + return builder.toString(); |
| 204 | + } |
| 205 | + public void assertModifiers(Set<Modifier> modifiers, String[] expected) { |
| 206 | + assertEquals("Incorrect no of modifiers", modifiers.size(), expected.length); |
| 207 | + Set<String> actual = new HashSet<>(expected.length); |
| 208 | + for (Modifier modifier : modifiers) { |
| 209 | + actual.add(modifier.toString()); |
| 210 | + } |
| 211 | + for(int i = 0, length = expected.length; i < length; i++) { |
| 212 | + boolean result = actual.remove(expected[i]); |
| 213 | + if (!result) reportError("Modifier not present :" + expected[i]); |
| 214 | + } |
| 215 | + if (!actual.isEmpty()) { |
| 216 | + reportError("Unexpected modifiers present:" + actual.toString()); |
| 217 | + } |
| 218 | + } |
| 219 | + public void assertTrue(String msg, boolean value) { |
| 220 | + if (!value) reportError(msg); |
| 221 | + } |
| 222 | + public void assertFalse(String msg, boolean value) { |
| 223 | + if (value) reportError(msg); |
| 224 | + } |
| 225 | + public void assertSame(String msg, Object obj1, Object obj2) { |
| 226 | + if (obj1 != obj2) { |
| 227 | + reportError(msg + ", should be " + obj1.toString() + " but " + obj2.toString()); |
| 228 | + } |
| 229 | + } |
| 230 | + public void assertNotSame(String msg, Object obj1, Object obj2) { |
| 231 | + if (obj1 == obj2) { |
| 232 | + reportError(msg + ", " + obj1.toString() + " should not be same as " + obj2.toString()); |
| 233 | + } |
| 234 | + } |
| 235 | + public void assertNotNull(String msg, Object obj) { |
| 236 | + if (obj == null) { |
| 237 | + reportError(msg); |
| 238 | + } |
| 239 | + } |
| 240 | + public void assertNull(String msg, Object obj) { |
| 241 | + if (obj != null) { |
| 242 | + reportError(msg); |
| 243 | + } |
| 244 | + } |
| 245 | + public void assertEquals(String message, Object expected, Object actual) { |
| 246 | + if (equalsRegardingNull(expected, actual)) { |
| 247 | + return; |
| 248 | + } else { |
| 249 | + reportError(message + ", expected " + expected.toString() + " but was " + actual.toString()); |
| 250 | + } |
| 251 | + } |
| 252 | + |
| 253 | + public void assertEquals(String message, Object expected, Object alternateExpected, Object actual) { |
| 254 | + if (equalsRegardingNull(expected, actual) || equalsRegardingNull(alternateExpected, actual)) { |
| 255 | + return; |
| 256 | + } else { |
| 257 | + reportError(message + ", expected " + expected.toString() + " but was " + actual.toString()); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + static boolean equalsRegardingNull(Object expected, Object actual) { |
| 262 | + if (expected == null) { |
| 263 | + return actual == null; |
| 264 | + } |
| 265 | + return expected.equals(actual); |
| 266 | + } |
| 267 | + |
| 268 | + public void assertEquals(String msg, int expected, int actual) { |
| 269 | + if (expected != actual) { |
| 270 | + StringBuilder buf = new StringBuilder(); |
| 271 | + buf.append(msg); |
| 272 | + buf.append(", expected " + expected + " but was " + actual); |
| 273 | + reportError(buf.toString()); |
| 274 | + } |
| 275 | + } |
| 276 | + private static class AssertionFailedError extends Error { |
| 277 | + private static final long serialVersionUID = 1L; |
| 278 | + |
| 279 | + public AssertionFailedError(String msg) { |
| 280 | + super(msg); |
| 281 | + } |
| 282 | + } |
| 283 | +} |
0 commit comments