Search in sources :

Example 16 with ToString

use of lombok.ToString in project pancm_project by xuwujing.

the class LombokTest method main.

/**
 * The entry point of application.
 *
 * @param args the input arguments
 */
public static void main(String[] args) {
    LombokTest student = new LombokTest();
    LombokTest student2 = new LombokTest(2, "zhangsan", 3);
    student.setId(1);
    student.setName("xuwujing");
    student.setClassId(1);
    log.info("id:{},姓名:{},班级:{}", student.getId(), student.getName(), student.getClassId());
    log.info("学生信息:{}", student2.toString());
    String name = "xuwujing";
    String name2 = null;
    try {
        test1();
        test2();
        test3(name);
        test4(name2);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NullPointerException e2) {
        System.out.println("出现了空指针!");
        e2.printStackTrace();
    }
/*
 				INFO  com.pancm.test.pojoTest.Student - id:1,姓名:xuwujing,班级:1
				INFO  com.pancm.test.pojoTest.Student - 学生信息:Student(name=zhangsan)
				成功创建!
				成功创建!
				xuwujing
				出现了空指针!
				java.lang.NullPointerException: name is marked @NonNull but is null
				at com.pancm.test.pojoTest.Student.test4(Student.java:143)
				at com.pancm.test.pojoTest.Student.main(Student.java:81)
		 */
}
Also used : ToString(lombok.ToString) IOException(java.io.IOException)

Example 17 with ToString

use of lombok.ToString in project lombok by rzwitserloot.

the class HandleToString method createToString.

static JCMethodDecl createToString(JavacNode typeNode, Collection<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) {
    JavacTreeMaker maker = typeNode.getTreeMaker();
    JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil());
    JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation));
    JCExpression returnType = genJavaLangTypeRef(typeNode, "String");
    boolean first = true;
    String typeName = getTypeName(typeNode);
    String infix = ", ";
    String suffix = ")";
    String prefix;
    if (callSuper) {
        prefix = typeName + "(super=";
    } else if (fields.isEmpty()) {
        prefix = typeName + "()";
    } else if (includeFieldNames) {
        prefix = typeName + "(" + ((JCVariableDecl) fields.iterator().next().get()).name.toString() + "=";
    } else {
        prefix = typeName + "(";
    }
    JCExpression current = maker.Literal(prefix);
    if (callSuper) {
        JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil());
        current = maker.Binary(CTC_PLUS, current, callToSuper);
        first = false;
    }
    for (JavacNode fieldNode : fields) {
        JCExpression expr;
        JCExpression fieldAccessor = createFieldAccessor(maker, fieldNode, fieldAccess);
        JCExpression fieldType = getFieldType(fieldNode, fieldAccess);
        // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option.
        boolean fieldIsPrimitive = fieldType instanceof JCPrimitiveTypeTree;
        boolean fieldIsPrimitiveArray = fieldType instanceof JCArrayTypeTree && ((JCArrayTypeTree) fieldType).elemtype instanceof JCPrimitiveTypeTree;
        boolean fieldIsObjectArray = !fieldIsPrimitiveArray && fieldType instanceof JCArrayTypeTree;
        @SuppressWarnings("unused") boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray;
        if (fieldIsPrimitiveArray || fieldIsObjectArray) {
            JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString");
            expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(fieldAccessor));
        } else
            expr = fieldAccessor;
        if (first) {
            current = maker.Binary(CTC_PLUS, current, expr);
            first = false;
            continue;
        }
        if (includeFieldNames) {
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + fieldNode.getName() + "="));
        } else {
            current = maker.Binary(CTC_PLUS, current, maker.Literal(infix));
        }
        current = maker.Binary(CTC_PLUS, current, expr);
    }
    if (!first)
        current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix));
    JCStatement returnStatement = maker.Return(current);
    JCBlock body = maker.Block(0, List.of(returnStatement));
    return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("toString"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source, typeNode.getContext());
}
Also used : JCBlock(com.sun.tools.javac.tree.JCTree.JCBlock) JavacTreeMaker(lombok.javac.JavacTreeMaker) ToString(lombok.ToString) JCStatement(com.sun.tools.javac.tree.JCTree.JCStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTypeParameter(com.sun.tools.javac.tree.JCTree.JCTypeParameter) JCMethodInvocation(com.sun.tools.javac.tree.JCTree.JCMethodInvocation) JCPrimitiveTypeTree(com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree) JCExpression(com.sun.tools.javac.tree.JCTree.JCExpression) JavacNode(lombok.javac.JavacNode) JCModifiers(com.sun.tools.javac.tree.JCTree.JCModifiers) JCAnnotation(com.sun.tools.javac.tree.JCTree.JCAnnotation) JCArrayTypeTree(com.sun.tools.javac.tree.JCTree.JCArrayTypeTree)

Example 18 with ToString

use of lombok.ToString in project lombok by rzwitserloot.

the class HandleToString method handle.

@Override
public void handle(AnnotationValues<ToString> annotation, JCAnnotation ast, JavacNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.TO_STRING_FLAG_USAGE, "@ToString");
    deleteAnnotationIfNeccessary(annotationNode, ToString.class);
    ToString ann = annotation.getInstance();
    List<String> excludes = List.from(ann.exclude());
    List<String> includes = List.from(ann.of());
    JavacNode typeNode = annotationNode.up();
    checkForBogusFieldNames(typeNode, annotation);
    Boolean callSuper = ann.callSuper();
    if (!annotation.isExplicit("callSuper"))
        callSuper = null;
    if (!annotation.isExplicit("exclude"))
        excludes = null;
    if (!annotation.isExplicit("of"))
        includes = null;
    if (excludes != null && includes != null) {
        excludes = null;
        annotation.setWarning("exclude", "exclude and of are mutually exclusive; the 'exclude' parameter will be ignored.");
    }
    Boolean doNotUseGettersConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_DO_NOT_USE_GETTERS);
    boolean doNotUseGetters = annotation.isExplicit("doNotUseGetters") || doNotUseGettersConfiguration == null ? ann.doNotUseGetters() : doNotUseGettersConfiguration;
    FieldAccess fieldAccess = doNotUseGetters ? FieldAccess.PREFER_FIELD : FieldAccess.GETTER;
    Boolean fieldNamesConfiguration = annotationNode.getAst().readConfiguration(ConfigurationKeys.TO_STRING_INCLUDE_FIELD_NAMES);
    boolean includeFieldNames = annotation.isExplicit("includeFieldNames") || fieldNamesConfiguration == null ? ann.includeFieldNames() : fieldNamesConfiguration;
    generateToString(typeNode, annotationNode, excludes, includes, includeFieldNames, callSuper, true, fieldAccess);
}
Also used : JavacNode(lombok.javac.JavacNode) ToString(lombok.ToString) ToString(lombok.ToString)

