| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ObjectDecor |
|
| 2.0;2 | ||||
| ObjectDecor$ArrayFormatAction |
|
| 2.0;2 | ||||
| ObjectDecor$ObjectContentsFormatAction |
|
| 2.0;2 |
| 1 | /** | |
| 2 | * Copyright (c) 2012-2015, jcabi.com | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: 1) Redistributions of source code must retain the above | |
| 8 | * copyright notice, this list of conditions and the following | |
| 9 | * disclaimer. 2) Redistributions in binary form must reproduce the above | |
| 10 | * copyright notice, this list of conditions and the following | |
| 11 | * disclaimer in the documentation and/or other materials provided | |
| 12 | * with the distribution. 3) Neither the name of the jcabi.com nor | |
| 13 | * the names of its contributors may be used to endorse or promote | |
| 14 | * products derived from this software without specific prior written | |
| 15 | * permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT | |
| 19 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
| 21 | * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 22 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
| 23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
| 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
| 26 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
| 28 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 | */ | |
| 30 | package com.jcabi.log; | |
| 31 | ||
| 32 | import com.jcabi.aspects.Immutable; | |
| 33 | import java.lang.reflect.Field; | |
| 34 | import java.security.AccessController; | |
| 35 | import java.security.PrivilegedAction; | |
| 36 | import java.util.Arrays; | |
| 37 | import java.util.Formattable; | |
| 38 | import java.util.Formatter; | |
| 39 | import lombok.EqualsAndHashCode; | |
| 40 | import lombok.ToString; | |
| 41 | ||
| 42 | /** | |
| 43 | * Format internal structure of an object. | |
| 44 | * @author Marina Kosenko (marina.kosenko@gmail.com) | |
| 45 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 46 | * @version $Id: aff0b3ff32a65cbf92af934aa9f440b2a6e8ce92 $ | |
| 47 | * @since 0.1 | |
| 48 | */ | |
| 49 | 0 | @ToString |
| 50 | 0 | @EqualsAndHashCode(of = "object") |
| 51 | final class ObjectDecor implements Formattable { | |
| 52 | ||
| 53 | /** | |
| 54 | * The object to work with. | |
| 55 | */ | |
| 56 | private final transient Object object; | |
| 57 | ||
| 58 | /** | |
| 59 | * Public ctor. | |
| 60 | * @param obj The object to format | |
| 61 | */ | |
| 62 | 0 | ObjectDecor(final Object obj) { |
| 63 | 0 | this.object = obj; |
| 64 | 0 | } |
| 65 | ||
| 66 | // @checkstyle ParameterNumber (4 lines) | |
| 67 | @Override | |
| 68 | public void formatTo(final Formatter formatter, final int flags, | |
| 69 | final int width, final int precision) { | |
| 70 | 0 | if (this.object == null) { |
| 71 | 0 | formatter.format("NULL"); |
| 72 | 0 | } else if (this.object.getClass().isArray()) { |
| 73 | 0 | formatter.format( |
| 74 | AccessController.doPrivileged( | |
| 75 | new ArrayFormatAction((Object[]) this.object) | |
| 76 | ) | |
| 77 | ); | |
| 78 | } else { | |
| 79 | 0 | final String output = |
| 80 | AccessController.doPrivileged( | |
| 81 | new ObjectDecor.ObjectContentsFormatAction(this.object) | |
| 82 | ); | |
| 83 | 0 | formatter.format(output); |
| 84 | } | |
| 85 | 0 | } |
| 86 | ||
| 87 | /** | |
| 88 | * {@link PrivilegedAction} for obtaining array contents. | |
| 89 | * @author Aleksey Popov (alopen@yandex.ru) | |
| 90 | * @version $Id: aff0b3ff32a65cbf92af934aa9f440b2a6e8ce92 $ | |
| 91 | */ | |
| 92 | 0 | @Immutable |
| 93 | private static final class ArrayFormatAction | |
| 94 | implements PrivilegedAction<String> { | |
| 95 | /** | |
| 96 | * Array to format. | |
| 97 | */ | |
| 98 | private final transient Object[] array; | |
| 99 | ||
| 100 | /** | |
| 101 | * Constructor. | |
| 102 | * @param arr Array to format | |
| 103 | */ | |
| 104 | 0 | ArrayFormatAction(final Object... arr) { |
| 105 | 0 | this.array = Arrays.copyOf(arr, arr.length); |
| 106 | 0 | } |
| 107 | ||
| 108 | @Override | |
| 109 | @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") | |
| 110 | public String run() { | |
| 111 | 0 | final StringBuilder builder = new StringBuilder("["); |
| 112 | 0 | final Formatter formatter = new Formatter(builder); |
| 113 | 0 | for (final Object obj : this.array) { |
| 114 | 0 | new ObjectDecor(obj).formatTo(formatter, 0, 0, 0); |
| 115 | // @checkstyle MultipleStringLiteralsCheck (1 line) | |
| 116 | 0 | builder.append(", "); |
| 117 | } | |
| 118 | 0 | builder.replace(builder.length() - 2, builder.length(), "]"); |
| 119 | 0 | return builder.toString(); |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * {@link PrivilegedAction} for obtaining object contents. | |
| 125 | * @author Marina Kosenko (marina.kosenko@gmail.com) | |
| 126 | * @author Yegor Bugayenko (yegor@teamed.io) | |
| 127 | * @version $Id: aff0b3ff32a65cbf92af934aa9f440b2a6e8ce92 $ | |
| 128 | */ | |
| 129 | 0 | @Immutable |
| 130 | private static final class ObjectContentsFormatAction | |
| 131 | implements PrivilegedAction<String> { | |
| 132 | /** | |
| 133 | * Object to format. | |
| 134 | */ | |
| 135 | private final transient Object object; | |
| 136 | ||
| 137 | /** | |
| 138 | * Constructor. | |
| 139 | * @param obj Object to format | |
| 140 | */ | |
| 141 | 0 | ObjectContentsFormatAction(final Object obj) { |
| 142 | 0 | this.object = obj; |
| 143 | 0 | } |
| 144 | ||
| 145 | @Override | |
| 146 | public String run() { | |
| 147 | 0 | final StringBuilder builder = new StringBuilder("{"); |
| 148 | for (final Field field | |
| 149 | 0 | : this.object.getClass().getDeclaredFields()) { |
| 150 | 0 | field.setAccessible(true); |
| 151 | try { | |
| 152 | 0 | builder.append( |
| 153 | String.format( | |
| 154 | "%s: \"%s\"", | |
| 155 | field.getName(), | |
| 156 | field.get(this.object) | |
| 157 | ) | |
| 158 | ); | |
| 159 | 0 | } catch (final IllegalAccessException ex) { |
| 160 | 0 | throw new IllegalStateException(ex); |
| 161 | 0 | } |
| 162 | // @checkstyle MultipleStringLiteralsCheck (1 line) | |
| 163 | 0 | builder.append(", "); |
| 164 | } | |
| 165 | 0 | builder.replace(builder.length() - 2, builder.length(), "}"); |
| 166 | 0 | return builder.toString(); |
| 167 | }; | |
| 168 | } | |
| 169 | } |