public JavaFileObject getJavaFileForOutput()

in src/org/jetbrains/jps/builders/javacApi/OptimizedFileManager.java [174:236]


    public JavaFileObject getJavaFileForOutput(Location location, final String className, final JavaFileObject.Kind kind, FileObject fileObject) throws IOException {
        final JavaFileObject result = super.getJavaFileForOutput(location, className, kind, fileObject);
        final String classFileName = PathUtil.toPath(result.toUri());
        final String sourceFileName = PathUtil.toPath(fileObject.toUri());

        return new ForwardingJavaFileObject<JavaFileObject>(result) {
            private OutputStream superOpenOutputStream() throws IOException {
                return super.openOutputStream();
            }

            @Override
            public OutputStream openOutputStream() throws IOException {
                return new OutputStream() {
                    public void flush() throws IOException {
                    }

                    public void close() throws IOException {
                    }

                    public void write(int b) throws IOException {
                        assert (false);
                    }

                    public void write(byte[] b) throws IOException {
                        assert (false);
                    }

                    public void write(final byte[] b, final int off, final int len) throws IOException {
                        final byte[] buffer = Arrays.copyOfRange(b, off, len);

                        if (kind.equals(JavaFileObject.Kind.CLASS)) {
                            loader.defineClass(className.replaceAll("\\.", "/"), buffer);

                            if (callback != null) {
                                final ClassReader reader = new ClassReader(buffer);
                                callback.associate(classFileName, Callbacks.getDefaultLookup(sourceFileName), reader);
                            }

                            myWriters.add(new DelayedClassFileWriter() {
                                public void commit() throws IOException {
                                    final OutputStream result = superOpenOutputStream();

                                    final byte[] instrumented = InstrumentationUtil.instrumentNotNull(buffer, loader);

                                    if (instrumented != null) {
                                        result.write(instrumented);
                                    } else {
                                        result.write(buffer);
                                    }

                                    result.close();
                                }
                            });
                        } else {
                            final OutputStream result = superOpenOutputStream();
                            result.write(buffer);
                            result.close();
                        }
                    }
                };
            }
        };
    }