public static Object getDocComments(JCCompilationUnit cu) { try { return JCCOMPILATIONUNIT_DOCCOMMENTS.get(cu); } catch (IllegalAccessException e) { throw sneakyThrow(e); } }
public static JCTree getExtendsClause(JCClassDecl decl) { try { return (JCTree) getExtendsClause.invoke(decl); } catch (IllegalAccessException e) { throw sneakyThrow(e); } catch (InvocationTargetException e) { throw sneakyThrow(e.getCause()); } }
public static void storeEnd(JCTree tree, int pos, JCCompilationUnit top) { try { Object endPositions = JCCOMPILATIONUNIT_ENDPOSITIONS.get(top); storeEnd.invoke(endPositions, tree, pos); } catch (IllegalAccessException e) { throw sneakyThrow(e); } catch (InvocationTargetException e) { throw sneakyThrow(e.getCause()); } }
public static int getEndPosition(DiagnosticPosition pos, JCCompilationUnit top) { try { Object endPositions = JCCOMPILATIONUNIT_ENDPOSITIONS.get(top); return (Integer) getEndPosition.invoke(pos, endPositions); } catch (IllegalAccessException e) { throw sneakyThrow(e); } catch (InvocationTargetException e) { throw sneakyThrow(e.getCause()); } }
public static void initDocComments(JCCompilationUnit cu) { try { JCCOMPILATIONUNIT_DOCCOMMENTS.set(cu, new HashMap<Object, String>()); } catch (IllegalArgumentException e) { // That's fine - we're on JDK8, we'll fix that later. } catch (IllegalAccessException e) { throw sneakyThrow(e); } }
private static Method getMethod(Class<?> clazz, String name, String... paramTypes) { try { Class<?>[] c = new Class[paramTypes.length]; for (int i = 0; i < paramTypes.length; i++) c[i] = Class.forName(paramTypes[i]); return Permit.getMethod(clazz, name, c); } catch (NoSuchMethodException e) { throw sneakyThrow(e); } catch (ClassNotFoundException e) { throw sneakyThrow(e); } }
static Object getFieldCached(ConcurrentMap<String, Object> cache, String className, String fieldName) { Object value = cache.get(fieldName); if (value != null) return value; try { value = Permit.getField(Class.forName(className), fieldName).get(null); } catch (NoSuchFieldException e) { throw Javac.sneakyThrow(e); } catch (IllegalAccessException e) { throw Javac.sneakyThrow(e); } catch (ClassNotFoundException e) { throw Javac.sneakyThrow(e); } cache.putIfAbsent(fieldName, value); return value; }
private static Method getMethod(Class<?> clazz, String name, Class<?>... paramTypes) { try { return Permit.getMethod(clazz, name, paramTypes); } catch (NoSuchMethodException e) { throw sneakyThrow(e); } }
public static TreeTag treeTag(JCTree o) { try { if (TAG_METHOD != null) return new TreeTag(TAG_METHOD.invoke(o)); else return new TreeTag(TAG_FIELD.get(o)); } catch (InvocationTargetException e) { throw Javac.sneakyThrow(e.getCause()); } catch (IllegalAccessException e) { throw Javac.sneakyThrow(e); } }
@SuppressWarnings("unchecked") private static <J> J invokeAny(Object owner, MethodId<J> m, Object... args) { Method method = METHOD_CACHE.get(m); if (method == null) method = addToCache(m); try { if (m.returnType.isPrimitive()) { Object res = method.invoke(owner, args); String sn = res.getClass().getSimpleName().toLowerCase(); if (!sn.startsWith(m.returnType.getSimpleName())) throw new ClassCastException(res.getClass() + " to " + m.returnType); return (J) res; } return m.returnType.cast(method.invoke(owner, args)); } catch (InvocationTargetException e) { throw Javac.sneakyThrow(e.getCause()); } catch (IllegalAccessException e) { throw Javac.sneakyThrow(e); } catch (IllegalArgumentException e) { System.err.println(method); throw Javac.sneakyThrow(e); } }
private static void registerCommentsCollectingScannerFactory(Context context) { try { Class<?> scannerFactory; int javaCompilerVersion = Javac.getJavaCompilerVersion(); if (javaCompilerVersion <= 6) { scannerFactory = Class.forName("lombok.javac.java6.CommentCollectingScannerFactory"); } else if (javaCompilerVersion == 7) { scannerFactory = Class.forName("lombok.javac.java7.CommentCollectingScannerFactory"); } else { scannerFactory = Class.forName("lombok.javac.java8.CommentCollectingScannerFactory"); } Permit.getMethod(scannerFactory, "preRegister", Context.class).invoke(null, context); } catch (InvocationTargetException e) { throw Javac.sneakyThrow(e.getCause()); } catch (Exception e) { throw Javac.sneakyThrow(e); } }
public static TypeTag typeTag(Type t) { if (t == null) return Javac.CTC_VOID; try { return new TypeTag(getFieldCached(FIELD_CACHE, t, "tag")); } catch (NoSuchFieldException e) { if (TYPE_TYPETAG_METHOD == null) throw new IllegalStateException("Type " + t.getClass() + " has neither 'tag' nor getTag()"); try { return new TypeTag(TYPE_TYPETAG_METHOD.invoke(t)); } catch (IllegalAccessException ex) { throw Javac.sneakyThrow(ex); } catch (InvocationTargetException ex) { throw Javac.sneakyThrow(ex.getCause()); } } }
private static void setInCompiler(JavaCompiler compiler, Context context) { try { Class<?> parserFactory; int javaCompilerVersion = Javac.getJavaCompilerVersion(); if (javaCompilerVersion <= 6) { parserFactory = Class.forName("lombok.javac.java6.CommentCollectingParserFactory"); } else if (javaCompilerVersion == 7) { parserFactory = Class.forName("lombok.javac.java7.CommentCollectingParserFactory"); } else if (javaCompilerVersion == 8) { parserFactory = Class.forName("lombok.javac.java8.CommentCollectingParserFactory"); } else { parserFactory = Class.forName("lombok.javac.java9.CommentCollectingParserFactory"); } Permit.getMethod(parserFactory, "setInCompiler", JavaCompiler.class, Context.class).invoke(null, compiler, context); } catch (InvocationTargetException e) { throw Javac.sneakyThrow(e.getCause()); } catch (Exception e) { throw Javac.sneakyThrow(e); } } }
static Object getFieldCached(ConcurrentMap<Class<?>, Field> cache, Object ref, String fieldName) throws NoSuchFieldException { Class<?> c = ref.getClass(); Field field = cache.get(c); if (field == null) { try { field = Permit.getField(c, fieldName); } catch (NoSuchFieldException e) { cache.putIfAbsent(c, NOSUCHFIELDEX_MARKER); throw Javac.sneakyThrow(e); } Permit.setAccessible(field); Field old = cache.putIfAbsent(c, field); if (old != null) field = old; } if (field == NOSUCHFIELDEX_MARKER) throw new NoSuchFieldException(fieldName); try { return field.get(ref); } catch (IllegalAccessException e) { throw Javac.sneakyThrow(e); } } }
public static Type createVoidType(Symtab symbolTable, TypeTag tag) { if (symtabVoidType != null) try { return (Type) symtabVoidType.get(symbolTable); } catch (IllegalAccessException ignore) {} if (Javac.getJavaCompilerVersion() < 8) { return new JCNoType(((Integer) tag.value).intValue()); } else { try { if (CTC_VOID.equals(tag)) { return (Type) JC_VOID_TYPE.newInstance(); } else { return (Type) JC_NO_TYPE.newInstance(); } } catch (IllegalAccessException e) { throw sneakyThrow(e); } catch (InstantiationException e) { throw sneakyThrow(e); } } }
public static TypeTag typeTag(JCTree o) { try { return new TypeTag(getFieldCached(FIELD_CACHE, o, "typetag")); } catch (NoSuchFieldException e) { throw Javac.sneakyThrow(e); } }