예제 #1
0
  @Test
  public void testVarargs() throws Throwable {
    int nrOfVarArgs = 0;
    int nrOfNoVarArgs = 0;

    Set<ASTFormalParameter> ops =
        getNodes(
            LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"),
            ASTFormalParameter.class,
            TEST1);
    for (Iterator<ASTFormalParameter> iter = ops.iterator(); iter.hasNext(); ) {
      ASTFormalParameter b = iter.next();
      ASTVariableDeclaratorId variableDeclId =
          b.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
      if (!"x".equals(variableDeclId.getImage())) {
        assertTrue(b.isVarargs());
        nrOfVarArgs++;
      } else {
        assertFalse(b.isVarargs());
        nrOfNoVarArgs++;
      }
    }

    // Ensure that both possibilities are tested
    assertEquals(1, nrOfVarArgs);
    assertEquals(1, nrOfNoVarArgs);
  }
  public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
      return super.visit(node, data);
    }

    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if (image.startsWith("java.lang.")) {
      image = image.substring(10);
    }

    boolean checkBoolean =
        ((RuleContext) data)
                .getLanguageVersion()
                .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"))
            >= 0;

    if (PREFIX_SET.contains(image) || (checkBoolean && "Boolean.valueOf".equals(image))) {
      ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
      if (parent.jjtGetNumChildren() >= 3) {
        Node n = parent.jjtGetChild(2);
        if (n instanceof ASTPrimarySuffix) {
          ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
          image = suffix.getImage();

          if (SUFFIX_SET.contains(image) || (checkBoolean && "booleanValue".equals(image))) {
            super.addViolation(data, node);
            return data;
          }
        }
      }
    }
    return super.visit(node, data);
  }
예제 #3
0
  /** Test on PLSQL file with default version */
  @Test
  public void testPlsql() {
    LanguageVersionDiscoverer discoverer = new LanguageVersionDiscoverer();
    File plsqlFile = new File("/path/to/MY_PACKAGE.sql");

    LanguageVersion languageVersion = discoverer.getDefaultLanguageVersionForFile(plsqlFile);
    assertEquals(
        "LanguageVersion must be PLSQL!",
        LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion(),
        languageVersion);
  }
예제 #4
0
 // FUTURE Delete this? I can't think of a good reason to keep it around.
 // Failure to determine the LanguageVersion for a file should be a hard
 // error, or simply cause the file to be skipped?
 public LanguageVersion getLanguageVersionOfFile(String fileName) {
   LanguageVersion languageVersion =
       languageVersionDiscoverer.getDefaultLanguageVersionForFile(fileName);
   if (languageVersion == null) {
     // For compatibility with older code that does not always pass in
     // a correct filename.
     languageVersion =
         languageVersionDiscoverer.getDefaultLanguageVersion(LanguageRegistry.getLanguage("Java"));
   }
   return languageVersion;
 }
예제 #5
0
 @Parameters
 public static Collection<Object[]> data() {
   return Arrays.asList(
       new Object[][] {
         {
           VmLanguageModule.NAME,
           VmLanguageModule.TERSE_NAME,
           "",
           LanguageRegistry.getLanguage(VmLanguageModule.NAME).getDefaultVersion(),
         },
       });
 }
예제 #6
0
 @Test
 public void testOnlyWorksForMethodsAndConstructors() {
   StatementAndBraceFinder sbf =
       new StatementAndBraceFinder(
           LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME)
               .getDefaultVersion()
               .getLanguageVersionHandler()
               .getDataFlowHandler());
   PLSQLNode node = new ASTMethodDeclaration(1);
   ((AbstractNode) node).testingOnly__setBeginColumn(1);
   sbf.buildDataFlowFor(node);
   // sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
   node = new ASTProgramUnit(1);
   ((AbstractNode) node).testingOnly__setBeginColumn(1);
   sbf.buildDataFlowFor(node);
 }
예제 #7
0
 public AbstractJspRule() {
   super.setLanguage(LanguageRegistry.getLanguage(JspLanguageModule.NAME));
 }
예제 #8
0
파일: FooRule.java 프로젝트: Monits/pmd
 public FooRule() {
   setLanguage(LanguageRegistry.getLanguage(DummyLanguageModule.NAME));
 }