View Javadoc
1   package lombok.launch;
2   
3   import java.lang.reflect.InvocationTargetException;
4   import java.lang.reflect.Method;
5   import java.io.File;
6   import java.io.IOException;
7   import java.util.List;
8   import java.util.Map;
9   
10  /**
11   * Since the Shadow Class Loader hides Lombok's internal Delombok, we need to access it via reflection.
12   *
13   * @see <a href="https://github.com/rzwitserloot/lombok/blob/master/src/delombok/lombok/delombok/Delombok.java">lombok.delombok.Delombok</a>
14   */
15  public class Delombok {
16  
17      private final Object delombokInstance;
18  
19      private final Method addDirectory;
20      private final Method delombok;
21      private final Method formatOptionsToMap;
22      private final Method setVerbose;
23      private final Method setCharset;
24      private final Method setClasspath;
25      private final Method setFormatPreferences;
26      private final Method setOutput;
27      private final Method setSourcepath;
28  
29      public Delombok () throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException {
30          final ClassLoader shadowClassLoader = Main.getShadowClassLoader();
31          final Class<?> delombokClass = shadowClassLoader.loadClass("lombok.delombok.Delombok");
32          this.delombokInstance = delombokClass.getDeclaredConstructor().newInstance();
33          // Get method handles...
34          this.addDirectory = delombokClass.getMethod("addDirectory", File.class);
35          this.delombok = delombokClass.getMethod("delombok");
36          this.formatOptionsToMap = delombokClass.getMethod("formatOptionsToMap", List.class);
37          this.setVerbose = delombokClass.getMethod("setVerbose", boolean.class);
38          this.setCharset = delombokClass.getMethod("setCharset", String.class);
39          this.setClasspath = delombokClass.getMethod("setClasspath", String.class);
40          this.setFormatPreferences = delombokClass.getMethod("setFormatPreferences", Map.class);
41          this.setOutput = delombokClass.getMethod("setOutput", File.class);
42          this.setSourcepath = delombokClass.getMethod("setSourcepath", String.class);
43      }
44  
45      public void addDirectory (final File base) throws IllegalAccessException, IOException, InvocationTargetException {
46          addDirectory.invoke(delombokInstance, base);
47      }
48  
49      public boolean delombok () throws IllegalAccessException, IOException, InvocationTargetException {
50          return Boolean.parseBoolean( delombok.invoke(delombokInstance).toString() );
51      }
52  
53      @SuppressWarnings("unchecked")
54      public Map<String, String> formatOptionsToMap (final List<String> formatOptions) throws Exception {
55          return (Map<String, String>)formatOptionsToMap.invoke(null, formatOptions);
56      }
57  
58      public void setVerbose (final boolean verbose) throws IllegalAccessException, InvocationTargetException {
59          setVerbose.invoke(delombokInstance, verbose);
60      }
61  
62      public void setCharset (final String charset) throws IllegalAccessException, InvocationTargetException {
63          setCharset.invoke(delombokInstance, charset);
64      }
65  
66      public void setClasspath (final String classpath) throws IllegalAccessException, InvocationTargetException {
67          setClasspath.invoke(delombokInstance, classpath);
68      }
69  
70      public void setFormatPreferences (final Map<String, String> prefs) throws IllegalAccessException, InvocationTargetException {
71          setFormatPreferences.invoke(delombokInstance, prefs);
72      }
73  
74      public void setOutput (final File dir) throws IllegalAccessException, InvocationTargetException {
75          setOutput.invoke(delombokInstance, dir);
76      }
77  
78      public void setSourcepath (final String sourcepath) throws IllegalAccessException, InvocationTargetException {
79          setSourcepath.invoke(delombokInstance, sourcepath);
80      }
81  }