summaryrefslogtreecommitdiff
path: root/java/compiler/javac2/src/com/intellij/ant/Javac2.java
blob: e5b8aee7130a61a1d888c12ad551c0803be8c3d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.ant;

import com.intellij.compiler.instrumentation.InstrumentationClassFinder;
import com.intellij.compiler.instrumentation.InstrumenterClassWriter;
import com.intellij.compiler.notNullVerification.NotNullVerifyingInstrumenter;
import com.intellij.uiDesigner.compiler.*;
import com.intellij.uiDesigner.lw.CompiledClassPropertiesProvider;
import com.intellij.uiDesigner.lw.LwRootContainer;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Javac;
import org.apache.tools.ant.types.Path;
import org.jetbrains.org.objectweb.asm.ClassReader;
import org.jetbrains.org.objectweb.asm.ClassVisitor;
import org.jetbrains.org.objectweb.asm.ClassWriter;
import org.jetbrains.org.objectweb.asm.Opcodes;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.*;

public class Javac2 extends Javac {
  public static final String PROPERTY_INSTRUMENTATION_INCLUDE_JAVA_RUNTIME = "javac2.instrumentation.includeJavaRuntime";
  private ArrayList myFormFiles;
  private List myNestedFormPathList;
  private boolean instrumentNotNull = true;

  public Javac2() {
  }

  /**
   * Check if Java classes should be actually compiled by the task. This method is overridden by
   * {@link com.intellij.ant.InstrumentIdeaExtensions} task in order to suppress actual compilation
   * of the java sources.
   *
   * @return true if the java classes are compiled, false if just instrumentation is performed.
   */
  protected boolean areJavaClassesCompiled() {
    return true;
  }

  /**
   * This method is called when option that supported only for the case when java sources are compiled
   * and it is not supported for the case when only instrumentation is performed.
   *
   * @param optionName the option name to warn about.
   */
  private void unsupportedOptionMessage(final String optionName) {
    if (!areJavaClassesCompiled()) {
      log("The option " + optionName + " is not supported by InstrumentIdeaExtensions task", Project.MSG_ERR);
    }
  }

  public boolean getInstrumentNotNull() {
    return instrumentNotNull;
  }

