|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package example; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Set; |
| 21 | +import javax.lang.model.element.TypeElement; |
| 22 | +import org.apache.logging.log4j.plugins.Namespace; |
| 23 | +import org.apache.logging.log4j.plugins.Plugin; |
| 24 | +import org.apache.logging.log4j.plugins.PluginBuilderAttribute; |
| 25 | +import org.apache.logging.log4j.plugins.PluginElement; |
| 26 | +import org.apache.logging.log4j.plugins.PluginFactory; |
| 27 | + |
| 28 | +/** |
| 29 | + * Example plugin |
| 30 | + * <p> |
| 31 | + * This is an example plugin. It has the following characteristics: |
| 32 | + * </p> |
| 33 | + * <ol> |
| 34 | + * <li>Plugin name: {@code MyPlugin},</li> |
| 35 | + * <li>Namespace: default (i.e. {@code Core}).</li> |
| 36 | + * </ol> |
| 37 | + * <p> |
| 38 | + * It also implements: |
| 39 | + * </p> |
| 40 | + * <ul> |
| 41 | + * <li>{@link Appender},</li> |
| 42 | + * <li>{@link BaseAppender}</li> |
| 43 | + * </ul> |
| 44 | + */ |
| 45 | +@Plugin |
| 46 | +@Namespace("namespace") |
| 47 | +public final class MyAppender extends AbstractAppender implements Appender { |
| 48 | + |
| 49 | + /** |
| 50 | + * Parent builder with some private fields that are not returned by |
| 51 | + * {@link javax.lang.model.util.Elements#getAllMembers(TypeElement)}. |
| 52 | + */ |
| 53 | + public static class ParentBuilder { |
| 54 | + |
| 55 | + /** |
| 56 | + * A {@code char} attribute. |
| 57 | + */ |
| 58 | + @PluginBuilderAttribute |
| 59 | + private char charAtt = 'L'; |
| 60 | + |
| 61 | + /** |
| 62 | + * An {@code int} attribute. |
| 63 | + */ |
| 64 | + @PluginBuilderAttribute |
| 65 | + private int intAtt = 4242; |
| 66 | + |
| 67 | + /** |
| 68 | + * An element with multiplicity 1. |
| 69 | + */ |
| 70 | + @PluginElement |
| 71 | + private Layout layout; |
| 72 | + } |
| 73 | + |
| 74 | + public static final class Builder extends ParentBuilder |
| 75 | + implements org.apache.logging.log4j.plugins.util.Builder<MyAppender> { |
| 76 | + |
| 77 | + /** |
| 78 | + * A {@code short} attribute annotated on type. |
| 79 | + */ |
| 80 | + private @PluginBuilderAttribute short shortAtt = 42; |
| 81 | + |
| 82 | + /** |
| 83 | + * A {@code long} attribute annotated on type. |
| 84 | + */ |
| 85 | + private @PluginBuilderAttribute long longAtt = 424242L; |
| 86 | + |
| 87 | + /** |
| 88 | + * A {@code String} attribute. |
| 89 | + */ |
| 90 | + @PluginBuilderAttribute |
| 91 | + private String stringAtt; |
| 92 | + |
| 93 | + /** |
| 94 | + * An attribute whose name differs from the field name. |
| 95 | + */ |
| 96 | + @PluginBuilderAttribute("anotherName") |
| 97 | + private String origName; |
| 98 | + |
| 99 | + /** |
| 100 | + * An attribute that is an enumeration annotated on type. |
| 101 | + */ |
| 102 | + private @PluginBuilderAttribute MyEnum enumAtt; |
| 103 | + |
| 104 | + /** |
| 105 | + * An attribute of type {@code float}. |
| 106 | + */ |
| 107 | + private @PluginBuilderAttribute float floatAtt; |
| 108 | + |
| 109 | + /** |
| 110 | + * An attribute of type {@code double}. |
| 111 | + */ |
| 112 | + private @PluginBuilderAttribute double aDouble; |
| 113 | + |
| 114 | + private Object notAnAttribute; |
| 115 | + |
| 116 | + /** |
| 117 | + * A collection element. |
| 118 | + */ |
| 119 | + @PluginElement |
| 120 | + private List<Appender2> appenderList; |
| 121 | + |
| 122 | + /** |
| 123 | + * A set of layouts |
| 124 | + */ |
| 125 | + @PluginElement |
| 126 | + private LayoutSet layoutSet; |
| 127 | + |
| 128 | + /** |
| 129 | + * A {@code boolean} attribute with annotated type. |
| 130 | + */ |
| 131 | + public Builder setBooleanAtt(final @PluginBuilderAttribute boolean booleanAtt) { |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * A {@code byte} attribute with annotated parameter. |
| 137 | + */ |
| 138 | + public Builder setByteAtt(@PluginBuilderAttribute final byte byteAtt) { |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * An element with multiplicity n with annotated setter. |
| 144 | + */ |
| 145 | + @PluginElement |
| 146 | + public Builder setFilters(final Filter[] filters) { |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * An element that is not an interface with annotated parameter. |
| 152 | + */ |
| 153 | + public Builder setAbstractElement(@PluginElement final AbstractAppender abstractAppender) { |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * An element with an annotated type. |
| 159 | + */ |
| 160 | + public Builder setNestedAppender(final @PluginElement Appender nestedAppender) { |
| 161 | + return this; |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * A setter with a varargs type. |
| 166 | + */ |
| 167 | + public Builder setVarargs(@PluginElement final Layout3... layouts) { |
| 168 | + return this; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public MyAppender build() { |
| 173 | + return null; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + @PluginFactory |
| 178 | + public static Builder newBuilder() { |
| 179 | + return new Builder(); |
| 180 | + } |
| 181 | + |
| 182 | + public static interface Appender2 {} |
| 183 | + |
| 184 | + public static interface Layout2 {} |
| 185 | + |
| 186 | + public static interface Layout3 {} |
| 187 | + |
| 188 | + public abstract static class LayoutSet implements Set<Layout2> {} |
| 189 | +} |
0 commit comments