MCPcopy Create free account
hub / github.com/antlr/codebuff / add

Method add

output/java/1.4.17/ST.java:216–287  ·  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

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

Callers 9

test1Method · 0.95
test4Method · 0.95
addAggrMethod · 0.95
formatMethod · 0.95
mainMethod · 0.95
testFeaturesMethod · 0.95
mainMethod · 0.95
testFeaturesMethod · 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