  public void setInstrumentNotNull(boolean instrumentNotNull) {
    this.instrumentNotNull = instrumentNotNull;
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param v the option value
   */
  public void setDebugLevel(String v) {
    unsupportedOptionMessage("debugLevel");
    super.setDebugLevel(v);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param list the option value
   */
  public void setListfiles(boolean list) {
    unsupportedOptionMessage("listFiles");
    super.setListfiles(list);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param memoryInitialSize the option value
   */
  public void setMemoryInitialSize(String memoryInitialSize) {
    unsupportedOptionMessage("memoryInitialSize");
    super.setMemoryInitialSize(memoryInitialSize);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param memoryMaximumSize the option value
   */
  public void setMemoryMaximumSize(String memoryMaximumSize) {
    unsupportedOptionMessage("memoryMaximumSize");
    super.setMemoryMaximumSize(memoryMaximumSize);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param encoding the option value
   */
  public void setEncoding(String encoding) {
    unsupportedOptionMessage("encoding");
    super.setEncoding(encoding);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param optimize the option value
   */
  public void setOptimize(boolean optimize) {
    unsupportedOptionMessage("optimize");
    super.setOptimize(optimize);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param depend the option value
   */
  public void setDepend(boolean depend) {
    unsupportedOptionMessage("depend");
    super.setDepend(depend);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param f the option value
   */
  public void setFork(boolean f) {
    unsupportedOptionMessage("fork");
    super.setFork(f);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param forkExec the option value
   */
  public void setExecutable(String forkExec) {
    unsupportedOptionMessage("executable");
    super.setExecutable(forkExec);
  }

  /**
   * The overridden setter method that warns about unsupported option.
   *
   * @param compiler the option value
   */
  public void setCompiler(String compiler) {
    unsupportedOptionMessage("compiler");
    super.setCompiler(compiler);
  }

  /**
   * Sets the nested form directories that will be used during the
   * compilation.
   * @param nestedformdirs a list of {@link PrefixedPath}
   */
  public void setNestedformdirs(List nestedformdirs) {
    myNestedFormPathList = nestedformdirs;
  }

  /**
   * Gets the nested form directories that will be used during the
   * compilation.
   * @return the extension directories as a list of {@link PrefixedPath}
   */
  public List getNestedformdirs() {
    return myNestedFormPathList;
  }

  /**
   * Adds a path to nested form directories.
   * @return a path to be configured
   */
  public PrefixedPath createNestedformdirs() {
    PrefixedPath p = new PrefixedPath(getProject());
    if (myNestedFormPathList == null) {
      myNestedFormPathList = new ArrayList();
    }
    myNestedFormPathList.add(p);
    return p;
  }



  /**
   * The overridden compile method that does not actually compiles java sources but only instruments
   * class files.
   */
  protected void compile() {
    // compile java
    if (areJavaClassesCompiled()) {
      super.compile();
    }

    InstrumentationClassFinder finder = buildClasspathClassLoader();
    if (finder == null) {
      return;
    }
    try {
      instrumentForms(finder);

      if (getInstrumentNotNull()) {
        //NotNull instrumentation
        final int instrumented = instrumentNotNull(getDestdir(), finder);
        log("Added @NotNull assertions to " + instrumented + " files", Project.MSG_INFO);
      }

    }
    finally {
      finder.releaseResources();
    }
  }

  /**
   * Instrument forms
   *
   * @param finder a classloader to use
   */
  private void instrumentForms(final InstrumentationClassFinder finder) {
    // we instrument every file, because we cannot find which files should not be instrumented without dependency storage
    final ArrayList formsToInstrument = myFormFiles;

    if (formsToInstrument.size() == 0) {
      log("No forms to instrument found", Project.MSG_VERBOSE);
      return;
    }

    final HashMap class2form = new HashMap();

    for (int i = 0; i < formsToInstrument.size(); i++) {
      final File formFile = (File)formsToInstrument.get(i);

      log("compiling form " + formFile.getAbsolutePath(), Project.MSG_VERBOSE);
      final LwRootContainer rootContainer;
      try {
        rootContainer = Utils.getRootContainer(formFile.toURI().toURL(), new CompiledClassPropertiesProvider(finder.getLoader()));
      }
      catch (AlienFormFileException e) {
        // ignore non-IDEA forms
        continue;
      }
      catch (Exception e) {
        fireError("Cannot process form file " + formFile.getAbsolutePath() + ". Reason: " + e);
        continue;
      }

      final String classToBind = rootContainer.getClassToBind();
      if (classToBind == null) {
        continue;
      }

      String name = classToBind.replace('.', '/');
      File classFile = getClassFile(name);
      if (classFile == null) {
        log(formFile.getAbsolutePath() + ": Class to bind does not exist: " + classToBind, Project.MSG_WARN);
        continue;
      }

      final File alreadyProcessedForm = (File)class2form.get(classToBind);
      if (alreadyProcessedForm != null) {
        fireError(formFile.getAbsolutePath() +
                  ": " +
                  "The form is bound to the class " +
                  classToBind +
                  ".\n" +
                  "Another form " +
                  alreadyProcessedForm.getAbsolutePath() +
                  " is also bound to this class.");
        continue;
      }
      class2form.put(classToBind, formFile);

      try {
        int version;
        InputStream stream = new FileInputStream(classFile);
        try {
          version = getClassFileVersion(new ClassReader(stream));
        }
        finally {
          stream.close();
        }
        AntNestedFormLoader formLoader = new AntNestedFormLoader(finder.getLoader(), myNestedFormPathList);
        InstrumenterClassWriter classWriter = new InstrumenterClassWriter(getAsmClassWriterFlags(version), finder);
        final AsmCodeGenerator codeGenerator = new AsmCodeGenerator(rootContainer, finder, formLoader, false, classWriter);
        codeGenerator.patchFile(classFile);
        final FormErrorInfo[] warnings = codeGenerator.getWarnings();

        for (int j = 0; j < warnings.length; j++) {
          log(formFile.getAbsolutePath() + ": " + warnings[j].getErrorMessage(), Project.MSG_WARN);
        }
        final FormErrorInfo[] errors = codeGenerator.getErrors();
        if (errors.length > 0) {
          StringBuffer message = new StringBuffer();
          for (int j = 0; j < errors.length; j++) {
            if (message.length() > 0) {
              message.append("\n");
            }
            message.append(formFile.getAbsolutePath()).append(": ").append(errors[j].getErrorMessage());
          }
          fireError(message.toString());
        }
      }
      catch (Exception e) {
        fireError("Forms instrumentation failed for " + formFile.getAbsolutePath() + ": " + e.toString());
      }
    }
  }

  /**
   * @return the flags for class writer
   */
  private static int getAsmClassWriterFlags(int version) {
    return version >= Opcodes.V1_6 && version != Opcodes.V1_1 ? ClassWriter.COMPUTE_FRAMES : ClassWriter.COMPUTE_MAXS;
  }

  /**
   * Create class loader based on classpath, bootclasspath, and sourcepath.
   *
   * @return a URL classloader
   */
  private InstrumentationClassFinder buildClasspathClassLoader() {
    final StringBuffer classPathBuffer = new StringBuffer();
    final Project project = getProject();
    final Path cp = new Path(project);
    appendPath(cp, getBootclasspath());
    cp.setLocation(getDestdir().getAbsoluteFile());
    appendPath(cp, getClasspath());
    appendPath(cp, getSourcepath());
    appendPath(cp, getSrcdir());
    if (getIncludeantruntime()) {
      cp.addExisting(cp.concatSystemClasspath("last"));
    }
    boolean shouldInclude = getIncludejavaruntime();
    if (!shouldInclude) {
      if (project != null) {
        final String propValue = project.getProperty(PROPERTY_INSTRUMENTATION_INCLUDE_JAVA_RUNTIME);
        shouldInclude = !("false".equalsIgnoreCase(propValue) || "no".equalsIgnoreCase(propValue));
      }
      else {
        shouldInclude = true;
      }
    }
    if (shouldInclude) {
      cp.addJavaRuntime();
    }

    cp.addExtdirs(getExtdirs());

    final String[] pathElements = cp.list();
    for (int i = 0; i < pathElements.length; i++) {
      final String pathElement = pathElements[i];
      classPathBuffer.append(File.pathSeparator);
      classPathBuffer.append(pathElement);
    }

    final String classPath = classPathBuffer.toString();
    log("classpath=" + classPath, Project.MSG_VERBOSE);

    try {
      return createInstrumentationClassFinder(classPath);
    }
    catch (MalformedURLException e) {
      fireError(e.getMessage());
      return null;
    }
  }

  /**
   * Append path to class path if the appened path is not empty and is not null
   *
   * @param cp the path to modify
   * @param p  the path to append
   */
  private void appendPath(Path cp, final Path p) {
    if (p != null && p.size() > 0) {
      cp.append(p);
    }
  }

  /**
   * Instrument classes with NotNull annotations
   *
   * @param dir    the directory with classes to instrument (the directory is processed recursively)
   * @param finder the classloader to use
   * @return the amount of classes actually affected by instrumentation
   */
  private int instrumentNotNull(File dir, final InstrumentationClassFinder finder) {
    int instrumented = 0;
    final File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
      File file = files[i];
      final String name = file.getName();
      if (name.endsWith(".class")) {
        final String path = file.getPath();
        log("Adding @NotNull assertions to " + path, Project.MSG_VERBOSE);
        try {
          final FileInputStream inputStream = new FileInputStream(file);
          try {
            ClassReader reader = new ClassReader(inputStream);

            int version = getClassFileVersion(reader);

            if (version >= Opcodes.V1_5) {
              ClassWriter writer = new InstrumenterClassWriter(getAsmClassWriterFlags(version), finder);

              if (NotNullVerifyingInstrumenter.processClassFile(reader, writer)) {
                final FileOutputStream fileOutputStream = new FileOutputStream(path);
                try {
                  fileOutputStream.write(writer.toByteArray());
                  instrumented++;
                }
                finally {
                  fileOutputStream.close();
                }
              }
            }
          }
          finally {
            inputStream.close();
          }
        }
        catch (IOException e) {
          log("Failed to instrument @NotNull assertion for " + path + ": " + e.getMessage(), Project.MSG_WARN);
        }
        catch (Exception e) {
          fireError("@NotNull instrumentation failed for " + path + ": " + e.toString());
        }
      }
      else if (file.isDirectory()) {
        instrumented += instrumentNotNull(file, finder);
      }
    }

    return instrumented;
  }

  private static int getClassFileVersion(ClassReader reader) {
    final int[] classfileVersion = new int[1];
    reader.accept(new ClassVisitor(Opcodes.ASM5) {
      public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        classfileVersion[0] = version;
      }
    }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);

    return classfileVersion[0];
  }

  private void fireError(final String message) {
    if (failOnError) {
      throw new BuildException(message, getLocation());
    }
    else {
      log(message, Project.MSG_ERR);
    }
  }

  private File getClassFile(String className) {
    final String classOrInnerName = getClassOrInnerName(className);
    if (classOrInnerName == null) return null;
    return new File(getDestdir().getAbsolutePath(), classOrInnerName + ".class");
  }

  private String getClassOrInnerName(String className) {
    File classFile = new File(getDestdir().getAbsolutePath(), className + ".class");
    if (classFile.exists()) return className;
    int position = className.lastIndexOf('/');
    if (position == -1) return null;
    return getClassOrInnerName(className.substring(0, position) + '$' + className.substring(position + 1));
  }

  protected void resetFileLists() {
    super.resetFileLists();
    myFormFiles = new ArrayList();
  }

  protected void scanDir(final File srcDir, final File destDir, final String[] files) {
    super.scanDir(srcDir, destDir, files);
    for (int i = 0; i < files.length; i++) {
      final String file = files[i];
      if (file.endsWith(".form")) {
        log("Found form file " + file, Project.MSG_VERBOSE);
        myFormFiles.add(new File(srcDir, file));
      }
    }
  }

  private static InstrumentationClassFinder createInstrumentationClassFinder(final String classPath) throws MalformedURLException {
    final ArrayList urls = new ArrayList();
    for (StringTokenizer tokenizer = new StringTokenizer(classPath, File.pathSeparator); tokenizer.hasMoreTokens();) {
      final String s = tokenizer.nextToken();
      urls.add(new File(s).toURI().toURL());
    }
    final URL[] urlsArr = (URL[])urls.toArray(new URL[urls.size()]);
    return new InstrumentationClassFinder(urlsArr);
  }

  private class AntNestedFormLoader implements NestedFormLoader {
    private final ClassLoader myLoader;
    private final List myNestedFormPathList;
    private final HashMap myFormCache = new HashMap();

    public AntNestedFormLoader(final ClassLoader loader, List nestedFormPathList) {
      myLoader = loader;
      myNestedFormPathList = nestedFormPathList;
    }

    public LwRootContainer loadForm(String formFilePath) throws Exception {
      if (myFormCache.containsKey(formFilePath)) {
        return (LwRootContainer)myFormCache.get(formFilePath);
      }

      String lowerFormFilePath = formFilePath.toLowerCase();
      log("Searching for form " + lowerFormFilePath, Project.MSG_VERBOSE);
      for (Iterator iterator = myFormFiles.iterator(); iterator.hasNext();) {
        File file = (File)iterator.next();
        String name = file.getAbsolutePath().replace(File.separatorChar, '/').toLowerCase();
        log("Comparing with " + name, Project.MSG_VERBOSE);
        if (name.endsWith(lowerFormFilePath)) {
          return loadForm(formFilePath, new FileInputStream(file));
        }
      }

      if (myNestedFormPathList != null) {
        for (int i = 0; i < myNestedFormPathList.size(); i++) {
          PrefixedPath path = (PrefixedPath)myNestedFormPathList.get(i);
          File formFile = path.findFile(formFilePath);
          if (formFile != null) {
            return loadForm(formFilePath, new FileInputStream(formFile));
          }
        }
      }
      InputStream resourceStream = myLoader.getResourceAsStream(formFilePath);
      if (resourceStream != null) {
        return loadForm(formFilePath, resourceStream);
      }
      throw new Exception("Cannot find nested form file " + formFilePath);
    }

    private LwRootContainer loadForm(String formFileName, InputStream resourceStream) throws Exception {
      final LwRootContainer container = Utils.getRootContainer(resourceStream, null);
      myFormCache.put(formFileName, container);
      return container;
    }

    public String getClassToBindName(LwRootContainer container) {
      final String className = container.getClassToBind();
      String result = getClassOrInnerName(className.replace('.', '/'));
      if (result != null) return result.replace('/', '.');
      return className;
    }
  }
}