language/server/web/language.web.api/src/org/netbeans/modules/jackpot30/backend/language/api/JavacCreatorImpl.java
author Jan Lahoda <jlahoda@netbeans.org>
Wed, 14 Aug 2013 08:17:42 +0200
changeset 978 cb2dd061627f
parent 970 a2dcd04c7747
child 1018 a970874548a7
permissions -rw-r--r--
Preventing eager initialization of JavaCompiler.
jlahoda@919
     1
/*
jlahoda@919
     2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
jlahoda@919
     3
 *
jlahoda@919
     4
 * Copyright 2012 Oracle and/or its affiliates. All rights reserved.
jlahoda@919
     5
 *
jlahoda@919
     6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
jlahoda@919
     7
 * Other names may be trademarks of their respective owners.
jlahoda@919
     8
 *
jlahoda@919
     9
 * The contents of this file are subject to the terms of either the GNU
jlahoda@919
    10
 * General Public License Version 2 only ("GPL") or the Common
jlahoda@919
    11
 * Development and Distribution License("CDDL") (collectively, the
jlahoda@919
    12
 * "License"). You may not use this file except in compliance with the
jlahoda@919
    13
 * License. You can obtain a copy of the License at
jlahoda@919
    14
 * http://www.netbeans.org/cddl-gplv2.html
jlahoda@919
    15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
jlahoda@919
    16
 * specific language governing permissions and limitations under the
jlahoda@919
    17
 * License.  When distributing the software, include this License Header
jlahoda@919
    18
 * Notice in each file and include the License file at
jlahoda@919
    19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
jlahoda@919
    20
 * particular file as subject to the "Classpath" exception as provided
jlahoda@919
    21
 * by Oracle in the GPL Version 2 section of the License file that
jlahoda@919
    22
 * accompanied this code. If applicable, add the following below the
jlahoda@919
    23
 * License Header, with the fields enclosed by brackets [] replaced by
jlahoda@919
    24
 * your own identifying information:
jlahoda@919
    25
 * "Portions Copyrighted [year] [name of copyright owner]"
jlahoda@919
    26
 *
jlahoda@919
    27
 * If you wish your version of this file to be governed by only the CDDL
jlahoda@919
    28
 * or only the GPL Version 2, indicate your decision by adding
jlahoda@919
    29
 * "[Contributor] elects to include this software in this distribution
jlahoda@919
    30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
jlahoda@919
    31
 * single choice of license, a recipient has the option to distribute
jlahoda@919
    32
 * your version of this file under either the CDDL, the GPL Version 2 or
jlahoda@919
    33
 * to extend the choice of license to its licensees as provided above.
jlahoda@919
    34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
jlahoda@919
    35
 * Version 2 license, then the option applies only if the new code is
jlahoda@919
    36
 * made subject to such option by the copyright holder.
jlahoda@919
    37
 *
jlahoda@919
    38
 * Contributor(s):
jlahoda@919
    39
 *
jlahoda@919
    40
 * Portions Copyrighted 2012 Sun Microsystems, Inc.
jlahoda@919
    41
 */
jlahoda@919
    42
package org.netbeans.modules.jackpot30.backend.language.api;
jlahoda@919
    43
jlahoda@919
    44
import com.sun.tools.javac.api.JavacTaskImpl;
jlahoda@942
    45
import com.sun.tools.javac.api.JavacTool;
jlahoda@919
    46
import com.sun.tools.javac.main.JavaCompiler;
jlahoda@919
    47
import com.sun.tools.javac.util.Context;
jlahoda@919
    48
import java.io.IOException;
jlahoda@919
    49
import java.io.PrintWriter;
jlahoda@919
    50
import java.io.Writer;
jlahoda@919
    51
import java.util.ArrayList;
jlahoda@919
    52
import java.util.List;
jlahoda@919
    53
import javax.tools.DiagnosticListener;
jlahoda@919
    54
import javax.tools.JavaFileManager;
jlahoda@919
    55
import javax.tools.JavaFileObject;
jlahoda@919
    56
import org.netbeans.lib.nbjavac.services.NBAttr;
jlahoda@919
    57
import org.netbeans.lib.nbjavac.services.NBClassReader;
jlahoda@919
    58
