Search in sources :

Example 36 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class InterfaceMethodTest method parseAndTypeResolveForClass.

// Note: If you're using Eclipse or some other IDE to run this test, you
// _must_ have the regress folder in
// the classpath. Normally the IDE doesn't put source directories themselves
// directly in the classpath, only
// the output directories are in the classpath.
private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz) {
    String sourceFile = clazz.getName().replace('.', '/') + ".java";
    InputStream is = InterfaceMethodTest.class.getClassLoader().getResourceAsStream(sourceFile);
    if (is == null) {
        throw new IllegalArgumentException("Unable to find source file " + sourceFile + " for " + clazz);
    }
    LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.8").getLanguageVersionHandler();
    ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
    languageVersionHandler.getSymbolFacade().start(acu);
    languageVersionHandler.getTypeResolutionFacade(InterfaceMethodTest.class.getClassLoader()).start(acu);
    return acu;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

Example 37 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverJava8Test method parseAndTypeResolveForClass.

// Note: If you're using Eclipse or some other IDE to run this test, you
// _must_ have the regress folder in
// the classpath. Normally the IDE doesn't put source directories themselves
// directly in the classpath, only
// the output directories are in the classpath.
private ASTCompilationUnit parseAndTypeResolveForClass(Class<?> clazz, String version) {
    String sourceFile = clazz.getName().replace('.', '/') + ".java";
    InputStream is = ClassTypeResolverJava8Test.class.getClassLoader().getResourceAsStream(sourceFile);
    if (is == null) {
        throw new IllegalArgumentException("Unable to find source file " + sourceFile + " for " + clazz);
    }
    LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion(version).getLanguageVersionHandler();
    ASTCompilationUnit acu = (ASTCompilationUnit) languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new InputStreamReader(is));
    languageVersionHandler.getSymbolFacade().start(acu);
    languageVersionHandler.getQualifiedNameResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
    languageVersionHandler.getTypeResolutionFacade(ClassTypeResolverJava8Test.class.getClassLoader()).start(acu);
    return acu;
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler)

Example 38 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverJava8Test method testThisExpression.

@Test
public void testThisExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass18(ThisExpression.class);
    List<ASTPrimaryExpression> expressions = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression"), ASTPrimaryExpression.class);
    List<ASTPrimaryPrefix> prefixes = convertList(acu.findChildNodesWithXPath("//VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix"), ASTPrimaryPrefix.class);
    int index = 0;
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.PrimaryThisInterface.class, expressions.get(index).getType());
    assertEquals(ThisExpression.PrimaryThisInterface.class, prefixes.get(index++).getType());
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
    assertEquals("All expressions not tested", index, prefixes.size());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ThisExpression(net.sourceforge.pmd.typeresolution.testdata.ThisExpression) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) Test(org.junit.Test)

Example 39 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class SourceFileScopeTest method testPackageNameFound.

@Test
public void testPackageNameFound() {
    parseCode(TEST2);
    ASTCompilationUnit decl = acu;
    assertEquals(decl.getScope().getEnclosingScope(SourceFileScope.class).getPackageName(), "foo.bar");
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Test(org.junit.Test)

Example 40 with ASTCompilationUnit

use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.

the class ClassTypeResolverTest method testUnaryNumericPromotion.

@Test
public void testUnaryNumericPromotion() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = " + "'unaryNumericPromotion']]//Expression[UnaryExpression]"), ASTExpression.class);
    int index = 0;
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Integer.TYPE, expressions.get(index++).getType());
    assertEquals(Long.TYPE, expressions.get(index++).getType());
    assertEquals(Float.TYPE, expressions.get(index++).getType());
    assertEquals(Double.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Aggregations

ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)79 Test (org.junit.Test)66 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)36 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)27 StringReader (java.io.StringReader)8 RuleContext (net.sourceforge.pmd.RuleContext)8 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)8 ASTImportDeclaration (net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration)7 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)5 ArrayList (java.util.ArrayList)4 ParserOptions (net.sourceforge.pmd.lang.ParserOptions)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 Method (java.lang.reflect.Method)2 List (java.util.List)2 LanguageVersion (net.sourceforge.pmd.lang.LanguageVersion)2 Parser (net.sourceforge.pmd.lang.Parser)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2