public BodyDeclaration getTreeNode() { // System.out.println("Building Method " + node.getName()); String finalName = name + type.name() + Config.get().getTestMethodSuffix(); MethodDeclaration result = new MethodDeclaration(ModifierSet.PUBLIC, new VoidType(), finalName); result.setComment( new BlockComment( "\n\t\tGenerated Unity test\n\t\t" + name + " - " + type.name() + " test." + "\n\t")); AnnotationExpr anno = new MarkerAnnotationExpr(new NameExpr("Test")); List<AnnotationExpr> annoList = new ArrayList<>(); annoList.add(anno); result.setAnnotations(annoList); BlockStmt body = new BlockStmt(); List<Statement> statements = new ArrayList<>(); statements.add(instance.getStatement()); body.setStmts(statements); result.setBody(body); // Set any mocks // Set instance // Set call parameters // Set call result // Set assertions return result; }
void changeTypeContext(ResolutionContext old, ResolutionContext new_, MethodDeclaration m) { m.setType(changeTypeContext(old, new_, m.getType())); m.setBody( new BlockStmt( Collections.singletonList( new ThrowStmt( new ObjectCreationExpr( null, new ClassOrInterfaceType("UnsupportedOperationException"), Collections.emptyList()))))); NodeUtil.forChildren( m, node -> { Expression scope = node.getScope(); // TODO: Currently guesses that it's a type name if first character is uppercase. // Should check for fields/variables which match instead if (scope instanceof NameExpr && Character.isUpperCase(((NameExpr) scope).getName().charAt(0))) { String name = ((NameExpr) scope).getName(); node.setScope(ASTHelper.createNameExpr(new_.typeToString(old.resolve(name)))); } }, MethodCallExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), VariableDeclarationExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), TypeExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), com.github.javaparser.ast.body.Parameter.class); }
@Override public void visit(MethodDeclaration methodDeclaration, Object arg) { if (methodDeclaration.getBody() != null) { for (Statement statement : methodDeclaration.getBody().getStmts()) { // check for starting a block statement if (statement instanceof SwitchStmt) { SwitchStmt switchStmt = (SwitchStmt) statement; for (SwitchEntryStmt switchEntryStmt : switchStmt.getEntries()) { reportExcessiveIndentation(switchEntryStmt.getStmts()); } } else if (statement instanceof IfStmt) { Statement currentIf = statement; do { reportExcessiveIndentation(((IfStmt) currentIf).getThenStmt()); currentIf = ((IfStmt) currentIf).getElseStmt(); } while (currentIf instanceof IfStmt); } else if (statement instanceof WhileStmt) { reportExcessiveIndentation(((WhileStmt) statement).getBody()); } else if (statement instanceof ForeachStmt) { reportExcessiveIndentation(((ForeachStmt) statement).getBody()); } else if (statement instanceof ForStmt) { reportExcessiveIndentation(((ForStmt) statement).getBody()); } else if (statement instanceof DoStmt) { reportExcessiveIndentation(((DoStmt) statement).getBody()); } } } super.visit(methodDeclaration, arg); }
@Override public void setTypeVariables(List<TypeVariable> typeVariables) { declaration.setTypeParameters( typeVariables .stream() .map(getContext()::unresolveTypeVariable) .collect(Collectors.toList())); }
// This generates all the test methods of the class, given a starting node private static List<BodyDeclaration> generateTestMethods(RoundTripPathTreeNode node) { final List<BlockStmt> bodies = generateTestBodies(node); final List<BodyDeclaration> methods = new ArrayList<BodyDeclaration>(); int i = 0; for (BlockStmt body : bodies) { final MethodDeclaration method = new MethodDeclaration( ModifierSet.PUBLIC, new VoidType(), "conformanceTest" + i++, Collections.<Parameter>emptyList()); method.getAnnotations().add(new MarkerAnnotationExpr(new NameExpr("Test"))); method.setBody(body); methods.add(method); } return methods; }
@Override public List<TypeVariable> getTypeVariables() { return declaration .getTypeParameters() .stream() .map(getContext()::resolveTypeVariable) .collect(Collectors.toList()); }
@Override public void add(MethodInfo method) { MethodDeclaration methodDeclaration; if (method instanceof MethodDeclarationWrapper) { val wrapper = (MethodDeclarationWrapper) method; methodDeclaration = (MethodDeclaration) wrapper.declaration.clone(); methodDeclaration.setAnnotations(Collections.emptyList()); wrapper .getClassInfo() .changeTypeContext(wrapper.getContext(), getContext(), methodDeclaration); } else { methodDeclaration = new MethodDeclaration(); new MethodDeclarationWrapper(methodDeclaration).setAll(method); } addMember(methodDeclaration); }
@Override public List<Parameter> getParameters() { return declaration .getParameters() .stream() .map( (parameter) -> new Parameter( getContext().resolve(parameter.getType()), parameter.getId().getName())) .collect(Collectors.toList()); }
private JavaMethod createMethod(MethodDeclaration method) { String className = classStack.peek().getName(); String name = method.getName(); boolean isStatic = ModifierSet.hasModifier(method.getModifiers(), ModifierSet.STATIC); String returnType = method.getType().toString(); ArrayList<Argument> arguments = new ArrayList<Argument>(); if (method.getParameters() != null) { for (Parameter parameter : method.getParameters()) { arguments.add(new Argument(getArgumentType(parameter), parameter.getId().getName())); } } return new JavaMethod( className, name, isStatic, returnType, null, arguments, method.getBeginLine(), method.getEndLine()); }
@Override public Boolean visit(final MethodDeclaration n1, final Node arg) { final MethodDeclaration n2 = (MethodDeclaration) arg; // javadoc are checked at CompilationUnit if (n1.getModifiers() != n2.getModifiers()) { return Boolean.FALSE; } if (n1.getArrayCount() != n2.getArrayCount()) { return Boolean.FALSE; } if (!objEquals(n1.getName(), n2.getName())) { return Boolean.FALSE; } if (!nodeEquals(n1.getType(), n2.getType())) { return Boolean.FALSE; } if (!nodesEquals(n1.getAnnotations(), n2.getAnnotations())) { return Boolean.FALSE; } if (!nodeEquals(n1.getBody(), n2.getBody())) { return Boolean.FALSE; } if (!nodesEquals(n1.getParameters(), n2.getParameters())) { return Boolean.FALSE; } if (!nodesEquals(n1.getThrows(), n2.getThrows())) { return Boolean.FALSE; } if (!nodesEquals(n1.getTypeParameters(), n2.getTypeParameters())) { return Boolean.FALSE; } if (n1.isDefault() != n2.isDefault()) { return Boolean.FALSE; } return Boolean.TRUE; }
@Override public AccessFlags getAccessFlags() { return new AccessFlags(declaration.getModifiers()); }
@Override public void setReturnType(Type type) { declaration.setType(setType(type, declaration.getType())); }
@Override public Type getReturnType() { return getContext().resolve(declaration.getType()); }
@Override public String getName() { return declaration.getName(); }
@Override public void setName(String name) { declaration.setName(name); }
@Override public List<Annotation> getAnnotations() { return SourceInfo.this.getAnnotationsInternal(declaration.getAnnotations()); }
@Override public void setAccessFlags(AccessFlags accessFlags) { declaration.setModifiers(accessFlags.access); }
@Override public void visit(final MethodDeclaration n, final A arg) { visitComment(n.getComment(), arg); visitAnnotations(n, arg); if (n.getTypeParameters() != null) { for (final TypeParameter t : n.getTypeParameters()) { t.accept(this, arg); } } n.getElementType().accept(this, arg); n.getNameExpr().accept(this, arg); if (n.getParameters() != null) { for (final Parameter p : n.getParameters()) { p.accept(this, arg); } } if (n.getThrows() != null) { for (final ReferenceType name : n.getThrows()) { name.accept(this, arg); } } if (n.getBody() != null) { n.getBody().accept(this, arg); } }