Search in sources :

Example 6 with PropertiesException

use of net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException in project pmd-eclipse-plugin by pmd.

the class BuildProjectCommand method execute.

/**
 * @see name.herlin.command.AbstractProcessableCommand#execute()
 */
public void execute() throws CommandException {
    try {
        project().build(IncrementalProjectBuilder.FULL_BUILD, this.getMonitor());
        projectProperties().setNeedRebuild(false);
    } catch (CoreException e) {
        throw new CommandException(e);
    } catch (PropertiesException e) {
        throw new CommandException(e);
    } finally {
        this.setTerminated(true);
    }
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CommandException(name.herlin.command.CommandException)

Example 7 with PropertiesException

use of net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException in project pmd-eclipse-plugin by pmd.

the class ReviewCodeCmd method processResource.

/**
 * Review a single resource
 */
private void processResource(IResource resource) throws CommandException {
    try {
        final IProject project = resource.getProject();
        final IProjectProperties properties = getProjectProperties(project);
        if (!runAlways && !properties.isPmdEnabled()) {
            return;
        }
        // properties.getProjectRuleSet();
        final RuleSet ruleSet = rulesetFrom(resource);
        // final PMDEngine pmdEngine = getPmdEngineForProject(project);
        int targetCount = 0;
        if (resource.exists()) {
            targetCount = countResourceElement(resource);
        }
        // Could add a property that lets us set the max number to analyze
        if (properties.isFullBuildEnabled() || isUserInitiated() || targetCount <= MAXIMUM_RESOURCE_COUNT) {
            setStepCount(targetCount);
            LOG.debug("Visiting resource " + resource.getName() + " : " + getStepCount());
            if (resource.exists()) {
                final ResourceVisitor visitor = new ResourceVisitor();
                visitor.setMonitor(getMonitor());
                visitor.setRuleSet(ruleSet);
                // visitor.setPmdEngine(pmdEngine);
                visitor.setAccumulator(markersByFile);
                visitor.setUseTaskMarker(taskMarker);
                visitor.setProjectProperties(properties);
                resource.accept(visitor);
                ruleCount = ruleSet.getRules().size();
                fileCount += visitor.getProcessedFilesCount();
                pmdDuration += visitor.getActualPmdDuration();
            } else {
                LOG.debug("Skipping resource " + resource.getName() + " because it doesn't exist.");
            }
        } else {
            String message = "Skipping resource " + resource.getName() + " because of fullBuildEnabled flag and " + "targetCount is " + targetCount + ". This is more than " + MAXIMUM_RESOURCE_COUNT + "." + " If you want to execute PMD, please check \"Full build enabled\" in the project settings";
            PMDPlugin.getDefault().logInformation(message);
        }
        // TODO - temp fix? BR
        worked(1);
    } catch (PropertiesException e) {
        throw new CommandException(e);
    } catch (CoreException e) {
        throw new CommandException(e);
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CoreException(org.eclipse.core.runtime.CoreException) CommandException(name.herlin.command.CommandException) IResourceVisitor(org.eclipse.core.resources.IResourceVisitor) IProject(org.eclipse.core.resources.IProject)

Example 8 with PropertiesException

use of net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException in project pmd-eclipse-plugin by pmd.

the class ReviewCodeCmd method processResourceDelta.

/**
 * Review a resource delta
 */
private void processResourceDelta() throws CommandException {
    try {
        IResource resource = resourceDelta.getResource();
        final IProject project = resource.getProject();
        final IProjectProperties properties = getProjectProperties(project);
        // properties.getProjectRuleSet();
        RuleSet ruleSet = rulesetFromResourceDelta();
        // PMDEngine pmdEngine = getPmdEngineForProject(project);
        int targetCount = countDeltaElement(resourceDelta);
        // Could add a property that lets us set the max number to analyze
        if (properties.isFullBuildEnabled() || isUserInitiated() || targetCount <= MAXIMUM_RESOURCE_COUNT) {
            setStepCount(targetCount);
            LOG.debug("Visiting delta of resource " + resource.getName() + " : " + getStepCount());
            DeltaVisitor visitor = new DeltaVisitor();
            visitor.setMonitor(getMonitor());
            visitor.setRuleSet(ruleSet);
            // visitor.setPmdEngine(pmdEngine);
            visitor.setAccumulator(markersByFile);
            visitor.setUseTaskMarker(taskMarker);
            visitor.setProjectProperties(properties);
            resourceDelta.accept(visitor);
            ruleCount = ruleSet.getRules().size();
            fileCount += visitor.getProcessedFilesCount();
            pmdDuration += visitor.getActualPmdDuration();
        } else {
            String message = "Skipping resourceDelta " + resource.getName() + " because of fullBuildEnabled flag and " + "targetCount is " + targetCount + ". This is more than " + MAXIMUM_RESOURCE_COUNT + "." + " If you want to execute PMD, please check \"Full build enabled\" in the project settings";
            PMDPlugin.getDefault().logInformation(message);
            LOG.debug(message);
        }
    } catch (PropertiesException e) {
        throw new CommandException(e);
    } catch (CoreException e) {
        throw new CommandException(e);
    }
}
Also used : RuleSet(net.sourceforge.pmd.RuleSet) IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IResourceDeltaVisitor(org.eclipse.core.resources.IResourceDeltaVisitor) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) CoreException(org.eclipse.core.runtime.CoreException) CommandException(name.herlin.command.CommandException) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 9 with PropertiesException

use of net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException in project pmd-eclipse-plugin by pmd.

the class BaseVisitor method reviewResource.

/**
 * Run PMD against a resource
 *
 * @param resource
 *            the resource to process
 */
protected final void reviewResource(IResource resource) {
    IFile file = (IFile) resource.getAdapter(IFile.class);
    if (file == null || file.getFileExtension() == null) {
        return;
    }
    Reader input = null;
    try {
        boolean included = isIncluded(file);
        LOG.debug("Derived files included: " + projectProperties.isIncludeDerivedFiles());
        LOG.debug("file " + file.getName() + " is derived: " + file.isDerived());
        LOG.debug("file checked: " + included);
        prepareMarkerAccumulator(file);
        LanguageVersionDiscoverer languageDiscoverer = new LanguageVersionDiscoverer();
        LanguageVersion languageVersion = languageDiscoverer.getDefaultLanguageVersionForFile(file.getName());
        // in case it is java, select the correct java version
        if (languageVersion != null && languageVersion.getLanguage() == LanguageRegistry.getLanguage(JavaLanguageModule.NAME)) {
            languageVersion = PMDPlugin.javaVersionFor(file.getProject());
        }
        if (languageVersion != null) {
            configuration().setDefaultLanguageVersion(languageVersion);
        }
        LOG.debug("discovered language: " + languageVersion);
        PMDPlugin.setJavaClassLoader(configuration(), resource.getProject());
        final File sourceCodeFile = file.getRawLocation().toFile();
        if (included && getRuleSet().applies(sourceCodeFile) && isFileInWorkingSet(file) && languageVersion != null) {
            subTask("PMD checking: " + file.getName());
            Timer timer = new Timer();
            RuleContext context = PMD.newRuleContext(file.getName(), sourceCodeFile);
            context.setLanguageVersion(languageVersion);
            input = new InputStreamReader(file.getContents(), file.getCharset());
            // getPmdEngine().processFile(input, getRuleSet(), context);
            // getPmdEngine().processFile(sourceCodeFile, getRuleSet(),
            // context);
            DataSource dataSource = new ReaderDataSource(input, file.getName());
            RuleSetFactory ruleSetFactory = new RuleSetFactory() {

                @Override
                public synchronized RuleSets createRuleSets(String referenceString) throws RuleSetNotFoundException {
                    return new RuleSets(getRuleSet());
                }
            };
            // need to disable multi threading, as the ruleset is
            // not recreated and shared between threads...
            // but as we anyway have only one file to process, it won't hurt
            // here.
            configuration().setThreads(0);
            LOG.debug("PMD running on file " + file.getName());
            final Report collectingReport = new Report();
            Renderer collectingRenderer = new AbstractRenderer("collectingRenderer", "Renderer that collect violations") {

                @Override
                public void startFileAnalysis(DataSource dataSource) {
                // TODO Auto-generated method stub
                }

                @Override
                public void start() throws IOException {
                // TODO Auto-generated method stub
                }

                @Override
                public void renderFileReport(Report report) throws IOException {
                    for (RuleViolation v : report) {
                        collectingReport.addRuleViolation(v);
                    }
                    for (Iterator<ProcessingError> it = report.errors(); it.hasNext(); ) {
                        collectingReport.addError(it.next());
                    }
                    for (Iterator<ConfigurationError> it = report.configErrors(); it.hasNext(); ) {
                        collectingReport.addConfigError(it.next());
                    }
                }

                @Override
                public void end() throws IOException {
                // TODO Auto-generated method stub
                }

                @Override
                public String defaultFileExtension() {
                    // TODO Auto-generated method stub
                    return null;
                }
            };
            // PMD.processFiles(configuration(), ruleSetFactory,
            // Arrays.asList(dataSource), context,
            // Arrays.asList(collectingRenderer));
            new MonoThreadProcessor(configuration()).processFiles(ruleSetFactory, Arrays.asList(dataSource), context, Arrays.asList(collectingRenderer));
            LOG.debug("PMD run finished.");
            timer.stop();
            pmdDuration += timer.getDuration();
            LOG.debug("PMD found " + collectingReport.size() + " violations for file " + file.getName());
            if (collectingReport.hasConfigErrors()) {
                StringBuilder message = new StringBuilder("There were configuration errors!\n");
                Iterator<ConfigurationError> errors = collectingReport.configErrors();
                while (errors.hasNext()) {
                    ConfigurationError error = errors.next();
                    message.append(error.rule().getName()).append(": ").append(error.issue()).append('\n');
                }
                PMDPlugin.getDefault().logWarn(message.toString());
                LOG.warn(message);
            }
            if (collectingReport.hasErrors()) {
                StringBuilder message = new StringBuilder("There were processing errors!\n");
                Iterator<ProcessingError> errors = collectingReport.errors();
                while (errors.hasNext()) {
                    ProcessingError error = errors.next();
                    message.append(error.getFile()).append(": ").append(error.getMsg()).append(' ').append(error.getDetail()).append("\n");
                }
                PMDPlugin.getDefault().logWarn(message.toString());
                throw new PMDException(message.toString());
            }
            updateMarkers(file, collectingReport.iterator(), isUseTaskMarker());
            worked(1);
            fileCount++;
        } else {
            LOG.debug("The file " + file.getName() + " is not in the working set");
        }
    } catch (CoreException e) {
        // TODO: complete message
        LOG.error("Core exception visiting " + file.getName(), e);
    } catch (PMDException e) {
        // TODO: complete message
        LOG.error("PMD exception visiting " + file.getName(), e);
    } catch (IOException e) {
        // TODO: complete message
        LOG.error("IO exception visiting " + file.getName(), e);
    } catch (PropertiesException e) {
        // TODO: complete message
        LOG.error("Properties exception visiting " + file.getName(), e);
    } catch (IllegalArgumentException e) {
        LOG.error("Illegal argument", e);
    } finally {
        IOUtil.closeQuietly(input);
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) RuleContext(net.sourceforge.pmd.RuleContext) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) RuleViolation(net.sourceforge.pmd.RuleViolation) LanguageVersionDiscoverer(net.sourceforge.pmd.lang.LanguageVersionDiscoverer) ProcessingError(net.sourceforge.pmd.Report.ProcessingError) RuleSetFactory(net.sourceforge.pmd.RuleSetFactory) RuleSets(net.sourceforge.pmd.RuleSets) LanguageVersion(net.sourceforge.pmd.lang.LanguageVersion) InputStreamReader(java.io.InputStreamReader) Report(net.sourceforge.pmd.Report) MonoThreadProcessor(net.sourceforge.pmd.processor.MonoThreadProcessor) IOException(java.io.IOException) DataSource(net.sourceforge.pmd.util.datasource.DataSource) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) ConfigurationError(net.sourceforge.pmd.Report.ConfigurationError) ReaderDataSource(net.sourceforge.pmd.util.datasource.ReaderDataSource) Timer(name.herlin.command.Timer) CoreException(org.eclipse.core.runtime.CoreException) AbstractRenderer(net.sourceforge.pmd.renderers.AbstractRenderer) Renderer(net.sourceforge.pmd.renderers.Renderer) AbstractRenderer(net.sourceforge.pmd.renderers.AbstractRenderer) PMDException(net.sourceforge.pmd.PMDException) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 10 with PropertiesException

use of net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException in project pmd-eclipse-plugin by pmd.

the class CPDVisitor method visit.

/**
 * @see org.eclipse.core.resources.IResourceVisitor#visit(IResource) Add
 *      java files into the CPD object
 */
public boolean visit(IResource resource) throws CoreException {
    LOG.debug("CPD Visiting " + resource.getName());
    if (resource instanceof IFile) {
        IFile file = (IFile) resource;
        File ioFile = file.getLocation().toFile();
        try {
            if (StringUtil.isNotEmpty(file.getFileExtension()) && language.getFileFilter().accept(ioFile, file.getName()) && isFileInWorkingSet(file) && (includeDerivedFiles || !file.isDerived())) {
                LOG.debug("Add file " + resource.getName());
                files.add(ioFile);
                return false;
            }
        } catch (PropertiesException e) {
            LOG.warn("ModelException when adding file " + resource.getName() + " to CPD. Continuing.", e);
        }
    }
    return true;
}
Also used : IFile(org.eclipse.core.resources.IFile) PropertiesException(net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

PropertiesException (net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)17 CoreException (org.eclipse.core.runtime.CoreException)10 IFile (org.eclipse.core.resources.IFile)9 File (java.io.File)6 IProjectProperties (net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties)6 CommandException (name.herlin.command.CommandException)5 RuleSet (net.sourceforge.pmd.RuleSet)3 IProject (org.eclipse.core.resources.IProject)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 IOException (java.io.IOException)2 BufferedReader (java.io.BufferedReader)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 Timer (name.herlin.command.Timer)1 PMDException (net.sourceforge.pmd.PMDException)1 Report (net.sourceforge.pmd.Report)1 ConfigurationError (net.sourceforge.pmd.Report.ConfigurationError)1 ProcessingError (net.sourceforge.pmd.Report.ProcessingError)1 RuleContext (net.sourceforge.pmd.RuleContext)1