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

Method add

corpus/java/training/stringtemplate4/org/stringtemplate/v4/ST.java:209–278  ·  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

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

Callers 15

addAggrMethod · 0.95
formatMethod · 0.95
test1Method · 0.95
test4Method · 0.95
lexerCallCommandMethod · 0.95
getTokenVocabOutputMethod · 0.95
walkMethod · 0.95
getListLabelMethod · 0.95
getImplicitTokenLabelMethod · 0.95
getImplicitSetLabelMethod · 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