use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testMethodInitialConstraints.
@Test
public void testMethodInitialConstraints() throws NoSuchMethodException, JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
List<Variable> variables = new ArrayList<>();
for (int i = 0; i < 2; ++i) {
variables.add(new Variable());
}
Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
List<Constraint> constraints = MethodTypeResolution.produceInitialConstraints(method, argList, variables);
assertEquals(constraints.size(), 3);
// A
assertTrue(constraints.contains(new Constraint(forClass(SuperClassA.class), variables.get(0), LOOSE_INVOCATION)));
assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther.class), variables.get(0), LOOSE_INVOCATION)));
// B
assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther2.class), variables.get(1), LOOSE_INVOCATION)));
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ClassTypeResolverTest method testLiterals.
@Test
public void testLiterals() throws JaxenException {
ASTCompilationUnit acu = parseAndTypeResolveForClass15(Literals.class);
List<ASTLiteral> literals = convertList(acu.findChildNodesWithXPath("//Literal"), ASTLiteral.class);
int index = 0;
// String s = "s";
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(String.class, literals.get(index++).getType());
// boolean boolean1 = false;
assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
assertEquals(Boolean.TYPE, literals.get(index++).getType());
// boolean boolean2 = true;
assertEquals(Boolean.TYPE, literals.get(index).getFirstDescendantOfType(ASTBooleanLiteral.class).getType());
assertEquals(Boolean.TYPE, literals.get(index++).getType());
// Object obj = null;
assertNull(literals.get(index).getFirstDescendantOfType(ASTNullLiteral.class).getType());
assertNull(literals.get(index++).getType());
// byte byte1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// byte byte2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// byte byte3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// short short3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char3 = 007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// char char4 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// int int1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// int int4 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// long long1 = 0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long2 = 0x0F;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long3 = -007;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// long long4 = 0L;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long5 = 0x0Fl;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long6 = -007L;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Long.TYPE, literals.get(index++).getType());
// long long7 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// float float1 = 0.0f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float2 = -10e+01f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float3 = 0x08.08p3f;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Float.TYPE, literals.get(index++).getType());
// float float4 = 0xFF;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// float float5 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// double double1 = 0.0;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double2 = -10e+01;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double3 = 0x08.08p3;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Double.TYPE, literals.get(index++).getType());
// double double4 = 0xFF;
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Integer.TYPE, literals.get(index++).getType());
// double double5 = 'a';
assertEquals(0, literals.get(index).jjtGetNumChildren());
assertEquals(Character.TYPE, literals.get(index++).getType());
// Make sure we got them all.
assertEquals("All literals not tested", index, literals.size());
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class StatementAndBraceFinderTest method testOnlyWorksForMethodsAndConstructors.
@Test(expected = RuntimeException.class)
public void testOnlyWorksForMethodsAndConstructors() {
StatementAndBraceFinder sbf = new StatementAndBraceFinder(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler().getDataFlowHandler());
sbf.buildDataFlowFor(new ASTMethodDeclaration(1));
sbf.buildDataFlowFor(new ASTConstructorDeclaration(1));
sbf.buildDataFlowFor(new ASTCompilationUnit(1));
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class ProjectMemoizerTest method forceMemoizationTest.
@Test
public void forceMemoizationTest() {
ASTCompilationUnit acu = ParserTstUtil.parseJavaDefaultVersion(MetricsVisitorTestData.class);
List<Integer> reference = visitWith(acu, true);
List<Integer> real = visitWith(acu, true);
assertEquals(reference.size(), real.size());
// we force recomputation so each result should be different
for (int i = 0; i < reference.size(); i++) {
assertNotEquals(reference.get(i), real.get(i));
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit in project pmd by pmd.
the class SignatureTest method testSetterDetection.
@Test
public void testSetterDetection() {
ASTCompilationUnit compilationUnit = parseJava17(SetterDetection.class);
compilationUnit.jjtAccept(new JavaParserVisitorAdapter() {
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
assertEquals(Role.GETTER_OR_SETTER, Role.get(node));
return data;
}
}, null);
}
Aggregations