c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java
branchrelease82
changeset 18423 b9d9af239a0c
child 18417 853976f2c616
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/c.s.tools.ide.analysis.modernize/src/com/sun/tools/ide/analysis/modernize/impl/ModernizeAnalyzerImpl.java	Wed Jun 07 20:23:29 2017 +0300
     1.3 @@ -0,0 +1,214 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
     1.8 + *
     1.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
    1.10 + * Other names may be trademarks of their respective owners.
    1.11 + *
    1.12 + * The contents of this file are subject to the terms of either the GNU
    1.13 + * General Public License Version 2 only ("GPL") or the Common
    1.14 + * Development and Distribution License("CDDL") (collectively, the
    1.15 + * "License"). You may not use this file except in compliance with the
    1.16 + * License. You can obtain a copy of the License at
    1.17 + * http://www.netbeans.org/cddl-gplv2.html
    1.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.19 + * specific language governing permissions and limitations under the
    1.20 + * License.  When distributing the software, include this License Header
    1.21 + * Notice in each file and include the License file at
    1.22 + * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
    1.23 + * particular file as subject to the "Classpath" exception as provided
    1.24 + * by Oracle in the GPL Version 2 section of the License file that
    1.25 + * accompanied this code. If applicable, add the following below the
    1.26 + * License Header, with the fields enclosed by brackets [] replaced by
    1.27 + * your own identifying information:
    1.28 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.29 + *
    1.30 + * If you wish your version of this file to be governed by only the CDDL
    1.31 + * or only the GPL Version 2, indicate your decision by adding
    1.32 + * "[Contributor] elects to include this software in this distribution
    1.33 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
    1.34 + * single choice of license, a recipient has the option to distribute
    1.35 + * your version of this file under either the CDDL, the GPL Version 2 or
    1.36 + * to extend the choice of license to its licensees as provided above.
    1.37 + * However, if you add GPL Version 2 code and therefore, elected the GPL
    1.38 + * Version 2 license, then the option applies only if the new code is
    1.39 + * made subject to such option by the copyright holder.
    1.40 + *
    1.41 + * Contributor(s): Ilia Gromov
    1.42 + */
    1.43 +package com.sun.tools.ide.analysis.modernize.impl;
    1.44 +
    1.45 +import com.sun.tools.ide.analysis.modernize.options.ClangAnalyzerOptions;
    1.46 +import com.sun.tools.ide.analysis.modernize.resources.BundleUtilities;
    1.47 +import java.io.CharConversionException;
    1.48 +import java.io.File;
    1.49 +import java.util.ArrayList;
    1.50 +import java.util.Arrays;
    1.51 +import java.util.Collection;
    1.52 +import java.util.Collections;
    1.53 +import java.util.List;
    1.54 +import java.util.concurrent.atomic.AtomicBoolean;
    1.55 +import java.util.prefs.Preferences;
    1.56 +import org.netbeans.modules.analysis.spi.Analyzer;
    1.57 +import org.netbeans.modules.cnd.analysis.api.AbstractAnalyzer;
    1.58 +import org.netbeans.modules.cnd.analysis.api.AbstractHintsPanel;
    1.59 +import org.netbeans.modules.cnd.api.model.CsmFile;
    1.60 +import org.netbeans.modules.cnd.api.model.syntaxerr.CodeAudit;
    1.61 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorInfo;
    1.62 +import org.netbeans.modules.cnd.api.model.syntaxerr.CsmErrorProvider;
    1.63 +import org.netbeans.modules.cnd.modelutil.CsmUtilities;
    1.64 +import org.netbeans.modules.cnd.utils.CndPathUtilities;
    1.65 +import org.netbeans.spi.editor.hints.ErrorDescription;
    1.66 +import org.netbeans.spi.editor.hints.ErrorDescriptionFactory;
    1.67 +import org.netbeans.spi.editor.hints.LazyFixList;
    1.68 +import org.netbeans.spi.editor.hints.Severity;
    1.69 +import org.openide.filesystems.FileObject;
    1.70 +import org.openide.util.ImageUtilities;
    1.71 +import org.openide.util.NbBundle;
    1.72 +import org.openide.util.lookup.ServiceProvider;
    1.73 +import org.openide.xml.XMLUtil;
    1.74 +
    1.75 +public class ModernizeAnalyzerImpl extends AbstractAnalyzer {
    1.76 +
    1.77 +    private static final String PREFIX = "tidy-"; //NOI18N
    1.78 +
    1.79 +    private ModernizeAnalyzerImpl(Context ctx) {
    1.80 +        super(ctx);
    1.81 +    }
    1.82 +
    1.83 +    @Override
    1.84 +    public ModernizeErrorProvider getErrorProvider(Preferences preferences) {
    1.85 +        return ModernizeErrorProvider.getInstance();
    1.86 +    }
    1.87 +
    1.88 +    @Override 
    1.89 +    protected boolean isCompileUnitBased() {
    1.90 +        return true;
    1.91 +    }
    1.92 +
    1.93 +    @Override
    1.94 +    protected Collection<? extends ErrorDescription> doRunImpl(final FileObject sr, final Context ctx, final CsmErrorProvider provider, final AtomicBoolean cancel) {
    1.95 +        final CsmFile csmFile = CsmUtilities.getCsmFile(sr, false, false);
    1.96 +        if (csmFile == null) {
    1.97 +            return Collections.<ErrorDescription>emptyList();
    1.98 +        }
    1.99 +        CsmErrorProvider.Request request = new RequestImpl(csmFile, ctx, cancel);
   1.100 +        final ArrayList<ErrorDescription> res = new ArrayList<ErrorDescription>();
   1.101 +        CsmErrorProvider.Response response = new ResponseImpl(sr, res, cancel);
   1.102 +        provider.getErrors(request, response);
   1.103 +        return res;
   1.104 +    }
   1.105 +
   1.106 +    protected static class ResponseImpl extends AbstractResponse {
   1.107 +
   1.108 +        public ResponseImpl(FileObject sr, ArrayList<ErrorDescription> res, AtomicBoolean cancel) {
   1.109 +            super(sr, res, cancel);
   1.110 +        }
   1.111 +
   1.112 +        @Override
   1.113 +        protected ErrorDescription addErrorImpl(CsmErrorInfo errorInfo, FileObject fo) {
   1.114 +            String messages[] = errorInfo.getMessage().split("\n"); //NOI18N
   1.115 +            if (messages.length > 0) {
   1.116 +                StringBuilder sb = new StringBuilder();
   1.117 +                if (errorInfo instanceof ModernizeErrorInfo) {
   1.118 +                    ModernizeErrorInfo mei = (ModernizeErrorInfo) errorInfo;
   1.119 +                    sb.append("<pre>"); //NOI18N
   1.120 +                    sb.append("["); //NOI18N
   1.121 +                    sb.append(mei.getId());
   1.122 +                    sb.append("]"); //NOI18N
   1.123 +                    sb.append("\n"); //NOI18N
   1.124 +                    List<YamlParser.Replacement> replacements = mei.getDiagnostics().getReplacements();
   1.125 +                    if (replacements.isEmpty()) {
   1.126 +                        sb.append(CndPathUtilities.getRelativePath(mei.getProject().getProjectRoot(), mei.getDiagnostics().getMessageFilePath())).append(" "); //NOI18N
   1.127 +                        sb.append(mei.getDiagnostics().getMessageFileOffset()).append("\n");
   1.128 +                    } else {
   1.129 +                        for (YamlParser.Replacement replacement : replacements) {
   1.130 +                            sb.append("\n"); //NOI18N
   1.131 +                            sb.append("Replacement: "); //NOI18N
   1.132 +                            sb.append(CndPathUtilities.getRelativePath(mei.getProject().getProjectRoot(), replacement.filePath)).append(" "); //NOI18N
   1.133 +                            sb.append(replacement.offset).append("-").append(replacement.offset + replacement.length).append("\n"); //NOI18N
   1.134 +                            String attributeValue = replacement.replacementText;
   1.135 +                            try {
   1.136 +                                attributeValue = XMLUtil.toAttributeValue(replacement.replacementText);
   1.137 +                            } catch (CharConversionException ex) {
   1.138 +                            }
   1.139 +                            sb.append(attributeValue).append("\n"); //NOI18N
   1.140 +                        }
   1.141 +                    }
   1.142 +                    sb.append(mei.getMessage());
   1.143 +                    sb.append("</pre>"); //NOI18N
   1.144 +
   1.145 +                    LazyFixList list = new LazyFixListImpl();
   1.146 +
   1.147 +                    return ErrorDescriptionFactory.createErrorDescription(PREFIX + mei.getId(), Severity.valueOf(mei.getSeverity().toString().toUpperCase()),
   1.148 +                            messages[0], sb.toString(), list, fo, errorInfo.getStartOffset(), errorInfo.getStartOffset());
   1.149 +                }
   1.150 +            }
   1.151 +            return null;
   1.152 +        }
   1.153 +    }
   1.154 +
   1.155 +    @ServiceProvider(service = AnalyzerFactory.class)
   1.156 +    public static final class AnalyzerFactoryImpl extends AnalyzerFactory {
   1.157 +
   1.158 +        public AnalyzerFactoryImpl() {
   1.159 +            super(ModernizeErrorProvider.NAME,
   1.160 +                    NbBundle.getMessage(ModernizeErrorProvider.class, "Modernize_NAME"), //NOI18N
   1.161 +                    ImageUtilities.loadImage("com/sun/tools/ide/analysis/modernize/impl/bugs.png")); //NOI18N
   1.162 +        }
   1.163 +
   1.164 +        @Override
   1.165 +        public Iterable<? extends WarningDescription> getWarnings() {
   1.166 +            List<WarningDescription> result = new ArrayList<WarningDescription>();
   1.167 +            final ModernizeErrorProvider provider = ModernizeErrorProvider.getInstance();
   1.168 +            for (CodeAudit audit : provider.getAudits()) {
   1.169 +                result.add(WarningDescription.create(PREFIX + audit.getID(), audit.getName(), ModernizeErrorProvider.NAME, provider.getDisplayName()));
   1.170 +            }
   1.171 +            String[] fatals = BundleUtilities.getFatalErrors();
   1.172 +            for (String id : fatals) { //NOI18N
   1.173 +                if (!id.isEmpty()) {
   1.174 +                    result.add(WarningDescription.create(PREFIX + id, BundleUtilities.getDescription(id), ModernizeErrorProvider.NAME, provider.getDisplayName()));
   1.175 +                }
   1.176 +            }
   1.177 +            return result;
   1.178 +        }
   1.179 +
   1.180 +        @Override
   1.181 +        public CustomizerProvider<Void, AbstractHintsPanel> getCustomizerProvider() {
   1.182 +            return new CustomizerProvider<Void, AbstractHintsPanel>() {
   1.183 +
   1.184 +                @Override
   1.185 +                public Void initialize() {
   1.186 +                    return null;
   1.187 +                }
   1.188 +
   1.189 +                @Override
   1.190 +                public AbstractHintsPanel createComponent(CustomizerContext<Void, AbstractHintsPanel> context) {
   1.191 +                    return AbstractAnalyzer.createComponent(ModernizeErrorProvider.getInstance());
   1.192 +                }
   1.193 +            };
   1.194 +        }
   1.195 +
   1.196 +        @Override
   1.197 +        public Collection<? extends MissingPlugin> requiredPlugins(Context context) {
   1.198 +//            ExecutionEnvironment env = detectEnvironment(context);
   1.199 +//            if (env == null) {
   1.200 +//                env = ExecutionEnvironmentFactory.getLocal();
   1.201 +//            }
   1.202 +            String installedTool = ClangAnalyzerOptions.getClangAnalyzerPath();
   1.203 +            if (installedTool == null || !new File(installedTool).exists()) {
   1.204 +                String codeBase = ClangAnalyzerOptions.getMissingModuleName();
   1.205 +                if (codeBase != null) {
   1.206 +                    return Arrays.asList(new MissingPlugin(codeBase, NbBundle.getMessage(ModernizeAnalyzerImpl.class, "Modernize_NAME"))); //NOI18N
   1.207 +                }
   1.208 +            }
   1.209 +            return Collections.emptyList();
   1.210 +        }
   1.211 +
   1.212 +        @Override
   1.213 +        public Analyzer createAnalyzer(Context context) {
   1.214 +            return new ModernizeAnalyzerImpl(context);
   1.215 +        }
   1.216 +    }
   1.217 +}