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;
}
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;
}
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());
}
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");
}
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());
}
Aggregations