Example 19 with ToString

use of lombok.ToString in project L42 by ElvisResearchGroup.

the class _Aux method isConsistent.

static boolean isConsistent(ClassB cb) {
    int countWalkBy = 0;
    HashSet<String> keys = new HashSet<String>();
    for (Member m : cb.getMs()) {
        if (m instanceof MethodWithType) {
            MethodWithType mwt = (MethodWithType) m;
            String key = mwt.getMs().toString();
            assert !keys.contains(key);
            keys.add(key);
        //assert mwt.getMt().getTDocs().size() == mwt.getMt().getTs().size();
        }
        if (m instanceof NestedClass) {
            NestedClass nc = (NestedClass) m;
            String key = nc.getName().toString();
            assert !keys.contains(key);
            keys.add(key);
            if (nc.getInner() instanceof ExpCore.WalkBy) {
                countWalkBy += 1;
            }
        }
        if (m instanceof MethodImplemented) {
            MethodImplemented mi = (MethodImplemented) m;
            String key = mi.getS().toString();
            assert !keys.contains(key);
            keys.add(key);
        }
    }
    //     || ((cb.getPhase()!=ast.ExpCore.ClassB.Phase.None && !cb.getUniqueId().isEmpty()) );
    assert countWalkBy <= 1 : cb;
    return true;
}
Also used : MethodImplemented(ast.ExpCore.ClassB.MethodImplemented) NestedClass(ast.ExpCore.ClassB.NestedClass) ToString(lombok.ToString) MethodWithType(ast.ExpCore.ClassB.MethodWithType) Member(ast.ExpCore.ClassB.Member) HashSet(java.util.HashSet)

Example 20 with ToString

use of lombok.ToString in project cas by apereo.

the class ShibbolethCompatiblePersistentIdGenerator method generate.

@Override
public String generate(final Principal principal, final Service service) {
    final Map<String, Object> attributes = principal.getAttributes();
    final String principalId = StringUtils.isNotBlank(this.attribute) && attributes.containsKey(this.attribute) ? attributes.get(this.attribute).toString() : principal.getId();
    return generate(principalId, service.getId());
}
Also used : 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