Search in sources :

Example 6 with ToString

use of lombok.ToString in project lombok by rzwitserloot.

the class HandleToString method generateToString.

public void generateToString(JavacNode typeNode, JavacNode source, List<String> excludes, List<String> includes, boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) {
    boolean notAClass = true;
    if (typeNode.get() instanceof JCClassDecl) {
        long flags = ((JCClassDecl) typeNode.get()).mods.flags;
        notAClass = (flags & (Flags.INTERFACE | Flags.ANNOTATION)) != 0;
    }
    if (callSuper == null) {
        try {
            callSuper = ((Boolean) ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue();
        } catch (Exception ignore) {
        }
    }
    if (notAClass) {
        source.addError("@ToString is only supported on a class or enum.");
        return;
    }
    ListBuffer<JavacNode> nodesForToString = new ListBuffer<JavacNode>();
    if (includes != null) {
        for (JavacNode child : typeNode.down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
            if (includes.contains(fieldDecl.name.toString()))
                nodesForToString.append(child);
        }
    } else {
        for (JavacNode child : typeNode.down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            JCVariableDecl fieldDecl = (JCVariableDecl) child.get();
            // Skip static fields.
            if ((fieldDecl.mods.flags & Flags.STATIC) != 0)
                continue;
            // Skip excluded fields.
            if (excludes != null && excludes.contains(fieldDecl.name.toString()))
                continue;
            // Skip fields that start with $.
            if (fieldDecl.name.toString().startsWith("$"))
                continue;
            nodesForToString.append(child);
        }
    }
    switch(methodExists("toString", typeNode, 0)) {
        case NOT_EXISTS:
            JCMethodDecl method = createToString(typeNode, nodesForToString.toList(), includeFieldNames, callSuper, fieldAccess, source.get());
            injectMethod(typeNode, method);
            break;
        case EXISTS_BY_LOMBOK:
            break;
        default:
        case EXISTS_BY_USER:
            if (whineIfExists) {
                source.addWarning("Not generating toString(): A method with that name already exists");
            }
            break;
    }
}
Also used : JCClassDecl(com.sun.tools.javac.tree.JCTree.JCClassDecl) JCMethodDecl(com.sun.tools.javac.tree.JCTree.JCMethodDecl) JavacNode(lombok.javac.JavacNode) ListBuffer(com.sun.tools.javac.util.ListBuffer) ToString(lombok.ToString) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Example 7 with ToString

use of lombok.ToString in project lombok by rzwitserloot.

the class HandleToString method generateToString.

public void generateToString(EclipseNode typeNode, EclipseNode errorNode, List<String> excludes, List<String> includes, boolean includeFieldNames, Boolean callSuper, boolean whineIfExists, FieldAccess fieldAccess) {
    TypeDeclaration typeDecl = null;
    if (typeNode.get() instanceof TypeDeclaration)
        typeDecl = (TypeDeclaration) typeNode.get();
    int modifiers = typeDecl == null ? 0 : typeDecl.modifiers;
    boolean notAClass = (modifiers & (ClassFileConstants.AccInterface | ClassFileConstants.AccAnnotation)) != 0;
    if (typeDecl == null || notAClass) {
        errorNode.addError("@ToString is only supported on a class or enum.");
    }
    if (callSuper == null) {
        try {
            callSuper = ((Boolean) ToString.class.getMethod("callSuper").getDefaultValue()).booleanValue();
        } catch (Exception ignore) {
        }
    }
    List<EclipseNode> nodesForToString = new ArrayList<EclipseNode>();
    if (includes != null) {
        for (EclipseNode child : typeNode.down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
            if (includes.contains(new String(fieldDecl.name)))
                nodesForToString.add(child);
        }
    } else {
        for (EclipseNode child : typeNode.down()) {
            if (child.getKind() != Kind.FIELD)
                continue;
            FieldDeclaration fieldDecl = (FieldDeclaration) child.get();
            if (!filterField(fieldDecl))
                continue;
            // Skip excluded fields.
            if (excludes != null && excludes.contains(new String(fieldDecl.name)))
                continue;
            nodesForToString.add(child);
        }
    }
    switch(methodExists("toString", typeNode, 0)) {
        case NOT_EXISTS:
            MethodDeclaration toString = createToString(typeNode, nodesForToString, includeFieldNames, callSuper, errorNode.get(), fieldAccess);
            injectMethod(typeNode, toString);
            break;
        case EXISTS_BY_LOMBOK:
            break;
        default:
        case EXISTS_BY_USER:
            if (whineIfExists) {
                errorNode.addWarning("Not generating toString(): A method with that name already exists");
            }
    }
}
Also used : MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ArrayList(java.util.ArrayList) EclipseNode(lombok.eclipse.EclipseNode) ToString(lombok.ToString) ToString(lombok.ToString) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Example 8 with ToString

use of lombok.ToString in project incubator-gobblin by apache.

the class MySqlBufferedInserter method createPrepareStatementStr.

@Override
protected String createPrepareStatementStr(int batchSize) {
    final String VALUE_FORMAT = "(%s)";
    StringBuilder sb = new StringBuilder(this.insertStmtPrefix);
    String values = String.format(VALUE_FORMAT, JOINER_ON_COMMA.useForNull("?").join(new String[this.columnNames.size()]));
    sb.append(values);
    for (int i = 1; i < batchSize; i++) {
        sb.append(',').append(values);
    }
    return sb.append(';').toString();
}
Also used : ToString(lombok.ToString)

Example 9 with ToString

use of lombok.ToString in project incubator-gobblin by apache.

the class TeradataBufferedInserter method createPrepareStatementStr.

@Override
protected String createPrepareStatementStr(int batchSize) {
    final String VALUE_FORMAT = "(%s)";
    StringBuilder sb = new StringBuilder(this.insertStmtPrefix);
    String values = String.format(VALUE_FORMAT, JOINER_ON_COMMA.useForNull("?").join(new String[this.columnNames.size()]));
    sb.append(values);
    return sb.append(';').toString();
}
Also used : ToString(lombok.ToString)

Example 10 with ToString

use of lombok.ToString in project jcabi-github by jcabi.

the class MkIssueEvents method create.

/**
 * Creates a new issue event.
 * This has no equivalent in GitHub's public API, since GitHub generates
 * events automatically in response to some other API calls.
 * @param type Type of event
 * @param issue ID number of issue the event is regarding
 * @param login Username of actor who caused the event
 * @param label Label added or removed
 * @return The newly created issue event
 * @throws IOException If there is any I/O problem
 * @todo #1063:30min Make it possible to set the "assignee" field for
 *  "assigned"/"unassigned" events. Make it possible to set the
 *  "milestone" field for "milestoned"/"demilestoned" events. Make it
 *  possible to set the "rename" field for "renamed" events. Make it
 *  possible to set the "commit_id" field for events related to commits.
 *  See https://developer.github.com/v3/issues/events/ for details.
 * @checkstyle ParameterNumberCheck (4 lines)
 */
public Event create(final String type, final int issue, final String login, final Optional<String> label) throws IOException {
    final String created = new Github.Time().toString();
    this.storage.lock();
    final int number;
    try {
        number = 1 + this.storage.xml().xpath(String.format("%s/issue-event/number/text()", this.xpath())).size();
        Directives directives = new Directives().xpath(this.xpath()).add("issue-event").add("issue").set(Integer.toString(issue)).up().add("number").set(Integer.toString(number)).up().add("event").set(type).up().add("created_at").set(created).up().add("login").set(login).up();
        if (label.isPresent()) {
            directives = directives.add("label").set(label.get()).up();
        }
        this.storage.apply(directives);
    } finally {
        this.storage.unlock();
    }
    Logger.info(MkEvent.class, "issue event #%d of type %s created in %s for issue #%d by %s", number, type, this.self, issue, login);
    return this.get(number);
}
Also used : Github(com.jcabi.github.Github) Directives(org.xembly.Directives) ToString(lombok.ToString)

Aggregations

ToString (lombok.ToString)28 Map (java.util.Map)6 HashMap (java.util.HashMap)5 List (java.util.List)4 HashSet (java.util.HashSet)3 EclipseNode (lombok.eclipse.EclipseNode)3 Slf4j (lombok.extern.slf4j.Slf4j)3 lombok.val (lombok.val)3 JsonIgnore (com.fasterxml.jackson.annotation.JsonIgnore)2 Github (com.jcabi.github.Github)2 JCVariableDecl (com.sun.tools.javac.tree.JCTree.JCVariableDecl)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 LinkedHashMap (java.util.LinkedHashMap)2 Optional (java.util.Optional)2 Collectors (java.util.stream.Collectors)2 Collectors.toList (java.util.stream.Collectors.toList)2 Collectors.toMap (java.util.stream.Collectors.toMap)2 Getter (lombok.Getter)2