Adjusting standalone compiler such that several processors can be run
authorJan Lahoda <jlahoda@netbeans.org>
Sun, 16 Jan 2011 19:22:56 +0100
changeset 518b5c29f249d1f
parent 517 5b0b48cf992a
child 519 7c187dccaf45
Adjusting standalone compiler such that several processors can be run
cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/AbstractHintsAnnotationProcessing.java
cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessing.java
cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessingImpl.java
cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/CreateStandaloneCompilerJar.java
cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessingTest.java
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/AbstractHintsAnnotationProcessing.java	Sun Jan 16 19:22:56 2011 +0100
     1.3 @@ -0,0 +1,63 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2009-2011 Sun Microsystems, Inc. All rights reserved.
     1.8 + *
     1.9 + * The contents of this file are subject to the terms of either the GNU
    1.10 + * General Public License Version 2 only ("GPL") or the Common
    1.11 + * Development and Distribution License("CDDL") (collectively, the
    1.12 + * "License"). You may not use this file except in compliance with the
    1.13 + * License. You can obtain a copy of the License at
    1.14 + * http://www.netbeans.org/cddl-gplv2.html
    1.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.16 + * specific language governing permissions and limitations under the
    1.17 + * License.  When distributing the software, include this License Header
    1.18 + * Notice in each file and include the License file at
    1.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.20 + * particular file as subject to the "Classpath" exception as provided
    1.21 + * by Sun in the GPL Version 2 section of the License file that
    1.22 + * accompanied this code. If applicable, add the following below the
    1.23 + * License Header, with the fields enclosed by brackets [] replaced by
    1.24 + * your own identifying information:
    1.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.26 + *
    1.27 + * If you wish your version of this file to be governed by only the CDDL
    1.28 + * or only the GPL Version 2, indicate your decision by adding
    1.29 + * "[Contributor] elects to include this software in this distribution
    1.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.31 + * single choice of license, a recipient has the option to distribute
    1.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.33 + * to extend the choice of license to its licensees as provided above.
    1.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.35 + * Version 2 license, then the option applies only if the new code is
    1.36 + * made subject to such option by the copyright holder.
    1.37 + *
    1.38 + * Contributor(s):
    1.39 + *
    1.40 + * Portions Copyrighted 2009-2011 Sun Microsystems, Inc.
    1.41 + */
    1.42 +package org.netbeans.modules.jackpot30.compiler;
    1.43 +
    1.44 +import java.util.Set;
    1.45 +import javax.annotation.processing.ProcessingEnvironment;
    1.46 +import org.netbeans.api.java.source.CompilationInfoHack;
    1.47 +
    1.48 +/**
    1.49 + *
    1.50 + * @author lahvac
    1.51 + */
    1.52 +public abstract class AbstractHintsAnnotationProcessing {
    1.53 +
    1.54 +    protected abstract boolean initialize(ProcessingEnvironment processingEnv);
    1.55 +
    1.56 +    protected abstract void doProcess(CompilationInfoHack info, ProcessingEnvironment processingEnv, Reporter reporter);
    1.57 +
    1.58 +    protected abstract void finish();
    1.59 +
    1.60 +    protected abstract Set<String> getSupportedOptions();
    1.61 +
    1.62 +    protected static interface Reporter {
    1.63 +        public void warning(int offset, String message);
    1.64 +    }
    1.65 +
    1.66 +}
     2.1 --- a/cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessing.java	Sun Jan 16 19:22:56 2011 +0100
     2.2 +++ b/cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessing.java	Sun Jan 16 19:22:56 2011 +0100
     2.3 @@ -38,43 +38,9 @@
     2.4   */
     2.5  package org.netbeans.modules.jackpot30.compiler;
     2.6  
     2.7 -import com.sun.source.tree.ClassTree;
     2.8 -import com.sun.source.tree.CompilationUnitTree;
     2.9 -import com.sun.source.tree.IdentifierTree;
    2.10 -import com.sun.source.tree.MemberSelectTree;
    2.11 -import com.sun.source.tree.MethodTree;
    2.12 -import com.sun.source.tree.ParameterizedTypeTree;
    2.13 -import com.sun.source.tree.Tree;
    2.14 -import com.sun.source.util.SourcePositions;
    2.15 -import com.sun.source.util.TreePath;
    2.16 -import com.sun.source.util.TreeScanner;
    2.17 -import com.sun.source.util.Trees;
    2.18 -import com.sun.tools.javac.api.JavacTrees;
    2.19 -import com.sun.tools.javac.comp.Attr;
    2.20 -import com.sun.tools.javac.comp.AttrContext;
    2.21 -import com.sun.tools.javac.comp.Enter;
    2.22 -import com.sun.tools.javac.comp.Env;
    2.23 -import com.sun.tools.javac.jvm.Gen;
    2.24 -import com.sun.tools.javac.main.JavaCompiler;
    2.25 -import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    2.26 -import com.sun.tools.javac.tree.JCTree;
    2.27 -import com.sun.tools.javac.tree.JCTree.JCClassDecl;
    2.28 -import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    2.29 -import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
    2.30 -import com.sun.tools.javac.tree.JCTree.JCIdent;
    2.31 -import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
    2.32 -import com.sun.tools.javac.util.Context;
    2.33 -import com.sun.tools.javac.util.Log;
    2.34 -import com.sun.tools.javac.util.Options;
    2.35 -import java.io.ByteArrayInputStream;
    2.36  import java.io.File;
    2.37  import java.io.IOException;
    2.38 -import java.io.InputStream;
    2.39  import java.io.OutputStream;
    2.40 -import java.io.Reader;
    2.41 -import java.io.StringReader;
    2.42 -import java.lang.reflect.Field;
    2.43 -import java.net.URL;
    2.44  import java.util.Arrays;
    2.45  import java.util.Collection;
    2.46  import java.util.Collections;
    2.47 @@ -85,41 +51,18 @@
    2.48  import java.util.List;
    2.49  import java.util.Map;
    2.50  import java.util.Map.Entry;
    2.51 -import java.util.Properties;
    2.52 -import java.util.Queue;
    2.53  import java.util.Set;
    2.54  import java.util.concurrent.atomic.AtomicBoolean;
    2.55 -import java.util.prefs.AbstractPreferences;
    2.56 -import java.util.prefs.BackingStoreException;
    2.57 -import java.util.prefs.Preferences;
    2.58 -import java.util.prefs.PreferencesFactory;
    2.59  import java.util.regex.Pattern;
    2.60 -import javax.annotation.processing.AbstractProcessor;
    2.61 -import javax.annotation.processing.Processor;
    2.62 -import javax.annotation.processing.RoundEnvironment;
    2.63 -import javax.annotation.processing.SupportedAnnotationTypes;
    2.64 -import javax.annotation.processing.SupportedOptions;
    2.65 -import javax.lang.model.SourceVersion;
    2.66 -import javax.lang.model.element.Element;
    2.67 -import javax.lang.model.element.ElementKind;
    2.68 -import javax.lang.model.element.TypeElement;
    2.69 -import javax.swing.event.ChangeListener;
    2.70 +import javax.annotation.processing.ProcessingEnvironment;
    2.71  import javax.swing.text.BadLocationException;
    2.72  import javax.tools.Diagnostic.Kind;
    2.73  import javax.tools.FileObject;
    2.74 -import javax.tools.JavaFileManager;
    2.75 -import javax.tools.JavaFileObject;
    2.76 -import javax.tools.StandardJavaFileManager;
    2.77  import javax.tools.StandardLocation;
    2.78 -import org.netbeans.api.diff.Difference;
    2.79 -import org.netbeans.api.editor.mimelookup.MimePath;
    2.80  import org.netbeans.api.java.classpath.ClassPath;
    2.81 -import org.netbeans.api.java.queries.SourceForBinaryQuery;
    2.82 -import org.netbeans.api.java.source.ClasspathInfo;
    2.83 +import org.netbeans.api.java.source.ClasspathInfo.PathKind;
    2.84  import org.netbeans.api.java.source.CompilationInfoHack;
    2.85  import org.netbeans.api.java.source.ModificationResult;
    2.86 -import org.netbeans.modules.diff.builtin.provider.BuiltInDiffProvider;
    2.87 -import org.netbeans.modules.diff.builtin.visualizer.TextDiffVisualizer;
    2.88  import org.netbeans.modules.jackpot30.file.DeclarativeHintRegistry;
    2.89  import org.netbeans.modules.jackpot30.impl.RulesManager;
    2.90  import org.netbeans.modules.jackpot30.impl.Utilities;
    2.91 @@ -128,214 +71,126 @@
    2.92  import org.netbeans.modules.jackpot30.spi.HintDescription;
    2.93  import org.netbeans.modules.jackpot30.spi.HintMetadata;
    2.94  import org.netbeans.modules.jackpot30.spi.HintsRunner;
    2.95 -import org.netbeans.modules.jackpot30.spi.JavaFix;
    2.96 -import org.netbeans.modules.java.source.parsing.JavacParserFactory;
    2.97 -import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
    2.98 -import org.netbeans.spi.diff.DiffProvider;
    2.99  import org.netbeans.spi.editor.hints.ErrorDescription;
   2.100  import org.netbeans.spi.editor.hints.Fix;
   2.101 -import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
   2.102 -import org.netbeans.spi.java.classpath.support.ClassPathSupport;
   2.103 -import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
   2.104 -import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2;
   2.105  import org.openide.filesystems.FileUtil;
   2.106  import org.openide.util.Exceptions;
   2.107 -import org.openide.util.Lookup;
   2.108 -import org.openide.util.NbPreferences;
   2.109 -import org.openide.util.NbPreferences.Provider;
   2.110 -import org.openide.util.lookup.Lookups;
   2.111  import org.openide.util.lookup.ServiceProvider;
   2.112  
   2.113  /**
   2.114   *
   2.115   * @author lahvac
   2.116   */
   2.117 -@SupportedAnnotationTypes("*")
   2.118 -@ServiceProvider(service=Processor.class)
   2.119 -public class HintsAnnotationProcessing extends AbstractProcessor {
   2.120 +@ServiceProvider(service=AbstractHintsAnnotationProcessing.class)
   2.121 +public class HintsAnnotationProcessing extends AbstractHintsAnnotationProcessing {
   2.122  
   2.123 +    static final String CLASSPATH_HINTS_ENABLE = "jackpot30_enable_cp_hints";
   2.124 +    static final String CLASSPATH_HINTS_FIXES_ENABLE = "jackpot30_apply_cp_hints";
   2.125      static final String HARDCODED_HINTS_ENABLE = "jackpot30_enabled_hc_hints";
   2.126      static final String HARDCODED_HINTS_FIXES_ENABLE = "jackpot30_apply_hc_hints";
   2.127      static final String EXTRA_HINTS = "jackpot30_extra_hints";
   2.128  
   2.129      public static final Set<String> OPTIONS = Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
   2.130 -        "jackpot30_enable_cp_hints",
   2.131 -        "jackpot30_apply_cp_hints",
   2.132 +        CLASSPATH_HINTS_ENABLE, CLASSPATH_HINTS_FIXES_ENABLE,
   2.133          HARDCODED_HINTS_ENABLE,
   2.134          HARDCODED_HINTS_FIXES_ENABLE,
   2.135          EXTRA_HINTS
   2.136      )));
   2.137  
   2.138 -    private final Collection<Element> types = new LinkedList<Element>();
   2.139 +    private OutputStream diff;
   2.140  
   2.141      @Override
   2.142 -    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   2.143 -        System.err.println("Invoked");
   2.144 +    protected boolean initialize(ProcessingEnvironment processingEnv) {
   2.145 +        return true;
   2.146 +    }
   2.147 +
   2.148 +    @Override
   2.149 +    protected void doProcess(CompilationInfoHack info, ProcessingEnvironment processingEnv, Reporter reporter) {
   2.150 +        Set<HintDescription> hardCodedHints = new LinkedHashSet<HintDescription>();
   2.151 +
   2.152 +        for (Collection<? extends HintDescription> v : RulesManager.computeAllHints().values()) {
   2.153 +            hardCodedHints.addAll(v);
   2.154 +        }
   2.155 +
   2.156 +        ContainsChecker<String> enabledHints = getEnabledHardcodedHints(processingEnv);
   2.157 +
   2.158 +        for (Iterator<HintDescription> it = hardCodedHints.iterator(); it.hasNext(); ) {
   2.159 +            HintMetadata current = it.next().getMetadata();
   2.160 +
   2.161 +            if (   (current.kind == HintMetadata.Kind.HINT || current.kind == HintMetadata.Kind.HINT_NON_GUI)
   2.162 +                && enabledHints.contains(current.id)) {
   2.163 +                continue;
   2.164 +            }
   2.165 +
   2.166 +            it.remove();
   2.167 +        }
   2.168 +
   2.169 +        ContainsChecker<String> enabledApplyHints = getApplyHardcodedFixes(processingEnv);
   2.170 +
   2.171 +        List<HintDescription> hintDescriptions = new LinkedList<HintDescription>(hardCodedHints);
   2.172 +
   2.173 +        if (isEnabled(processingEnv, CLASSPATH_HINTS_ENABLE)) {
   2.174 +            hintDescriptions.addAll(new LinkedList<HintDescription>(Utilities.listClassPathHints(new HashSet<ClassPath>(Arrays.asList(info.getClasspathInfo().getClassPath(PathKind.COMPILE), info.getClasspathInfo().getClassPath(PathKind.SOURCE))))));
   2.175 +        }
   2.176 +
   2.177 +        boolean applyCPHints = isEnabled(processingEnv, CLASSPATH_HINTS_FIXES_ENABLE);
   2.178 +
   2.179 +        Collection<? extends HintDescription> extraHints = getExtraHints(processingEnv);
   2.180 +
   2.181 +        hintDescriptions.addAll(extraHints);
   2.182 +
   2.183 +        Map<HintDescription, List<ErrorDescription>> hints = HintsRunner.computeErrors(info, hintDescriptions, new AtomicBoolean());
   2.184 +
   2.185          try {
   2.186 -            return doProcess(annotations, roundEnv);
   2.187 -        } catch (Throwable ex) {
   2.188 -            ex.printStackTrace();
   2.189 -            return false;
   2.190 +            boolean fixPerformed = false;
   2.191 +
   2.192 +            for (Entry<HintDescription, List<ErrorDescription>> e : hints.entrySet()) {
   2.193 +                boolean applyFix = hardCodedHints.contains(e.getKey()) ? enabledApplyHints.contains(e.getKey().getMetadata().id) : extraHints.contains(e.getKey()) ? true : applyCPHints;
   2.194 +
   2.195 +                for (ErrorDescription ed : e.getValue()) {
   2.196 +                    reporter.warning(ed.getRange().getBegin().getOffset(), ed.getDescription());
   2.197 +
   2.198 +                    if (!applyFix) continue;
   2.199 +
   2.200 +                    Fix f = ed.getFixes().getFixes().get(0);
   2.201 +
   2.202 +                    if (!(f instanceof JavaFixImpl)) {
   2.203 +                        reporter.warning(ed.getRange().getBegin().getOffset(), "Cannot apply primary fix (not a JavaFix)");
   2.204 +                        continue;
   2.205 +                    }
   2.206 +
   2.207 +                    JavaFixImpl jfi = (JavaFixImpl) f;
   2.208 +
   2.209 +                    try {
   2.210 +                        JavaFixImpl.Accessor.INSTANCE.process(jfi.jf, info, false);
   2.211 +                    } catch (Exception ex) {
   2.212 +                        Exceptions.printStackTrace(ex);
   2.213 +                    }
   2.214 +
   2.215 +                    fixPerformed = true;
   2.216 +                }
   2.217 +            }
   2.218 +
   2.219 +            if (fixPerformed) {
   2.220 +                ModificationResult mr = info.computeResult();
   2.221 +
   2.222 +                if (diff == null) {
   2.223 +                    FileObject upgradeDiffFO = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", "META-INF/upgrade/upgrade.diff");
   2.224 +
   2.225 +                    diff = upgradeDiffFO.openOutputStream();
   2.226 +                }
   2.227 +
   2.228 +                BatchUtilities.exportDiff(mr, diff);
   2.229 +            }
   2.230 +        } catch (IOException ex) {
   2.231 +            Exceptions.printStackTrace(ex);
   2.232 +        } catch (BadLocationException ex) {
   2.233 +            Exceptions.printStackTrace(ex);
   2.234          }
   2.235      }
   2.236  
   2.237 -    private boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   2.238 -        types.addAll(roundEnv.getRootElements());
   2.239 -        
   2.240 -        if (!roundEnv.processingOver())
   2.241 -            return false;
   2.242 -
   2.243 -        try {
   2.244 -            File tmp = File.createTempFile("jackpot30", null);
   2.245 -
   2.246 -            tmp.delete();
   2.247 -            tmp.mkdirs();
   2.248 -            tmp.deleteOnExit();
   2.249 -
   2.250 -            tmp = FileUtil.normalizeFile(tmp);
   2.251 -            FileUtil.refreshFor(tmp.getParentFile());
   2.252 -
   2.253 -            org.openide.filesystems.FileObject tmpFO = FileUtil.toFileObject(tmp);
   2.254 -
   2.255 -            if (tmpFO == null) {
   2.256 -                return false;
   2.257 -            }
   2.258 -
   2.259 -            CacheFolder.setCacheFolder(tmpFO);
   2.260 -        } catch (IOException ex) {
   2.261 -            ex.printStackTrace();
   2.262 -        }
   2.263 -
   2.264 -        OutputStream diff = null;
   2.265 -        Context c = ((JavacProcessingEnvironment) processingEnv).getContext();
   2.266 -        StandardJavaFileManager s = (StandardJavaFileManager) c.get(JavaFileManager.class);
   2.267 -        ClassPath boot = computeClassPath(s, StandardLocation.PLATFORM_CLASS_PATH);
   2.268 -        ClassPath compile = computeClassPath(s, StandardLocation.CLASS_PATH);
   2.269 -        ClassPath source = computeClassPath(s, StandardLocation.SOURCE_PATH);
   2.270 -        Trees trees = JavacTrees.instance(c);
   2.271 -        Collection<CompilationUnitTree> toClean = new LinkedList<CompilationUnitTree>();
   2.272 -        Log log = Log.instance(c);
   2.273 -        
   2.274 -        for (Element el : types) {
   2.275 -            if (!el.getKind().isClass() && !el.getKind().isInterface()) {
   2.276 -//                processingEnv.getMessager().printMessage(Kind.NOTE, "Not a class", el);
   2.277 -                continue;
   2.278 -            }
   2.279 -
   2.280 -            TreePath elTree = trees.getPath(el);
   2.281 -            JCCompilationUnit cut = (JCCompilationUnit) elTree.getCompilationUnit();
   2.282 -
   2.283 -            if (!cut.sourcefile.toUri().isAbsolute()) {
   2.284 -                processingEnv.getMessager().printMessage(Kind.NOTE, "Not an absolute URI: " + cut.sourcefile.toUri().toASCIIString(), el);
   2.285 -                continue; //XXX
   2.286 -            }
   2.287 -
   2.288 -            toClean.add(cut);
   2.289 -
   2.290 -            doAttribute(c, cut);
   2.291 -            
   2.292 -            int nerrors = log.nerrors;
   2.293 -            int nwarnings = log.nwarnings;
   2.294 -            
   2.295 -            CompilationInfoHack info = new CompilationInfoHack(c, ClasspathInfo.create(boot, compile, source), cut);
   2.296 -            Set<HintDescription> hardCodedHints = new LinkedHashSet<HintDescription>();
   2.297 -
   2.298 -            for (Collection<? extends HintDescription> v : RulesManager.computeAllHints().values()) {
   2.299 -                hardCodedHints.addAll(v);
   2.300 -            }
   2.301 -
   2.302 -            ContainsChecker<String> enabledHints = getEnabledHardcodedHints();
   2.303 -            
   2.304 -            for (Iterator<HintDescription> it = hardCodedHints.iterator(); it.hasNext(); ) {
   2.305 -                HintMetadata current = it.next().getMetadata();
   2.306 -
   2.307 -                if (   (current.kind == HintMetadata.Kind.HINT || current.kind == HintMetadata.Kind.HINT_NON_GUI)
   2.308 -                    && enabledHints.contains(current.id)) {
   2.309 -                    continue;
   2.310 -                }
   2.311 -                
   2.312 -                it.remove();
   2.313 -            }
   2.314 -
   2.315 -            ContainsChecker<String> enabledApplyHints = getApplyHardcodedFixes();
   2.316 -
   2.317 -            List<HintDescription> hintDescriptions = new LinkedList<HintDescription>(hardCodedHints);
   2.318 -
   2.319 -            if (isEnabled("jackpot30_enable_cp_hints")) {
   2.320 -                hintDescriptions.addAll(new LinkedList<HintDescription>(Utilities.listClassPathHints(new HashSet<ClassPath>(Arrays.asList(compile, source)))));
   2.321 -            }
   2.322 -
   2.323 -            boolean applyCPHints = isEnabled("jackpot30_apply_cp_hints");
   2.324 -
   2.325 -            Collection<? extends HintDescription> extraHints = getExtraHints();
   2.326 -
   2.327 -            hintDescriptions.addAll(extraHints);
   2.328 -
   2.329 -            Map<HintDescription, List<ErrorDescription>> hints = HintsRunner.computeErrors(info, hintDescriptions, new AtomicBoolean());
   2.330 -
   2.331 -            log.nerrors = nerrors;
   2.332 -            log.nwarnings = nwarnings;
   2.333 -
   2.334 -            JavaFileObject currentSource = log.currentSourceFile();
   2.335 -
   2.336 -            try {
   2.337 -                boolean fixPerformed = false;
   2.338 -
   2.339 -                log.useSource(cut.sourcefile);
   2.340 -
   2.341 -                for (Entry<HintDescription, List<ErrorDescription>> e : hints.entrySet()) {
   2.342 -                    boolean applyFix = hardCodedHints.contains(e.getKey()) ? enabledApplyHints.contains(e.getKey().getMetadata().id) : extraHints.contains(e.getKey()) ? true : applyCPHints;
   2.343 -                    
   2.344 -                    for (ErrorDescription ed : e.getValue()) {
   2.345 -                        log.warning(ed.getRange().getBegin().getOffset(), "proc.messager", ed.getDescription());
   2.346 -
   2.347 -                        if (!applyFix) continue;
   2.348 -
   2.349 -                        nerrors = log.nerrors;
   2.350 -                        nwarnings = log.nwarnings;
   2.351 -                        Fix f = ed.getFixes().getFixes().get(0);
   2.352 -
   2.353 -                        if (!(f instanceof JavaFixImpl)) {
   2.354 -                            log.warning(ed.getRange().getBegin().getOffset(), "proc.messager", "Cannot apply primary fix (not a JavaFix)");
   2.355 -                            continue;
   2.356 -                        }
   2.357 -
   2.358 -                        JavaFixImpl jfi = (JavaFixImpl) f;
   2.359 -
   2.360 -                        try {
   2.361 -                            JavaFixImpl.Accessor.INSTANCE.process(jfi.jf, info, false);
   2.362 -                        } catch (Exception ex) {
   2.363 -                            Exceptions.printStackTrace(ex);
   2.364 -                        } finally {
   2.365 -                            log.nerrors = nerrors;
   2.366 -                            log.nwarnings = nwarnings;
   2.367 -                        }
   2.368 -
   2.369 -                        fixPerformed = true;
   2.370 -                    }
   2.371 -                }
   2.372 -
   2.373 -                if (fixPerformed) {
   2.374 -                    ModificationResult mr = info.computeResult();
   2.375 -
   2.376 -                    if (diff == null) {
   2.377 -                        FileObject upgradeDiffFO = processingEnv.getFiler().createResource(StandardLocation.SOURCE_OUTPUT, "", "META-INF/upgrade/upgrade.diff");
   2.378 -
   2.379 -                        diff = upgradeDiffFO.openOutputStream();
   2.380 -                    }
   2.381 -
   2.382 -                    BatchUtilities.exportDiff(mr, diff);
   2.383 -                }
   2.384 -            } catch (IOException ex) {
   2.385 -                Exceptions.printStackTrace(ex);
   2.386 -            } catch (BadLocationException ex) {
   2.387 -                Exceptions.printStackTrace(ex);
   2.388 -            } finally {
   2.389 -                log.useSource(currentSource);
   2.390 -            }
   2.391 -        }
   2.392 -
   2.393 -        //XXX: should be in finally!
   2.394 +    @Override
   2.395 +    protected void finish() {
   2.396          if (diff != null) {
   2.397              try {
   2.398                  diff.close();
   2.399 @@ -344,26 +199,7 @@
   2.400              }
   2.401          }
   2.402  
   2.403 -        for (CompilationUnitTree cut : toClean) {
   2.404 -            new ThoroughTreeCleaner(cut, trees.getSourcePositions()).scan(cut, null);
   2.405 -        }
   2.406 -
   2.407 -        try {
   2.408 -            //XXX: workarounding a bug in CRTable (see HintsAnnotationProcessingTest.testCRTable):
   2.409 -            Options.instance(c).remove("-Xjcov");
   2.410 -            Field f = Gen.class.getDeclaredField("genCrt");
   2.411 -            f.setAccessible(true);
   2.412 -            f.set(Gen.instance(c), false);
   2.413 -        } catch (Exception e) {
   2.414 -            e.printStackTrace();
   2.415 -        }
   2.416 -        
   2.417 -        return false;
   2.418 -    }
   2.419 -
   2.420 -    @Override
   2.421 -    public SourceVersion getSupportedSourceVersion() {
   2.422 -        return SourceVersion.latest();
   2.423 +        diff = null;
   2.424      }
   2.425  
   2.426      @Override
   2.427 @@ -371,62 +207,7 @@
   2.428          return OPTIONS;
   2.429      }
   2.430  
   2.431 -    private static ClassPath computeClassPath(StandardJavaFileManager m, StandardLocation kind) {
   2.432 -        List<URL> urls = new LinkedList<URL>();
   2.433 -        Iterable<? extends File> files = m.getLocation(kind);
   2.434 -
   2.435 -        if (files != null) {
   2.436 -            for (File f : files) {
   2.437 -                urls.add(FileUtil.urlForArchiveOrDir(f));
   2.438 -            }
   2.439 -        }
   2.440 -
   2.441 -        return ClassPathSupport.createClassPath(urls.toArray(new URL[0]));
   2.442 -    }
   2.443 -
   2.444 -    private static void doAttribute(Context c, JCCompilationUnit cut) {
   2.445 -        JavaCompiler jc = JavaCompiler.instance(c);
   2.446 -        final Enter enter = Enter.instance(c);
   2.447 -        final Queue<Env<AttrContext>> queued = new LinkedList<Env<AttrContext>>();
   2.448 -
   2.449 -        queued.add(enter.getTopLevelEnv(cut));
   2.450 -
   2.451 -        new TreeScanner<Void, Void>() {
   2.452 -            @Override
   2.453 -            public Void visitClass(ClassTree node, Void p) {
   2.454 -                Env<AttrContext> env = enter.getEnv(((JCClassDecl) node).sym);
   2.455 -
   2.456 -                if (env != null)
   2.457 -                    queued.add(env);
   2.458 -
   2.459 -                return super.visitClass(node, p);
   2.460 -            }
   2.461 -        }.scan(cut, null);
   2.462 -
   2.463 -        Attr attr = Attr.instance(c);
   2.464 -
   2.465 -        for (Env<AttrContext> env : queued) {
   2.466 -            attr.attribClass(env.tree.pos(), env.enclClass.sym);
   2.467 -        }
   2.468 -    }
   2.469 -
   2.470 -
   2.471 -
   2.472 -    static {
   2.473 -        try {
   2.474 -            ClassLoader l = HintsAnnotationProcessing.class.getClassLoader();
   2.475 -
   2.476 -            if (l == null) {
   2.477 -                l = ClassLoader.getSystemClassLoader();
   2.478 -            }
   2.479 -
   2.480 -            l.setClassAssertionStatus("org.netbeans.api.java.source.CompilationInfo", false);
   2.481 -        } catch (Throwable t) {
   2.482 -            t.printStackTrace();
   2.483 -        }
   2.484 -    }
   2.485 -
   2.486 -    private boolean isEnabled(String key) {
   2.487 +    private boolean isEnabled(ProcessingEnvironment processingEnv, String key) {
   2.488          if (processingEnv.getOptions().containsKey(key)) {
   2.489              return Boolean.valueOf(processingEnv.getOptions().get(key));
   2.490          }
   2.491 @@ -434,7 +215,7 @@
   2.492          return true;
   2.493      }
   2.494  
   2.495 -    private ContainsChecker<String> getApplyHardcodedFixes() {
   2.496 +    private ContainsChecker<String> getApplyHardcodedFixes(ProcessingEnvironment processingEnv) {
   2.497          if (processingEnv.getOptions().containsKey(HARDCODED_HINTS_FIXES_ENABLE)) {
   2.498              String toSplit = processingEnv.getOptions().get(HARDCODED_HINTS_FIXES_ENABLE);
   2.499  
   2.500 @@ -448,7 +229,7 @@
   2.501          return new HardcodedContainsChecker<String>(true);
   2.502      }
   2.503  
   2.504 -    private ContainsChecker<String> getEnabledHardcodedHints() {
   2.505 +    private ContainsChecker<String> getEnabledHardcodedHints(ProcessingEnvironment processingEnv) {
   2.506          if (processingEnv.getOptions().containsKey(HARDCODED_HINTS_ENABLE)) {
   2.507              String toSplit = processingEnv.getOptions().get(HARDCODED_HINTS_ENABLE);
   2.508  
   2.509 @@ -507,7 +288,7 @@
   2.510          }
   2.511      }
   2.512  
   2.513 -    private Collection<? extends HintDescription> getExtraHints() {
   2.514 +    private Collection<? extends HintDescription> getExtraHints(ProcessingEnvironment processingEnv) {
   2.515          if (processingEnv.getOptions().containsKey(EXTRA_HINTS)) {
   2.516              String toSplit = processingEnv.getOptions().get(EXTRA_HINTS);
   2.517  
   2.518 @@ -536,225 +317,4 @@
   2.519          return Collections.emptyList();
   2.520      }
   2.521  
   2.522 -    private static final class ThoroughTreeCleaner extends TreeScanner<Void, Void> {
   2.523 -
   2.524 -        private final CompilationUnitTree cut;
   2.525 -        private final SourcePositions positions;
   2.526 -
   2.527 -        public ThoroughTreeCleaner(CompilationUnitTree cut, SourcePositions positions) {
   2.528 -            this.cut = cut;
   2.529 -            this.positions = positions;
   2.530 -        }
   2.531 -
   2.532 -        @Override
   2.533 -        public Void scan(Tree node, Void p) {
   2.534 -            if (node != null) ((JCTree) node).type = null;
   2.535 -            return super.scan(node, p);
   2.536 -        }
   2.537 -
   2.538 -        @Override
   2.539 -        public Void visitParameterizedType(ParameterizedTypeTree node, Void p) {
   2.540 -            return super.visitParameterizedType(node, p);
   2.541 -        }
   2.542 -
   2.543 -        @Override
   2.544 -        public Void visitMemberSelect(MemberSelectTree node, Void p) {
   2.545 -            ((JCFieldAccess) node).sym = null;
   2.546 -            return super.visitMemberSelect(node, p);
   2.547 -        }
   2.548 -
   2.549 -        @Override
   2.550 -        public Void visitIdentifier(IdentifierTree node, Void p) {
   2.551 -            ((JCIdent) node).sym = null;
   2.552 -            return super.visitIdentifier(node, p);
   2.553 -        }
   2.554 -
   2.555 -        @Override
   2.556 -        public Void visitMethod(MethodTree node, Void p) {
   2.557 -            JCMethodDecl method = (JCMethodDecl) node;
   2.558 -
   2.559 -            if (method.sym != null && method.sym.getKind() == ElementKind.CONSTRUCTOR && method.sym.owner.getKind() == ElementKind.ENUM) {
   2.560 -                if (positions.getEndPosition(cut, method.body.stats.head) == (-1)) {
   2.561 -                    method.body.stats = method.body.stats.tail;
   2.562 -                }
   2.563 -            }
   2.564 -
   2.565 -            return super.visitMethod(node, p);
   2.566 -        }
   2.567 -
   2.568 -        @Override
   2.569 -        public Void visitClass(ClassTree node, Void p) {
   2.570 -            JCClassDecl decl = (JCClassDecl) node;
   2.571 -
   2.572 -            if (decl.sym.getKind() == ElementKind.ENUM) {
   2.573 -                if (positions.getEndPosition(cut, decl.defs.head) == (-1)) {
   2.574 -                    decl.defs = decl.defs.tail;
   2.575 -                }
   2.576 -            }
   2.577 -            return super.visitClass(node, p);
   2.578 -        }
   2.579 -
   2.580 -    }
   2.581 -
   2.582 -    @ServiceProvider(service=MimeDataProvider.class)
   2.583 -    public static final class MimeDataProviderImpl implements MimeDataProvider {
   2.584 -
   2.585 -        private static final Lookup L = Lookups.fixed(new JavacParserFactory());
   2.586 -
   2.587 -        public Lookup getLookup(MimePath mimePath) {
   2.588 -            if ("text/x-java".equals(mimePath.getPath()))
   2.589 -                return L;
   2.590 -            return null;
   2.591 -        }
   2.592 -        
   2.593 -    }
   2.594 -
   2.595 -    public static final class StandaloneMimeDataProviderImpl implements MimeDataProvider {
   2.596 -
   2.597 -        private static final Lookup L = Lookups.fixed(NbPreferences.forModule(HintsAnnotationProcessing.class), new JavacParserFactory());
   2.598 -
   2.599 -        public Lookup getLookup(MimePath mimePath) {
   2.600 -            if ("text/x-java".equals(mimePath.getPath()))
   2.601 -                return L;
   2.602 -            return null;
   2.603 -        }
   2.604 -
   2.605 -    }
   2.606 -
   2.607 -    @ServiceProvider(service=SourceForBinaryQueryImplementation.class, position=0)
   2.608 -    public static final class EmptySourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation2 {
   2.609 -        public Result findSourceRoots2(URL binaryRoot) {
   2.610 -            return INSTANCE;
   2.611 -        }
   2.612 -        public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
   2.613 -            return findSourceRoots2(binaryRoot);
   2.614 -        }
   2.615 -        private static final Result INSTANCE = new Result() {
   2.616 -            public boolean preferSources() {
   2.617 -                return false;
   2.618 -            }
   2.619 -            public org.openide.filesystems.FileObject[] getRoots() {
   2.620 -                return new org.openide.filesystems.FileObject[0];
   2.621 -            }
   2.622 -            public void addChangeListener(ChangeListener l) {}
   2.623 -            public void removeChangeListener(ChangeListener l) {}
   2.624 -        };
   2.625 -    }
   2.626 -
   2.627 -    public static class PreferencesProvider implements Provider {
   2.628 -
   2.629 -        private final MemoryPreferencesFactory f;
   2.630 -
   2.631 -        public PreferencesProvider() {
   2.632 -            this.f = new MemoryPreferencesFactory();
   2.633 -        }
   2.634 -
   2.635 -        @Override
   2.636 -        public Preferences preferencesForModule(Class cls) {
   2.637 -            return f.userRoot().node(cls.getPackage().getName());
   2.638 -        }
   2.639 -
   2.640 -        @Override
   2.641 -        public Preferences preferencesRoot() {
   2.642 -            return f.userRoot();
   2.643 -        }
   2.644 -
   2.645 -    }
   2.646 -    //copied from NB junit:
   2.647 -    public static class MemoryPreferencesFactory implements PreferencesFactory {
   2.648 -        /** Creates a new instance  */
   2.649 -        public MemoryPreferencesFactory() {}
   2.650 -
   2.651 -        public Preferences userRoot() {
   2.652 -            return NbPreferences.userRootImpl();
   2.653 -        }
   2.654 -
   2.655 -        public Preferences systemRoot() {
   2.656 -            return NbPreferences.systemRootImpl();
   2.657 -        }
   2.658 -
   2.659 -        private static class NbPreferences extends AbstractPreferences {
   2.660 -            private static Preferences USER_ROOT;
   2.661 -            private static Preferences SYSTEM_ROOT;
   2.662 -
   2.663 -            /*private*/Properties properties;
   2.664 -
   2.665 -            static Preferences userRootImpl() {
   2.666 -                if (USER_ROOT == null) {
   2.667 -                    USER_ROOT = new NbPreferences();
   2.668 -                }
   2.669 -                return USER_ROOT;
   2.670 -            }
   2.671 -
   2.672 -            static Preferences systemRootImpl() {
   2.673 -                if (SYSTEM_ROOT == null) {
   2.674 -                    SYSTEM_ROOT = new NbPreferences();
   2.675 -                }
   2.676 -                return SYSTEM_ROOT;
   2.677 -            }
   2.678 -
   2.679 -
   2.680 -            private NbPreferences() {
   2.681 -                super(null, "");
   2.682 -            }
   2.683 -
   2.684 -            /** Creates a new instance of PreferencesImpl */
   2.685 -            private  NbPreferences(NbPreferences parent, String name)  {
   2.686 -                super(parent, name);
   2.687 -                newNode = true;
   2.688 -            }
   2.689 -
   2.690 -            protected final String getSpi(String key) {
   2.691 -                return properties().getProperty(key);
   2.692 -            }
   2.693 -
   2.694 -            protected final String[] childrenNamesSpi() throws BackingStoreException {
   2.695 -                return new String[0];
   2.696 -            }
   2.697 -
   2.698 -            protected final String[] keysSpi() throws BackingStoreException {
   2.699 -                return properties().keySet().toArray(new String[0]);
   2.700 -            }
   2.701 -
   2.702 -            protected final void putSpi(String key, String value) {
   2.703 -                properties().put(key,value);
   2.704 -            }
   2.705 -
   2.706 -            protected final void removeSpi(String key) {
   2.707 -                properties().remove(key);
   2.708 -            }
   2.709 -
   2.710 -            protected final void removeNodeSpi() throws BackingStoreException {}
   2.711 -            protected  void flushSpi() throws BackingStoreException {}
   2.712 -            protected void syncSpi() throws BackingStoreException {
   2.713 -                properties().clear();
   2.714 -            }
   2.715 -
   2.716 -            @Override
   2.717 -            public void put(String key, String value) {
   2.718 -                try {
   2.719 -                    super.put(key, value);
   2.720 -                } catch (IllegalArgumentException iae) {
   2.721 -                    if (iae.getMessage().contains("too long")) {
   2.722 -                        // Not for us!
   2.723 -                        putSpi(key, value);
   2.724 -                    } else {
   2.725 -                        throw iae;
   2.726 -                    }
   2.727 -                }
   2.728 -            }
   2.729 -
   2.730 -            Properties properties()  {
   2.731 -                if (properties == null) {
   2.732 -                    properties = new Properties();
   2.733 -                }
   2.734 -                return properties;
   2.735 -            }
   2.736 -
   2.737 -            protected AbstractPreferences childSpi(String name) {
   2.738 -                return new NbPreferences(this, name);
   2.739 -            }
   2.740 -        }
   2.741 -
   2.742 -    }
   2.743  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/cmdline/compiler/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessingImpl.java	Sun Jan 16 19:22:56 2011 +0100
     3.3 @@ -0,0 +1,533 @@
     3.4 +/*
     3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3.6 + *
     3.7 + * Copyright 2009-2011 Sun Microsystems, Inc. All rights reserved.
     3.8 + *
     3.9 + * The contents of this file are subject to the terms of either the GNU
    3.10 + * General Public License Version 2 only ("GPL") or the Common
    3.11 + * Development and Distribution License("CDDL") (collectively, the
    3.12 + * "License"). You may not use this file except in compliance with the
    3.13 + * License. You can obtain a copy of the License at
    3.14 + * http://www.netbeans.org/cddl-gplv2.html
    3.15 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    3.16 + * specific language governing permissions and limitations under the
    3.17 + * License.  When distributing the software, include this License Header
    3.18 + * Notice in each file and include the License file at
    3.19 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    3.20 + * particular file as subject to the "Classpath" exception as provided
    3.21 + * by Sun in the GPL Version 2 section of the License file that
    3.22 + * accompanied this code. If applicable, add the following below the
    3.23 + * License Header, with the fields enclosed by brackets [] replaced by
    3.24 + * your own identifying information:
    3.25 + * "Portions Copyrighted [year] [name of copyright owner]"
    3.26 + *
    3.27 + * If you wish your version of this file to be governed by only the CDDL
    3.28 + * or only the GPL Version 2, indicate your decision by adding
    3.29 + * "[Contributor] elects to include this software in this distribution
    3.30 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    3.31 + * single choice of license, a recipient has the option to distribute
    3.32 + * your version of this file under either the CDDL, the GPL Version 2 or
    3.33 + * to extend the choice of license to its licensees as provided above.
    3.34 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    3.35 + * Version 2 license, then the option applies only if the new code is
    3.36 + * made subject to such option by the copyright holder.
    3.37 + *
    3.38 + * Contributor(s):
    3.39 + *
    3.40 + * Portions Copyrighted 2009-2011 Sun Microsystems, Inc.
    3.41 + */
    3.42 +package org.netbeans.modules.jackpot30.compiler;
    3.43 +
    3.44 +import com.sun.source.tree.ClassTree;
    3.45 +import com.sun.source.tree.CompilationUnitTree;
    3.46 +import com.sun.source.tree.IdentifierTree;
    3.47 +import com.sun.source.tree.MemberSelectTree;
    3.48 +import com.sun.source.tree.MethodTree;
    3.49 +import com.sun.source.tree.ParameterizedTypeTree;
    3.50 +import com.sun.source.tree.Tree;
    3.51 +import com.sun.source.util.SourcePositions;
    3.52 +import com.sun.source.util.TreePath;
    3.53 +import com.sun.source.util.TreeScanner;
    3.54 +import com.sun.source.util.Trees;
    3.55 +import com.sun.tools.javac.api.JavacTrees;
    3.56 +import com.sun.tools.javac.comp.Attr;
    3.57 +import com.sun.tools.javac.comp.AttrContext;
    3.58 +import com.sun.tools.javac.comp.Enter;
    3.59 +import com.sun.tools.javac.comp.Env;
    3.60 +import com.sun.tools.javac.jvm.Gen;
    3.61 +import com.sun.tools.javac.main.JavaCompiler;
    3.62 +import com.sun.tools.javac.processing.JavacProcessingEnvironment;
    3.63 +import com.sun.tools.javac.tree.JCTree;
    3.64 +import com.sun.tools.javac.tree.JCTree.JCClassDecl;
    3.65 +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
    3.66 +import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
    3.67 +import com.sun.tools.javac.tree.JCTree.JCIdent;
    3.68 +import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
    3.69 +import com.sun.tools.javac.util.Context;
    3.70 +import com.sun.tools.javac.util.Log;
    3.71 +import com.sun.tools.javac.util.Options;
    3.72 +import java.io.File;
    3.73 +import java.io.IOException;
    3.74 +import java.lang.reflect.Field;
    3.75 +import java.net.URL;
    3.76 +import java.util.ArrayList;
    3.77 +import java.util.Collection;
    3.78 +import java.util.HashSet;
    3.79 +import java.util.LinkedList;
    3.80 +import java.util.List;
    3.81 +import java.util.Properties;
    3.82 +import java.util.Queue;
    3.83 +import java.util.Set;
    3.84 +import java.util.prefs.AbstractPreferences;
    3.85 +import java.util.prefs.BackingStoreException;
    3.86 +import java.util.prefs.Preferences;
    3.87 +import java.util.prefs.PreferencesFactory;
    3.88 +import javax.annotation.processing.AbstractProcessor;
    3.89 +import javax.annotation.processing.Processor;
    3.90 +import javax.annotation.processing.RoundEnvironment;
    3.91 +import javax.annotation.processing.SupportedAnnotationTypes;
    3.92 +import javax.lang.model.SourceVersion;
    3.93 +import javax.lang.model.element.Element;
    3.94 +import javax.lang.model.element.ElementKind;
    3.95 +import javax.lang.model.element.TypeElement;
    3.96 +import javax.swing.event.ChangeListener;
    3.97 +import javax.tools.Diagnostic.Kind;
    3.98 +import javax.tools.JavaFileManager;
    3.99 +import javax.tools.JavaFileObject;
   3.100 +import javax.tools.StandardJavaFileManager;
   3.101 +import javax.tools.StandardLocation;
   3.102 +import org.netbeans.api.editor.mimelookup.MimePath;
   3.103 +import org.netbeans.api.java.classpath.ClassPath;
   3.104 +import org.netbeans.api.java.queries.SourceForBinaryQuery;
   3.105 +import org.netbeans.api.java.source.ClasspathInfo;
   3.106 +import org.netbeans.api.java.source.CompilationInfoHack;
   3.107 +import org.netbeans.modules.jackpot30.compiler.AbstractHintsAnnotationProcessing.Reporter;
   3.108 +import org.netbeans.modules.java.source.parsing.JavacParserFactory;
   3.109 +import org.netbeans.modules.parsing.impl.indexing.CacheFolder;
   3.110 +import org.netbeans.spi.editor.mimelookup.MimeDataProvider;
   3.111 +import org.netbeans.spi.java.classpath.support.ClassPathSupport;
   3.112 +import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation;
   3.113 +import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation2;
   3.114 +import org.openide.filesystems.FileUtil;
   3.115 +import org.openide.util.Lookup;
   3.116 +import org.openide.util.NbPreferences;
   3.117 +import org.openide.util.NbPreferences.Provider;
   3.118 +import org.openide.util.lookup.Lookups;
   3.119 +import org.openide.util.lookup.ServiceProvider;
   3.120 +
   3.121 +/**
   3.122 + *
   3.123 + * @author lahvac
   3.124 + */
   3.125 +@SupportedAnnotationTypes("*")
   3.126 +@ServiceProvider(service=Processor.class)
   3.127 +public final class HintsAnnotationProcessingImpl extends AbstractProcessor {
   3.128 +
   3.129 +    private final Collection<Element> types = new LinkedList<Element>();
   3.130 +
   3.131 +    @Override
   3.132 +    public final boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   3.133 +        try {
   3.134 +            return doProcess(annotations, roundEnv);
   3.135 +        } catch (Throwable ex) {
   3.136 +            ex.printStackTrace();
   3.137 +            return false;
   3.138 +        }
   3.139 +    }
   3.140 +
   3.141 +    private boolean doProcess(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
   3.142 +        types.addAll(roundEnv.getRootElements());
   3.143 +        
   3.144 +        if (!roundEnv.processingOver())
   3.145 +            return false;
   3.146 +
   3.147 +        try {
   3.148 +            File tmp = File.createTempFile("jackpot30", null);
   3.149 +
   3.150 +            tmp.delete();
   3.151 +            tmp.mkdirs();
   3.152 +            tmp.deleteOnExit();
   3.153 +
   3.154 +            tmp = FileUtil.normalizeFile(tmp);
   3.155 +            FileUtil.refreshFor(tmp.getParentFile());
   3.156 +
   3.157 +            org.openide.filesystems.FileObject tmpFO = FileUtil.toFileObject(tmp);
   3.158 +
   3.159 +            if (tmpFO == null) {
   3.160 +                return false;
   3.161 +            }
   3.162 +
   3.163 +            CacheFolder.setCacheFolder(tmpFO);
   3.164 +        } catch (IOException ex) {
   3.165 +            ex.printStackTrace();
   3.166 +        }
   3.167 +
   3.168 +        Context c = ((JavacProcessingEnvironment) processingEnv).getContext();
   3.169 +        StandardJavaFileManager s = (StandardJavaFileManager) c.get(JavaFileManager.class);
   3.170 +        ClassPath boot = computeClassPath(s, StandardLocation.PLATFORM_CLASS_PATH);
   3.171 +        ClassPath compile = computeClassPath(s, StandardLocation.CLASS_PATH);
   3.172 +        ClassPath source = computeClassPath(s, StandardLocation.SOURCE_PATH);
   3.173 +        Trees trees = JavacTrees.instance(c);
   3.174 +        Collection<CompilationUnitTree> toClean = new LinkedList<CompilationUnitTree>();
   3.175 +        final Log log = Log.instance(c);
   3.176 +
   3.177 +        List<AbstractHintsAnnotationProcessing> processors = new ArrayList<AbstractHintsAnnotationProcessing>();
   3.178 +
   3.179 +        for (AbstractHintsAnnotationProcessing p : Lookup.getDefault().lookupAll(AbstractHintsAnnotationProcessing.class)) {
   3.180 +            if (p.initialize(processingEnv)) {
   3.181 +                processors.add(p);
   3.182 +            }
   3.183 +        }
   3.184 +
   3.185 +        try {
   3.186 +            for (Element el : types) {
   3.187 +                if (!el.getKind().isClass() && !el.getKind().isInterface()) {
   3.188 +    //                processingEnv.getMessager().printMessage(Kind.NOTE, "Not a class", el);
   3.189 +                    continue;
   3.190 +                }
   3.191 +
   3.192 +                TreePath elTree = trees.getPath(el);
   3.193 +                JCCompilationUnit cut = (JCCompilationUnit) elTree.getCompilationUnit();
   3.194 +
   3.195 +                if (!cut.sourcefile.toUri().isAbsolute()) {
   3.196 +                    processingEnv.getMessager().printMessage(Kind.NOTE, "Not an absolute URI: " + cut.sourcefile.toUri().toASCIIString(), el);
   3.197 +                    continue; //XXX
   3.198 +                }
   3.199 +
   3.200 +                toClean.add(cut);
   3.201 +
   3.202 +                doAttribute(c, cut);
   3.203 +
   3.204 +                CompilationInfoHack info = new CompilationInfoHack(c, ClasspathInfo.create(boot, compile, source), cut);
   3.205 +                JavaFileObject origSourceFile = log.currentSourceFile();
   3.206 +
   3.207 +                try {
   3.208 +                    log.useSource(cut.sourcefile);
   3.209 +
   3.210 +                    for (AbstractHintsAnnotationProcessing p : processors) {
   3.211 +                        p.doProcess(info, processingEnv, new Reporter() {
   3.212 +                            @Override public void warning(int offset, String message) {
   3.213 +                                log.warning(offset, "proc.messager", message);
   3.214 +                            }
   3.215 +                        });
   3.216 +                    }
   3.217 +                } finally {
   3.218 +                    log.useSource(origSourceFile);
   3.219 +                }
   3.220 +            }
   3.221 +        } finally {
   3.222 +            for (AbstractHintsAnnotationProcessing p : processors) {
   3.223 +                p.finish();
   3.224 +            }
   3.225 +        }
   3.226 +
   3.227 +        for (CompilationUnitTree cut : toClean) {
   3.228 +            new ThoroughTreeCleaner(cut, trees.getSourcePositions()).scan(cut, null);
   3.229 +        }
   3.230 +
   3.231 +        try {
   3.232 +            //XXX: workarounding a bug in CRTable (see HintsAnnotationProcessingTest.testCRTable):
   3.233 +            Options.instance(c).remove("-Xjcov");
   3.234 +            Field f = Gen.class.getDeclaredField("genCrt");
   3.235 +            f.setAccessible(true);
   3.236 +            f.set(Gen.instance(c), false);
   3.237 +        } catch (Exception e) {
   3.238 +            e.printStackTrace();
   3.239 +        }
   3.240 +        
   3.241 +        return false;
   3.242 +    }
   3.243 +
   3.244 +    @Override
   3.245 +    public SourceVersion getSupportedSourceVersion() {
   3.246 +        return SourceVersion.latest();
   3.247 +    }
   3.248 +
   3.249 +    @Override
   3.250 +    public Set<String> getSupportedOptions() {
   3.251 +        Set<String> options = new HashSet<String>();
   3.252 +
   3.253 +        for (AbstractHintsAnnotationProcessing p : Lookup.getDefault().lookupAll(AbstractHintsAnnotationProcessing.class)) {
   3.254 +            options.addAll(p.getSupportedOptions());
   3.255 +        }
   3.256 +
   3.257 +        return options;
   3.258 +    }
   3.259 +
   3.260 +    private static ClassPath computeClassPath(StandardJavaFileManager m, StandardLocation kind) {
   3.261 +        List<URL> urls = new LinkedList<URL>();
   3.262 +        Iterable<? extends File> files = m.getLocation(kind);
   3.263 +
   3.264 +        if (files != null) {
   3.265 +            for (File f : files) {
   3.266 +                urls.add(FileUtil.urlForArchiveOrDir(f));
   3.267 +            }
   3.268 +        }
   3.269 +
   3.270 +        return ClassPathSupport.createClassPath(urls.toArray(new URL[0]));
   3.271 +    }
   3.272 +
   3.273 +    private static void doAttribute(Context c, JCCompilationUnit cut) {
   3.274 +        JavaCompiler jc = JavaCompiler.instance(c);
   3.275 +        final Enter enter = Enter.instance(c);
   3.276 +        final Queue<Env<AttrContext>> queued = new LinkedList<Env<AttrContext>>();
   3.277 +
   3.278 +        queued.add(enter.getTopLevelEnv(cut));
   3.279 +
   3.280 +        new TreeScanner<Void, Void>() {
   3.281 +            @Override
   3.282 +            public Void visitClass(ClassTree node, Void p) {
   3.283 +                Env<AttrContext> env = enter.getEnv(((JCClassDecl) node).sym);
   3.284 +
   3.285 +                if (env != null)
   3.286 +                    queued.add(env);
   3.287 +
   3.288 +                return super.visitClass(node, p);
   3.289 +            }
   3.290 +        }.scan(cut, null);
   3.291 +
   3.292 +        Attr attr = Attr.instance(c);
   3.293 +
   3.294 +        for (Env<AttrContext> env : queued) {
   3.295 +            attr.attribClass(env.tree.pos(), env.enclClass.sym);
   3.296 +        }
   3.297 +    }
   3.298 +
   3.299 +
   3.300 +
   3.301 +    static {
   3.302 +        try {
   3.303 +            ClassLoader l = HintsAnnotationProcessingImpl.class.getClassLoader();
   3.304 +
   3.305 +            if (l == null) {
   3.306 +                l = ClassLoader.getSystemClassLoader();
   3.307 +            }
   3.308 +
   3.309 +            l.setClassAssertionStatus("org.netbeans.api.java.source.CompilationInfo", false);
   3.310 +        } catch (Throwable t) {
   3.311 +            t.printStackTrace();
   3.312 +        }
   3.313 +    }
   3.314 +
   3.315 +    private static final class ThoroughTreeCleaner extends TreeScanner<Void, Void> {
   3.316 +
   3.317 +        private final CompilationUnitTree cut;
   3.318 +        private final SourcePositions positions;
   3.319 +
   3.320 +        public ThoroughTreeCleaner(CompilationUnitTree cut, SourcePositions positions) {
   3.321 +            this.cut = cut;
   3.322 +            this.positions = positions;
   3.323 +        }
   3.324 +
   3.325 +        @Override
   3.326 +        public Void scan(Tree node, Void p) {
   3.327 +            if (node != null) ((JCTree) node).type = null;
   3.328 +            return super.scan(node, p);
   3.329 +        }
   3.330 +
   3.331 +        @Override
   3.332 +        public Void visitParameterizedType(ParameterizedTypeTree node, Void p) {
   3.333 +            return super.visitParameterizedType(node, p);
   3.334 +        }
   3.335 +
   3.336 +        @Override
   3.337 +        public Void visitMemberSelect(MemberSelectTree node, Void p) {
   3.338 +            ((JCFieldAccess) node).sym = null;
   3.339 +            return super.visitMemberSelect(node, p);
   3.340 +        }
   3.341 +
   3.342 +        @Override
   3.343 +        public Void visitIdentifier(IdentifierTree node, Void p) {
   3.344 +            ((JCIdent) node).sym = null;
   3.345 +            return super.visitIdentifier(node, p);
   3.346 +        }
   3.347 +
   3.348 +        @Override
   3.349 +        public Void visitMethod(MethodTree node, Void p) {
   3.350 +            JCMethodDecl method = (JCMethodDecl) node;
   3.351 +
   3.352 +            if (method.sym != null && method.sym.getKind() == ElementKind.CONSTRUCTOR && method.sym.owner.getKind() == ElementKind.ENUM) {
   3.353 +                if (positions.getEndPosition(cut, method.body.stats.head) == (-1)) {
   3.354 +                    method.body.stats = method.body.stats.tail;
   3.355 +                }
   3.356 +            }
   3.357 +
   3.358 +            return super.visitMethod(node, p);
   3.359 +        }
   3.360 +
   3.361 +        @Override
   3.362 +        public Void visitClass(ClassTree node, Void p) {
   3.363 +            JCClassDecl decl = (JCClassDecl) node;
   3.364 +
   3.365 +            if (decl.sym.getKind() == ElementKind.ENUM) {
   3.366 +                if (positions.getEndPosition(cut, decl.defs.head) == (-1)) {
   3.367 +                    decl.defs = decl.defs.tail;
   3.368 +                }
   3.369 +            }
   3.370 +            return super.visitClass(node, p);
   3.371 +        }
   3.372 +
   3.373 +    }
   3.374 +
   3.375 +    @ServiceProvider(service=MimeDataProvider.class)
   3.376 +    public static final class MimeDataProviderImpl implements MimeDataProvider {
   3.377 +
   3.378 +        private static final Lookup L = Lookups.fixed(new JavacParserFactory());
   3.379 +
   3.380 +        public Lookup getLookup(MimePath mimePath) {
   3.381 +            if ("text/x-java".equals(mimePath.getPath()))
   3.382 +                return L;
   3.383 +            return null;
   3.384 +        }
   3.385 +        
   3.386 +    }
   3.387 +
   3.388 +    public static final class StandaloneMimeDataProviderImpl implements MimeDataProvider {
   3.389 +
   3.390 +        private static final Lookup L = Lookups.fixed(NbPreferences.forModule(HintsAnnotationProcessing.class), new JavacParserFactory());
   3.391 +
   3.392 +        public Lookup getLookup(MimePath mimePath) {
   3.393 +            if ("text/x-java".equals(mimePath.getPath()))
   3.394 +                return L;
   3.395 +            return null;
   3.396 +        }
   3.397 +
   3.398 +    }
   3.399 +
   3.400 +    @ServiceProvider(service=SourceForBinaryQueryImplementation.class, position=0)
   3.401 +    public static final class EmptySourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation2 {
   3.402 +        public Result findSourceRoots2(URL binaryRoot) {
   3.403 +            return INSTANCE;
   3.404 +        }
   3.405 +        public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) {
   3.406 +            return findSourceRoots2(binaryRoot);
   3.407 +        }
   3.408 +        private static final Result INSTANCE = new Result() {
   3.409 +            public boolean preferSources() {
   3.410 +                return false;
   3.411 +            }
   3.412 +            public org.openide.filesystems.FileObject[] getRoots() {
   3.413 +                return new org.openide.filesystems.FileObject[0];
   3.414 +            }
   3.415 +            public void addChangeListener(ChangeListener l) {}
   3.416 +            public void removeChangeListener(ChangeListener l) {}
   3.417 +        };
   3.418 +    }
   3.419 +
   3.420 +    public static class PreferencesProvider implements Provider {
   3.421 +
   3.422 +        private final MemoryPreferencesFactory f;
   3.423 +
   3.424 +        public PreferencesProvider() {
   3.425 +            this.f = new MemoryPreferencesFactory();
   3.426 +        }
   3.427 +
   3.428 +        @Override
   3.429 +        public Preferences preferencesForModule(Class cls) {
   3.430 +            return f.userRoot().node(cls.getPackage().getName());
   3.431 +        }
   3.432 +
   3.433 +        @Override
   3.434 +        public Preferences preferencesRoot() {
   3.435 +            return f.userRoot();
   3.436 +        }
   3.437 +
   3.438 +    }
   3.439 +    //copied from NB junit:
   3.440 +    public static class MemoryPreferencesFactory implements PreferencesFactory {
   3.441 +        /** Creates a new instance  */
   3.442 +        public MemoryPreferencesFactory() {}
   3.443 +
   3.444 +        public Preferences userRoot() {
   3.445 +            return NbPreferences.userRootImpl();
   3.446 +        }
   3.447 +
   3.448 +        public Preferences systemRoot() {
   3.449 +            return NbPreferences.systemRootImpl();
   3.450 +        }
   3.451 +
   3.452 +        private static class NbPreferences extends AbstractPreferences {
   3.453 +            private static Preferences USER_ROOT;
   3.454 +            private static Preferences SYSTEM_ROOT;
   3.455 +
   3.456 +            /*private*/Properties properties;
   3.457 +
   3.458 +            static Preferences userRootImpl() {
   3.459 +                if (USER_ROOT == null) {
   3.460 +                    USER_ROOT = new NbPreferences();
   3.461 +                }
   3.462 +                return USER_ROOT;
   3.463 +            }
   3.464 +
   3.465 +            static Preferences systemRootImpl() {
   3.466 +                if (SYSTEM_ROOT == null) {
   3.467 +                    SYSTEM_ROOT = new NbPreferences();
   3.468 +                }
   3.469 +                return SYSTEM_ROOT;
   3.470 +            }
   3.471 +
   3.472 +
   3.473 +            private NbPreferences() {
   3.474 +                super(null, "");
   3.475 +            }
   3.476 +
   3.477 +            /** Creates a new instance of PreferencesImpl */
   3.478 +            private  NbPreferences(NbPreferences parent, String name)  {
   3.479 +                super(parent, name);
   3.480 +                newNode = true;
   3.481 +            }
   3.482 +
   3.483 +            protected final String getSpi(String key) {
   3.484 +                return properties().getProperty(key);
   3.485 +            }
   3.486 +
   3.487 +            protected final String[] childrenNamesSpi() throws BackingStoreException {
   3.488 +                return new String[0];
   3.489 +            }
   3.490 +
   3.491 +            protected final String[] keysSpi() throws BackingStoreException {
   3.492 +                return properties().keySet().toArray(new String[0]);
   3.493 +            }
   3.494 +
   3.495 +            protected final void putSpi(String key, String value) {
   3.496 +                properties().put(key,value);
   3.497 +            }
   3.498 +
   3.499 +            protected final void removeSpi(String key) {
   3.500 +                properties().remove(key);
   3.501 +            }
   3.502 +
   3.503 +            protected final void removeNodeSpi() throws BackingStoreException {}
   3.504 +            protected  void flushSpi() throws BackingStoreException {}
   3.505 +            protected void syncSpi() throws BackingStoreException {
   3.506 +                properties().clear();
   3.507 +            }
   3.508 +
   3.509 +            @Override
   3.510 +            public void put(String key, String value) {
   3.511 +                try {
   3.512 +                    super.put(key, value);
   3.513 +                } catch (IllegalArgumentException iae) {
   3.514 +                    if (iae.getMessage().contains("too long")) {
   3.515 +                        // Not for us!
   3.516 +                        putSpi(key, value);
   3.517 +                    } else {
   3.518 +                        throw iae;
   3.519 +                    }
   3.520 +                }
   3.521 +            }
   3.522 +
   3.523 +            Properties properties()  {
   3.524 +                if (properties == null) {
   3.525 +                    properties = new Properties();
   3.526 +                }
   3.527 +                return properties;
   3.528 +            }
   3.529 +
   3.530 +            protected AbstractPreferences childSpi(String name) {
   3.531 +                return new NbPreferences(this, name);
   3.532 +            }
   3.533 +        }
   3.534 +
   3.535 +    }
   3.536 +}
     4.1 --- a/cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/CreateStandaloneCompilerJar.java	Sun Jan 16 19:22:56 2011 +0100
     4.2 +++ b/cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/CreateStandaloneCompilerJar.java	Sun Jan 16 19:22:56 2011 +0100
     4.3 @@ -54,9 +54,10 @@
     4.4  
     4.5      @Override
     4.6      protected Info computeInfo() {
     4.7 -        return new Info().addAdditionalRoots(UtilitiesSPIImpl.class.getName(), "com.sun.tools.javac.Main", HintsAnnotationProcessing.class.getName())
     4.8 +        return new Info().addAdditionalRoots(UtilitiesSPIImpl.class.getName(), "com.sun.tools.javac.Main", HintsAnnotationProcessing.class.getName(), HintsAnnotationProcessingImpl.class.getName())
     4.9                           .addMetaInfRegistrations(new MetaInfRegistration(Utilities.SPI.class, UtilitiesSPIImpl.class))
    4.10 -                         .addMetaInfRegistrations(new MetaInfRegistration(Processor.class, HintsAnnotationProcessing.class));
    4.11 +                         .addMetaInfRegistrations(new MetaInfRegistration(Processor.class, HintsAnnotationProcessingImpl.class))
    4.12 +                         .addMetaInfRegistrationToCopy(AbstractHintsAnnotationProcessing.class.getName());
    4.13      }
    4.14  
    4.15  }
     5.1 --- a/cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessingTest.java	Sun Jan 16 19:22:56 2011 +0100
     5.2 +++ b/cmdline/compiler/test/unit/src/org/netbeans/modules/jackpot30/compiler/HintsAnnotationProcessingTest.java	Sun Jan 16 19:22:56 2011 +0100
     5.3 @@ -82,7 +82,10 @@
     5.4          doRunCompiler(golden, "src/test/Test.java",
     5.5                                "package test; public class Test {private void test(java.io.File f) {f.isDirectory();}}\n",
     5.6                                "src/META-INF/upgrade/joFile.hint",
     5.7 -                              "'test':\n$1.isDirectory() :: $1 instanceof java.io.File => !$1.isFile();;");
     5.8 +                              "'test':\n$1.isDirectory() :: $1 instanceof java.io.File => !$1.isFile();;",
     5.9 +                              null,
    5.10 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_ENABLE + "=true",
    5.11 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_FIXES_ENABLE + "=true");
    5.12      }
    5.13  
    5.14      public void testRunCompiler2() throws Exception {
    5.15 @@ -96,7 +99,10 @@
    5.16          doRunCompiler(golden, "src/test/Test.java",
    5.17                                "package test; public class Test {private void test() {Character.toLowerCase('a');}}\n",
    5.18                                "src/META-INF/upgrade/joFile.hint",
    5.19 -                              "'test':\njava.lang.Character.toLowerCase($1) :: $1 instanceof char => java.lang.Character.toUpperCase($1) ;;");
    5.20 +                              "'test':\njava.lang.Character.toLowerCase($1) :: $1 instanceof char => java.lang.Character.toUpperCase($1) ;;",
    5.21 +                              null,
    5.22 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_ENABLE + "=true",
    5.23 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_FIXES_ENABLE + "=true");
    5.24      }
    5.25  
    5.26      public void testRunCompilerMulti() throws Exception {
    5.27 @@ -117,7 +123,10 @@
    5.28                                "src/test/Dep.java",
    5.29                                "package test; public class Dep {static void test() {Character.toLowerCase('a');}}\n",
    5.30                                "src/META-INF/upgrade/joFile.hint",
    5.31 -                              "'test':\njava.lang.Character.toLowerCase($1) :: $1 instanceof char => java.lang.Character.toUpperCase($1) ;;");
    5.32 +                              "'test':\njava.lang.Character.toLowerCase($1) :: $1 instanceof char => java.lang.Character.toUpperCase($1) ;;",
    5.33 +                              null,
    5.34 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_ENABLE + "=true",
    5.35 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_FIXES_ENABLE + "=true");
    5.36      }
    5.37  
    5.38  //    public void testNPEFromAttribute() throws Exception {//TODO: does not reproduce the problem - likely caused by null Env<AttrContext> for annonymous innerclasses
    5.39 @@ -217,7 +226,9 @@
    5.40                                "'test':\njava.lang.Character.toLowerCase($1) :: $1 instanceof char => java.lang.Character.toUpperCase($1) ;;",
    5.41                                null,
    5.42                                "-classpath",
    5.43 -                              "comp");
    5.44 +                              "comp",
    5.45 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_ENABLE + "=true",
    5.46 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_FIXES_ENABLE + "=true");
    5.47      }
    5.48  
    5.49      public void testNoDebugInfo() throws Exception {
    5.50 @@ -233,7 +244,9 @@
    5.51                                "src/META-INF/upgrade/joFile.hint",
    5.52                                "'test':\n$1.isDirectory() :: $1 instanceof java.io.File => !$1.isFile();;",
    5.53                                null,
    5.54 -                              "-g:none");
    5.55 +                              "-g:none",
    5.56 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_ENABLE + "=true",
    5.57 +                              "-A" + HintsAnnotationProcessing.CLASSPATH_HINTS_FIXES_ENABLE + "=true");
    5.58      }
    5.59  
    5.60      private void doRunCompiler(String goldenDiff, String... fileContentAndExtraOptions) throws Exception {