001package ball.annotation.processing; 002/*- 003 * ########################################################################## 004 * Utilities 005 * %% 006 * Copyright (C) 2008 - 2022 Allen D. Ball 007 * %% 008 * Licensed under the Apache License, Version 2.0 (the "License"); 009 * you may not use this file except in compliance with the License. 010 * You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, software 015 * distributed under the License is distributed on an "AS IS" BASIS, 016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 017 * See the License for the specific language governing permissions and 018 * limitations under the License. 019 * ########################################################################## 020 */ 021/* import com.sun.tools.javac.processing.JavacProcessingEnvironment; */ 022/* import com.sun.tools.javac.util.Context; */ 023import java.lang.reflect.Method; 024import javax.annotation.processing.ProcessingEnvironment; 025import javax.tools.JavaFileManager; 026import lombok.NoArgsConstructor; 027import lombok.ToString; 028 029import static lombok.AccessLevel.PRIVATE; 030 031/** 032 * JDK 8, 9, and 10-specific methods for {@link AbstractProcessor}. 033 * 034 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball} 035 */ 036@NoArgsConstructor(access = PRIVATE) @ToString 037public abstract class Shims { 038 039 /** 040 * Method to get the {@link JavaFileManager} from a 041 * {@link ProcessingEnvironment}. 042 * 043 * @param env The {@link ProcessingEnvironment}. 044 * 045 * @return The {@link JavaFileManager} if it can be obtained; 046 * {@code null} otherwise. 047 */ 048 @SuppressWarnings({ "sunapi" }) 049 protected static JavaFileManager getJavaFileManager(ProcessingEnvironment env) { 050 JavaFileManager fm = null; 051 /* 052 * Use reflection to avoid tickling 053 * https://bugs.openjdk.java.net/browse/JDK-8209058 on JDK 9 and 10. 054 */ 055 try { 056 /* 057 * Context context = ((JavacProcessingEnvironment) env).getContext(); 058 */ 059 Method getContext = env.getClass().getMethod("getContext"); 060 061 getContext.setAccessible(true); 062 063 Object context = getContext.invoke(env); 064 /* 065 * fm = context.get(JavaFileManager.class); 066 */ 067 Method get = context.getClass().getMethod("get", Class.class); 068 069 get.setAccessible(true); 070 071 fm = (JavaFileManager) get.invoke(context, JavaFileManager.class); 072 } catch (Exception exception) { 073 } 074 075 return fm; 076 } 077}