|
| 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.LinkedHashSet; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Set; |
| 23 | +import java.util.TreeSet; |
| 24 | +import javax.annotation.processing.Filer; |
| 25 | +import javax.annotation.processing.Messager; |
| 26 | +import javax.annotation.processing.ProcessingEnvironment; |
| 27 | +import javax.annotation.processing.RoundEnvironment; |
| 28 | +import javax.annotation.processing.SupportedAnnotationTypes; |
| 29 | +import javax.annotation.processing.SupportedSourceVersion; |
| 30 | +import javax.lang.model.SourceVersion; |
| 31 | +import javax.lang.model.element.Element; |
| 32 | +import javax.lang.model.element.TypeElement; |
| 33 | +import org.eclipse.jdt.compiler.apt.tests.processors.base.BaseProcessor; |
| 34 | + |
| 35 | +/** |
| 36 | + * A processor that explores the module import feature and related aspects. |
| 37 | + * To enable this processor, add |
| 38 | + * -Aorg.eclipse.jdt.compiler.apt.tests.processors.elements.ModuleImportProcessor to the command line. |
| 39 | + */ |
| 40 | +@SupportedAnnotationTypes("*") |
| 41 | +@SupportedSourceVersion(SourceVersion.RELEASE_8) |
| 42 | +public class ModuleImportProcessor extends BaseProcessor { |
| 43 | + boolean reportSuccessAlready = true; |
| 44 | + RoundEnvironment roundEnv = null; |
| 45 | + Messager _messager = null; |
| 46 | + Filer _filer = null; |
| 47 | + boolean isBinaryMode = false; |
| 48 | + String mode; |
| 49 | + @Override |
| 50 | + public synchronized void init(ProcessingEnvironment processingEnv) { |
| 51 | + super.init(processingEnv); |
| 52 | + _elementUtils = processingEnv.getElementUtils(); |
| 53 | + _messager = processingEnv.getMessager(); |
| 54 | + _filer = processingEnv.getFiler(); |
| 55 | + } |
| 56 | + // Always return false from this processor, because it supports "*". |
| 57 | + // The return value does not signify success or failure! |
| 58 | + @Override |
| 59 | + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
| 60 | + if (roundEnv.processingOver()) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + this.roundEnv = roundEnv; |
| 65 | + Map<String, String> options = processingEnv.getOptions(); |
| 66 | + if (!options.containsKey(this.getClass().getName())) { |
| 67 | + // Disable this processor unless we are intentionally performing the test. |
| 68 | + return false; |
| 69 | + } else { |
| 70 | + try { |
| 71 | + if (options.containsKey("binary")) { |
| 72 | + this.isBinaryMode = true; |
| 73 | + this.mode = "binary"; |
| 74 | + } else { |
| 75 | + this.mode = "source"; |
| 76 | + } |
| 77 | + if (!invokeTestMethods(options)) { |
| 78 | + testAll(); |
| 79 | + } |
| 80 | + if (this.reportSuccessAlready) { |
| 81 | + super.reportSuccess(); |
| 82 | + } |
| 83 | + } catch (AssertionFailedError e) { |
| 84 | + super.reportError(getExceptionStackTrace(e)); |
| 85 | + } catch (Throwable e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } |
| 88 | + } |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + private boolean invokeTestMethods(Map<String, String> options) throws Throwable { |
| 93 | + Method testMethod = null; |
| 94 | + Set<String> keys = options.keySet(); |
| 95 | + boolean testsFound = false; |
| 96 | + for (String option : keys) { |
| 97 | + if (option.startsWith("test")) { |
| 98 | + try { |
| 99 | + testMethod = this.getClass().getDeclaredMethod(option, new Class[0]); |
| 100 | + if (testMethod != null) { |
| 101 | + testsFound = true; |
| 102 | + testMethod.invoke(this, new Object[0]); |
| 103 | + } |
| 104 | + } catch (InvocationTargetException e) { |
| 105 | + throw e.getCause(); |
| 106 | + } catch (Exception e) { |
| 107 | + super.reportError(getExceptionStackTrace(e)); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + return testsFound; |
| 112 | + } |
| 113 | + |
| 114 | + public void testAll() throws AssertionFailedError, IOException { |
| 115 | + test001(); |
| 116 | + } |
| 117 | + |
| 118 | + public void test001() throws IOException { |
| 119 | + Set<? extends Element> rootElements = this.roundEnv.getRootElements(); |
| 120 | + Set<Element> rootModules = new TreeSet<>((o1, o2) -> o1.getSimpleName().toString().compareTo(o2.getSimpleName().toString())); |
| 121 | + for (Element root : rootElements) { |
| 122 | + Element cur = root; |
| 123 | + LinkedHashSet<Element> stack = new LinkedHashSet<>(); |
| 124 | + stack.add(root); |
| 125 | + |
| 126 | + for (; ; ) { |
| 127 | + Element parent = cur.getEnclosingElement(); |
| 128 | + |
| 129 | + if (stack.contains(parent)) { |
| 130 | + break; |
| 131 | + } |
| 132 | + |
| 133 | + stack.add(parent); |
| 134 | + if (parent != null) { |
| 135 | + cur = parent; |
| 136 | + } else { |
| 137 | + rootModules.add(cur); |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + assertEquals("Incorrect number of root modules", 3, rootModules.size()); |
| 143 | + } |
| 144 | + public void test002() throws IOException { |
| 145 | + } |
| 146 | + @Override |
| 147 | + public void reportError(String msg) { |
| 148 | + throw new AssertionFailedError(msg); |
| 149 | + } |
| 150 | + private String getExceptionStackTrace(Throwable t) { |
| 151 | + StringBuilder buf = new StringBuilder(t.getMessage()); |
| 152 | + StackTraceElement[] traces = t.getStackTrace(); |
| 153 | + for (int i = 0; i < traces.length; i++) { |
| 154 | + StackTraceElement trace = traces[i]; |
| 155 | + buf.append("\n\tat " + trace); |
| 156 | + if (i == 12) |
| 157 | + break; // Don't dump all stacks |
| 158 | + } |
| 159 | + return buf.toString(); |
| 160 | + } |
| 161 | + public void assertEquals(String message, int expected, int actual) { |
| 162 | + if (expected == actual) { |
| 163 | + return; |
| 164 | + } else { |
| 165 | + reportError(message + ", expected " + expected + " but was " + actual); |
| 166 | + } |
| 167 | + } |
| 168 | + private static class AssertionFailedError extends Error { |
| 169 | + private static final long serialVersionUID = 1L; |
| 170 | + |
| 171 | + public AssertionFailedError(String msg) { |
| 172 | + super(msg); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments