Search in sources :

Example 6 with IProjectProperties

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

the class DetectCutAndPasteCmd method findCandidateFiles.

/**
 * Finds all files in a project based on a language. Uses internally the
 * CPDVisitor.
 *
 * @return List of files
 * @throws PropertiesException
 * @throws CoreException
 */
private List<File> findCandidateFiles() throws PropertiesException, CoreException {
    final IProjectProperties properties = projectProperties();
    final CPDVisitor visitor = new CPDVisitor();
    visitor.setWorkingSet(properties.getProjectWorkingSet());
    visitor.setIncludeDerivedFiles(properties.isIncludeDerivedFiles());
    visitor.setLanguage(language);
    visitor.setFiles(new ArrayList<File>());
    visitProjectResourcesWith(visitor);
    return visitor.getFiles();
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 7 with IProjectProperties

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

the class ReviewCmdTest method setUp.

/**
 * @see junit.framework.TestCase#setUp()
 */
@Before
public void setUp() throws Exception {
    // 1. Create a Java project
    this.testProject = EclipseUtils.createJavaProject("PMDTestProject");
    Assert.assertTrue("A test project cannot be created; the tests cannot be performed.", this.testProject != null && this.testProject.exists() && this.testProject.isAccessible());
    // 2. Create a test source file inside that project
    final IFile testFile = EclipseUtils.createTestSourceFile(this.testProject);
    final InputStream is = EclipseUtils.getResourceStream(this.testProject, "/src/Test.java");
    Assert.assertNotNull("Cannot find the test source file", is);
    is.close();
    // 3. Enable PMD for the test project
    IProjectProperties properties = PMDPlugin.getDefault().getPropertiesManager().loadProjectProperties(testProject);
    properties.setPmdEnabled(true);
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IFile(org.eclipse.core.resources.IFile) InputStream(java.io.InputStream) Before(org.junit.Before)

Example 8 with IProjectProperties

use of net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties 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 9 with IProjectProperties

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

the class ReviewCodeCmd method rulesetFromResourceDelta.

private RuleSet rulesetFromResourceDelta() throws PropertiesException, CommandException {
    IResource resource = resourceDelta.getResource();
    final IProject project = resource.getProject();
    final IProjectProperties properties = getProjectProperties(project);
    // properties.getProjectRuleSet();
    return filteredRuleSet(properties);
}
Also used : IProjectProperties(net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties) IResource(org.eclipse.core.resources.IResource) IProject(org.eclipse.core.resources.IProject)

Example 10 with IProjectProperties

use of net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties 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)

Aggregations

IProjectProperties (net.sourceforge.pmd.eclipse.runtime.properties.IProjectProperties)14 PropertiesException (net.sourceforge.pmd.eclipse.runtime.properties.PropertiesException)6 IProject (org.eclipse.core.resources.IProject)6 RuleSet (net.sourceforge.pmd.RuleSet)5 IFile (org.eclipse.core.resources.IFile)4 CommandException (name.herlin.command.CommandException)3 CoreException (org.eclipse.core.runtime.CoreException)3 Test (org.junit.Test)3 InputStream (java.io.InputStream)2 Rule (net.sourceforge.pmd.Rule)2 IResource (org.eclipse.core.resources.IResource)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 Set (java.util.Set)1 RuleSetFactory (net.sourceforge.pmd.RuleSetFactory)1 IProjectPropertiesManager (net.sourceforge.pmd.eclipse.runtime.properties.IProjectPropertiesManager)1 IFolder (org.eclipse.core.resources.IFolder)1 IResourceDeltaVisitor (org.eclipse.core.resources.IResourceDeltaVisitor)1 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)1 Before (org.junit.Before)1