import org.netbeans.lib.nbjavac.services.NBClassWriter;
jlahoda@919
    59
import org.netbeans.lib.nbjavac.services.NBJavacTrees;
jlahoda@919
    60
import org.netbeans.lib.nbjavac.services.NBJavadocEnter;
jlahoda@919
    61
import org.netbeans.lib.nbjavac.services.NBJavadocMemberEnter;
jlahoda@970
    62
import org.netbeans.lib.nbjavac.services.NBMessager;
jlahoda@919
    63
import org.netbeans.lib.nbjavac.services.NBParserFactory;
jlahoda@919
    64
import org.netbeans.lib.nbjavac.services.NBTreeMaker;
jlahoda@919
    65
import org.netbeans.modules.jackpot30.resolve.api.JavacCreator;
jlahoda@919
    66
import org.openide.util.lookup.ServiceProvider;
jlahoda@919
    67
jlahoda@919
    68
/**
jlahoda@919
    69
 *
jlahoda@919
    70
 * @author lahvac
jlahoda@919
    71
 */
jlahoda@919
    72
@ServiceProvider(service = JavacCreator.class)
jlahoda@919
    73
public class JavacCreatorImpl extends JavacCreator {
jlahoda@919
    74
jlahoda@919
    75
    @Override
jlahoda@919
    76
    protected JavacTaskImpl doCreate(Writer out, JavaFileManager fileManager, DiagnosticListener<? super JavaFileObject> diagnosticListener, Iterable<String> options, Iterable<String> classes, Iterable<? extends JavaFileObject> compilationUnits) {
jlahoda@919
    77
        List<String> realOptions = new ArrayList<String>();
jlahoda@919
    78
        for (String option : options) {
jlahoda@919
    79
            realOptions.add(option);
jlahoda@919
    80
        }
jlahoda@919
    81
        realOptions.add("-Xjcov"); //NOI18N, Make the compiler store end positions
jlahoda@919
    82
        realOptions.add("-XDallowStringFolding=false"); //NOI18N
jlahoda@919
    83
        realOptions.add("-XDshouldStopPolicy=GENERATE");   // NOI18N, parsing should not stop in phase where an error is found
jlahoda@919
    84
        realOptions.add("-XDsuppressAbortOnBadClassFile=true");
jlahoda@978
    85
        realOptions.add("-XDkeepComments=true"); //NOI18N
jlahoda@942
    86
        Context context = new Context();
jlahoda@942
    87
        //need to preregister the Messages here, because the getTask below requires Log instance:
jlahoda@970
    88
        NBMessager.preRegister(context, null, DEV_NULL, DEV_NULL, DEV_NULL);
jlahoda@942
    89
        JavacTaskImpl task = (JavacTaskImpl) JavacTool.create().getTask(out, fileManager, diagnosticListener, realOptions, classes, compilationUnits, context);
jlahoda@919
    90
        NBClassReader.preRegister(context, true);
jlahoda@919
    91
        NBAttr.preRegister(context);
jlahoda@919
    92
        NBClassWriter.preRegister(context);
jlahoda@919
    93
        NBParserFactory.preRegister(context);
jlahoda@919
    94
        NBTreeMaker.preRegister(context);
jlahoda@919
    95
        NBJavacTrees.preRegister(context);
jlahoda@919
    96
        NBJavadocEnter.preRegister(context);
jlahoda@919
    97
        NBJavadocMemberEnter.preRegister(context);
jlahoda@919
    98
jlahoda@919
    99
        return task;
jlahoda@919
   100
    }
jlahoda@919
   101
jlahoda@919
   102
    private static final PrintWriter DEV_NULL = new PrintWriter(new NullWriter(), false);
jlahoda@919
   103
jlahoda@919
   104
    private static class NullWriter extends Writer {
jlahoda@919
   105
jlahoda@919
   106
        public void write(char[] cbuf, int off, int len) throws IOException {
jlahoda@919
   107
        }
jlahoda@919
   108
jlahoda@919
   109
        public void flush() throws IOException {
jlahoda@919
   110
        }
jlahoda@919
   111
jlahoda@919
   112
        public void close() throws IOException {
jlahoda@919
   113
        }
jlahoda@919
   114
    }
jlahoda@919
   115
}