use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.
the class JavaParserTest method parseIntersectionType.
@Test
public void parseIntersectionType() {
String code = "(Runnable & Serializable) (() -> {})";
Expression expression = JavaParser.parseExpression(code);
Type type = expression.asCastExpr().getType();
assertTrue(type instanceof IntersectionType);
IntersectionType intersectionType = type.asIntersectionType();
assertEquals(2, intersectionType.getElements().size());
assertTrue(intersectionType.getElements().get(0) instanceof ClassOrInterfaceType);
assertEquals("Runnable", intersectionType.getElements().get(0).asClassOrInterfaceType().getNameAsString());
assertTrue(intersectionType.getElements().get(1) instanceof ClassOrInterfaceType);
assertEquals("Serializable", intersectionType.getElements().get(1).asClassOrInterfaceType().getNameAsString());
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.
the class Java7ValidatorTest method multiCatchWithOneElement.
@Test
public void multiCatchWithOneElement() {
UnionType unionType = new UnionType();
unionType.getElements().add(new ClassOrInterfaceType());
List<Problem> problems = new ArrayList<>();
new Java7Validator().accept(unionType, new ProblemReporter(problems::add));
assertProblems(problems, "Union type (multi catch) must have at least two elements.");
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.
the class ModuleDeclarationTest method jlsExample1.
@Test
public void jlsExample1() {
CompilationUnit cu = parse("@Foo(1) @Foo(2) @Bar " + "module M.N {" + " requires A.B;" + " requires transitive C.D;" + " requires static E.F;" + " requires transitive static G.H;" + "" + " exports P.Q;" + " exports R.S to T1.U1, T2.U2;" + "" + " opens P.Q;" + " opens R.S to T1.U1, T2.U2;" + "" + " uses V.W;" + " provides X.Y with Z1.Z2, Z3.Z4;" + "}");
ModuleDeclaration module = cu.getModule().get();
assertEquals("M.N", module.getNameAsString());
assertEquals(false, module.isOpen());
assertThat(module.getAnnotations()).containsExactly(new SingleMemberAnnotationExpr(new Name("Foo"), new IntegerLiteralExpr("1")), new SingleMemberAnnotationExpr(new Name("Foo"), new IntegerLiteralExpr("2")), new MarkerAnnotationExpr(new Name("Bar")));
ModuleRequiresStmt moduleRequiresStmt = module.getModuleStmts().get(0).asModuleRequiresStmt();
assertThat(moduleRequiresStmt.getNameAsString()).isEqualTo("A.B");
assertThat(moduleRequiresStmt.getModifiers()).isEmpty();
ModuleExportsStmt moduleExportsStmt = module.getModuleStmts().get(5).asModuleExportsStmt();
assertThat(moduleExportsStmt.getNameAsString()).isEqualTo("R.S");
assertThat(moduleExportsStmt.getModuleNames()).containsExactly(parseName("T1.U1"), parseName("T2.U2"));
ModuleOpensStmt moduleOpensStmt = module.getModuleStmts().get(7).asModuleOpensStmt();
assertThat(moduleOpensStmt.getNameAsString()).isEqualTo("R.S");
assertThat(moduleOpensStmt.getModuleNames()).containsExactly(parseName("T1.U1"), parseName("T2.U2"));
ModuleUsesStmt moduleUsesStmt = module.getModuleStmts().get(8).asModuleUsesStmt();
assertThat(moduleUsesStmt.getType().toString()).isEqualTo("V.W");
ModuleProvidesStmt moduleProvidesStmt = module.getModuleStmts().get(9).asModuleProvidesStmt();
assertThat(moduleProvidesStmt.getType().toString()).isEqualTo("X.Y");
assertThat(moduleProvidesStmt.getWithTypes()).containsExactly(new ClassOrInterfaceType(parseClassOrInterfaceType("Z1"), "Z2"), new ClassOrInterfaceType(parseClassOrInterfaceType("Z3"), "Z4"));
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project javaparser by javaparser.
the class NodeWithMembersBuildersTest method testGetMethodsWithParameterTypes.
@Test
public void testGetMethodsWithParameterTypes() {
classDeclaration.addMethod("foo", Modifier.PUBLIC);
MethodDeclaration addMethod2 = classDeclaration.addMethod("foo", Modifier.PUBLIC).addParameter(int.class, "overload");
ClassOrInterfaceType type = parseClassOrInterfaceType("List");
type.setTypeArguments(parseClassOrInterfaceType("String"));
MethodDeclaration methodWithListParam = classDeclaration.addMethod("fooList", Modifier.PUBLIC).addParameter(type, "overload");
MethodDeclaration addMethod3 = classDeclaration.addMethod("foo2", Modifier.PUBLIC).addParameter(int.class, "overload");
List<MethodDeclaration> methodsByParam = classDeclaration.getMethodsByParameterTypes(int.class);
assertEquals(2, methodsByParam.size());
assertTrue(methodsByParam.contains(addMethod2));
assertTrue(methodsByParam.contains(addMethod3));
List<MethodDeclaration> methodsByParam2 = classDeclaration.getMethodsByParameterTypes("List<String>");
assertEquals(1, methodsByParam2.size());
assertTrue(methodsByParam2.contains(methodWithListParam));
}
use of com.github.javaparser.ast.type.ClassOrInterfaceType in project drools by kiegroup.
the class ExpressionTyper method drlNameExpr.
private Optional<TypedExpressionCursor> drlNameExpr(Expression drlxExpr, DrlNameExpr firstNode, boolean isInLineCast, java.lang.reflect.Type originalTypeCursor) {
String firstName = firstNode.getName().getIdentifier();
java.lang.reflect.Type typeCursor;
// In OOPath a declaration is based on a position rather than a name.
// Only an OOPath chunk can have a backreference expression
Optional<DeclarationSpec> backReference = empty();
if (firstNode.getBackReferencesCount() > 0) {
List<DeclarationSpec> ooPathDeclarations = ruleContext.getOOPathDeclarations();
DeclarationSpec backReferenceDeclaration = ooPathDeclarations.get(ooPathDeclarations.size() - 1 - firstNode.getBackReferencesCount());
typeCursor = backReferenceDeclaration.getDeclarationClass();
backReference = of(backReferenceDeclaration);
context.addUsedDeclarations(backReferenceDeclaration.getBindingId());
} else {
typeCursor = originalTypeCursor;
}
try {
Class<?> resolvedType = ruleContext.getTypeResolver().resolveType(firstName);
return of(new TypedExpressionCursor(new NameExpr(firstName), resolvedType));
} catch (ClassNotFoundException e) {
// ignore
}
Class<?> classCursor = toRawClass(typeCursor);
if (classCursor != null) {
Method firstAccessor = DrlxParseUtil.getAccessor(!isInLineCast ? classCursor : patternType, firstName, ruleContext);
if (firstAccessor != null) {
if (!"".equals(firstName)) {
context.addReactOnProperties(firstName);
}
NameExpr thisAccessor = new NameExpr(THIS_PLACEHOLDER);
NameExpr scope = backReference.map(d -> new NameExpr(d.getBindingId())).orElse(thisAccessor);
Expression fieldAccessor = new MethodCallExpr(scope, firstAccessor.getName());
if (isInLineCast) {
return of(new TypedExpressionCursor(fieldAccessor, typeCursor));
}
Optional<java.lang.reflect.Type> castType = ruleContext.explicitCastType(firstName).flatMap(t -> safeResolveType(ruleContext.getTypeResolver(), t.asString()));
if (castType.isPresent()) {
java.lang.reflect.Type typeOfFirstAccessor = castType.get();
ClassOrInterfaceType typeWithoutDollar = toClassOrInterfaceType(typeOfFirstAccessor.getTypeName());
return of(new TypedExpressionCursor(addCastToExpression(typeWithoutDollar, fieldAccessor, false), typeOfFirstAccessor));
}
return of(new TypedExpressionCursor(fieldAccessor, firstAccessor.getGenericReturnType()));
}
Field field = DrlxParseUtil.getField(classCursor, firstName);
if (field != null) {
NameExpr scope = new NameExpr(Modifier.isStatic(field.getModifiers()) ? classCursor.getCanonicalName() : THIS_PLACEHOLDER);
return of(new TypedExpressionCursor(new FieldAccessExpr(scope, field.getName()), field.getType()));
}
}
Optional<DeclarationSpec> declarationById = ruleContext.getDeclarationById(firstName);
if (declarationById.isPresent()) {
// do NOT append any reactOnProperties.
// because reactOnProperties is referring only to the properties of the type of the pattern, not other declarations properites.
context.addUsedDeclarations(firstName);
typeCursor = isInLineCast ? originalTypeCursor : declarationById.get().getDeclarationClass();
return of(new TypedExpressionCursor(new NameExpr(firstName), typeCursor));
}
if (packageModel.getGlobals().containsKey(firstName)) {
context.addUsedDeclarations(firstName);
return of(new TypedExpressionCursor(new NameExpr(firstName), packageModel.getGlobals().get(firstName)));
}
final Optional<Node> rootNode = findRootNodeViaParent(drlxExpr);
rootNode.ifPresent(n -> {
// In the error messages HalfBinary are transformed to Binary
Node withHalfBinaryReplaced = replaceAllHalfBinaryChildren(n);
ruleContext.addCompilationError(new ParseExpressionErrorResult((Expression) withHalfBinaryReplaced, ruleContext.getCurrentConstraintDescr()));
});
return empty();
}
Aggregations