/**
  * @see
  *     org.eclipse.jdt.internal.core.builder.AbstractImageBuilder#writeClassFileContents(org.eclipse.jdt.internal.compiler.ClassFile,
  *     org.eclipse.core.resources.IFile, java.lang.String, boolean,
  *     org.eclipse.jdt.internal.core.builder.SourceFile)
  */
 protected void writeClassFileContents(
     ClassFile classfile,
     IFile file,
     String qualifiedFileName,
     boolean isTopLevelType,
     SourceFile compilationUnit)
     throws CoreException {
   // Before writing out the class file, compare it to the previous file
   // If structural changes occurred then add dependent source files
   byte[] bytes = classfile.getBytes();
   if (file.exists()) {
     if (writeClassFileCheck(file, qualifiedFileName, bytes)
         || compilationUnit.updateClassFile) { // see 46093
       if (JavaBuilder.DEBUG)
         System.out.println("Writing changed class file " + file.getName()); // $NON-NLS-1$
       if (!file.isDerived()) file.setDerived(true, null);
       file.setContents(new ByteArrayInputStream(bytes), true, false, null);
     } else if (JavaBuilder.DEBUG) {
       System.out.println("Skipped over unchanged class file " + file.getName()); // $NON-NLS-1$
     }
   } else {
     if (isTopLevelType) addDependentsOf(new Path(qualifiedFileName), true); // new type
     if (JavaBuilder.DEBUG)
       System.out.println("Writing new class file " + file.getName()); // $NON-NLS-1$
     try {
       file.create(new ByteArrayInputStream(bytes), IResource.FORCE | IResource.DERIVED, null);
     } catch (CoreException e) {
       if (e.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) {
         IStatus status = e.getStatus();
         if (status instanceof IResourceStatus) {
           IPath oldFilePath = ((IResourceStatus) status).getPath();
           char[] oldTypeName = oldFilePath.removeFileExtension().lastSegment().toCharArray();
           char[][] previousTypeNames =
               this.newState.getDefinedTypeNamesFor(compilationUnit.typeLocator());
           boolean fromSameFile = false;
           if (previousTypeNames == null) {
             fromSameFile = CharOperation.equals(compilationUnit.getMainTypeName(), oldTypeName);
           } else {
             for (int i = 0, l = previousTypeNames.length; i < l; i++) {
               if (CharOperation.equals(previousTypeNames[i], oldTypeName)) {
                 fromSameFile = true;
                 break;
               }
             }
           }
           if (fromSameFile) {
             // file is defined by the same compilationUnit, but won't be deleted until later so do
             // it now
             IFile collision = file.getParent().getFile(new Path(oldFilePath.lastSegment()));
             collision.delete(true, false, null);
             boolean success = false;
             try {
               file.create(
                   new ByteArrayInputStream(bytes), IResource.FORCE | IResource.DERIVED, null);
               success = true;
             } catch (CoreException ignored) {
               // ignore the second exception
             }
             if (success) return;
           }
         }
         // catch the case that a type has been renamed and collides on disk with an
         // as-yet-to-be-deleted type
         throw new AbortCompilation(true, new AbortIncrementalBuildException(qualifiedFileName));
       }
       throw e; // rethrow
     }
   }
 }
Beispiel #2
0
  /**
   * 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() == Language.JAVA) {
        languageVersion = PMDPlugin.javaVersionFor(file.getProject());
      }
      if (languageVersion != null) {
        configuration().setDefaultLanguageVersion(languageVersion);
      }
      log.debug("discovered language: " + languageVersion);

      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);

        RuleSets rSets = new RuleSets(getRuleSet());
        new SourceCodeProcessor(configuration()).processSourceCode(input, rSets, context);

        timer.stop();
        pmdDuration += timer.getDuration();

        updateMarkers(file, context, isUseTaskMarker());

        worked(1);
        fileCount++;
      } else {
        log.debug("The file " + file.getName() + " is not in the working set");
      }

    } catch (CoreException e) {
      log.error("Core exception visiting " + file.getName(), e); // TODO:		// complete message
    } catch (PMDException e) {
      log.error("PMD exception visiting " + file.getName(), e); // TODO: 		// complete message
    } catch (IOException e) {
      log.error("IO exception visiting " + file.getName(), e); // TODO: 		// complete message
    } catch (PropertiesException e) {
      log.error("Properties exception visiting " + file.getName(), e); // TODO:	// complete message
    } finally {
      IOUtil.closeQuietly(input);
    }
  }
 void checkFileIsNotDerived(IContainer container, String fName) {
   IFile file = container.getFile(new Path(fName));
   assertTrue(file + " should exist", file.exists());
   assertFalse(file + " should not be derived", file.isDerived());
 }
Beispiel #4
0
 private boolean isIncluded(IFile file) throws PropertiesException {
   return projectProperties.isIncludeDerivedFiles()
       || !projectProperties.isIncludeDerivedFiles() && !file.isDerived();
 }