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