MCPcopy Index your code
hub / github.com/antlr/codebuff / add

Method add

output/java8/1.4.13/ST.java:217–288  ·  view source on GitHub ↗

Inject an attribute (name/value pair). If there is already an attribute with that name, this method turns the attribute into an AttributeList with both the previous and the new attribute as elements. This method will never alter a List that you inject. If you send in a {@link Lis

(String name, Object value)

Source from the content-addressed store, hash-verified

215 */
216
217 public synchronized ST add(String name, Object value) {
218 if ( name==null ) {
219 throw new NullPointerException("null attribute name");
220 }
221
222 if ( name.indexOf('.')>=0 ) {
223 throw new IllegalArgumentException("cannot have '.' in attribute names");
224 }
225
226 if ( STGroup.trackCreationEvents ) {
227 if ( debugState==null ) debugState = new ST. DebugState();
228 debugState.addAttrEvents.map(name, new AddAttributeEvent(name, value));
229 }
230
231 FormalArgument arg = null;
232 if ( impl.hasFormalArgs ) {
233 if ( impl.formalArguments!=null ) arg = impl.formalArguments.get(name);
234 if ( arg==null ) {
235 throw new IllegalArgumentException("no such attribute: "+name);
236 }
237 }
238 else {
239 // define and make room in locals (a hack to make new ST("simple template") work.)
240 if ( impl.formalArguments!=null ) {
241 arg = impl.formalArguments.get(name);
242 }
243
244 if ( arg==null ) { // not defined
245 arg = new FormalArgument(name);
246 impl.addArg(arg);
247 if ( locals==null ) locals = new Object[1];
248 //else locals = Arrays.copyOf(locals, impl.formalArguments.size());
249 else {
250 Object[] copy = new Object[impl.formalArguments.size()];
251 System.arraycopy(locals, 0, copy, 0, Math.min(locals.length, impl.formalArguments.size()));
252 locals = copy;
253 }
254 locals[arg.index] = EMPTY_ATTR;
255 }
256 }
257
258 Object curvalue = locals[arg.index];
259 if ( curvalue==EMPTY_ATTR ) { // new attribute
260 locals[arg.index] = value;
261 return this;
262 }
263
264 // attribute will be multi-valued for sure now
265 // convert current attribute to list if not already
266 // copy-on-write semantics; copy a list injected by user to add new value
267
268 AttributeList multi = convertToAttributeList(curvalue);
269 locals[arg.index] = multi; // replace with list
270
271 // now, add incoming value to multi-valued attribute
272 if ( value instanceof List ) {
273 // flatten incoming list into existing list
274 multi.addAll((List<?>)value);

Callers 4

test1Method · 0.95
test4Method · 0.95
addAggrMethod · 0.95
formatMethod · 0.95

Calls 11

getMethod · 0.65
sizeMethod · 0.65
addAllMethod · 0.65
addMethod · 0.65
indexOfMethod · 0.45
mapMethod · 0.45
addArgMethod · 0.45
minMethod · 0.45
isArrayMethod · 0.45
asListMethod · 0.45

Tested by

no test coverage detected