use of java.util.Iterator in project groovy by apache.
the class GroovyPosixParser method flatten.
/**
* An implementation of {@link Parser}'s abstract
* {@link Parser#flatten(Options,String[],boolean) flatten} method.
* <p>
* The following are the rules used by this flatten method.
* <ol>
* <li>if <code>stopAtNonOption</code> is <b>true</b> then do not
* burst anymore of <code>arguments</code> entries, just add each
* successive entry without further processing. Otherwise, ignore
* <code>stopAtNonOption</code>.</li>
* <li>if the current <code>arguments</code> entry is "<b>--</b>"
* just add the entry to the list of processed tokens</li>
* <li>if the current <code>arguments</code> entry is "<b>-</b>"
* just add the entry to the list of processed tokens</li>
* <li>if the current <code>arguments</code> entry is two characters
* in length and the first character is "<b>-</b>" then check if this
* is a valid {@link Option} id. If it is a valid id, then add the
* entry to the list of processed tokens and set the current {@link Option}
* member. If it is not a valid id and <code>stopAtNonOption</code>
* is true, then the remaining entries are copied to the list of
* processed tokens. Otherwise, the current entry is ignored.</li>
* <li>if the current <code>arguments</code> entry is more than two
* characters in length and the first character is "<b>-</b>" then
* we need to burst the entry to determine its constituents. For more
* information on the bursting algorithm see
* {@link GroovyPosixParser#burstToken(String, boolean) burstToken}.</li>
* <li>if the current <code>arguments</code> entry is not handled
* by any of the previous rules, then the entry is added to the list
* of processed tokens.</li>
* </ol>
*
* @param options The command line {@link Options}
* @param arguments The command line arguments to be parsed
* @param stopAtNonOption Specifies whether to stop flattening when an non option is found.
* @return The flattened <code>arguments</code> String array.
*/
protected String[] flatten(Options options, String[] arguments, boolean stopAtNonOption) {
init();
this.options = options;
// an iterator for the command line tokens
Iterator iter = Arrays.asList(arguments).iterator();
// process each command line token
while (iter.hasNext()) {
// get the next command line token
String token = (String) iter.next();
// handle long option --foo or --foo=bar
if (token.startsWith("--")) {
int pos = token.indexOf('=');
// --foo
String opt = pos == -1 ? token : token.substring(0, pos);
if (!options.hasOption(opt)) {
processNonOptionToken(token, stopAtNonOption);
} else {
tokens.add(opt);
if (pos != -1) {
tokens.add(token.substring(pos + 1));
} else {
currentOption = options.getOption(opt);
}
}
} else // single hyphen
if ("-".equals(token)) {
tokens.add(token);
} else if (token.startsWith("-")) {
if (token.length() == 2 || options.hasOption(token)) {
processOptionToken(token, stopAtNonOption);
} else // requires bursting
{
burstToken(token, stopAtNonOption);
}
} else {
processNonOptionToken(token, stopAtNonOption);
}
gobble(iter);
}
return (String[]) tokens.toArray(new String[tokens.size()]);
}
use of java.util.Iterator in project groovy by apache.
the class ErrorCollector method write.
//---------------------------------------------------------------------------
// OUTPUT
private void write(PrintWriter writer, Janitor janitor, List messages, String txt) {
if (messages == null || messages.isEmpty())
return;
Iterator iterator = messages.iterator();
while (iterator.hasNext()) {
Message message = (Message) iterator.next();
message.write(writer, janitor);
if (configuration.getDebug() && (message instanceof SyntaxErrorMessage)) {
SyntaxErrorMessage sem = (SyntaxErrorMessage) message;
sem.getCause().printStackTrace(writer);
}
writer.println();
}
writer.print(messages.size());
writer.print(" " + txt);
if (messages.size() > 1)
writer.print("s");
writer.println();
}
use of java.util.Iterator in project groovy by apache.
the class Janitor method cleanup.
public void cleanup() {
Iterator iterator = pending.iterator();
while (iterator.hasNext()) {
HasCleanup object = (HasCleanup) iterator.next();
try {
object.cleanup();
} catch (Exception e) {
// Ignore
}
}
pending.clear();
}
use of java.util.Iterator in project groovy by apache.
the class TupleListTest method testIterateOverTuple.
public void testIterateOverTuple() throws Exception {
StringBuilder buffer = new StringBuilder();
for (Iterator iter = InvokerHelper.asIterator(InvokerHelper.createTuple(new Object[] { "a", "b", "c" })); iter.hasNext(); ) {
Object i = iter.next();
buffer.append(i);
}
assertEquals("buffer", "abc", buffer.toString());
}
use of java.util.Iterator in project groovy by apache.
the class InnerClassVisitor method visitConstructorCallExpression.
@Override
public void visitConstructorCallExpression(ConstructorCallExpression call) {
super.visitConstructorCallExpression(call);
if (!call.isUsingAnonymousInnerClass()) {
passThisReference(call);
return;
}
InnerClassNode innerClass = (InnerClassNode) call.getType();
ClassNode outerClass = innerClass.getOuterClass();
ClassNode superClass = innerClass.getSuperClass();
if (superClass instanceof InnerClassNode && !superClass.isInterface() && !(superClass.isStaticClass() || ((superClass.getModifiers() & ACC_STATIC) == ACC_STATIC))) {
insertThis0ToSuperCall(call, innerClass);
}
if (!innerClass.getDeclaredConstructors().isEmpty())
return;
if ((innerClass.getModifiers() & ACC_STATIC) != 0)
return;
VariableScope scope = innerClass.getVariableScope();
if (scope == null)
return;
// expressions = constructor call arguments
List<Expression> expressions = ((TupleExpression) call.getArguments()).getExpressions();
// block = init code for the constructor we produce
BlockStatement block = new BlockStatement();
// parameters = parameters of the constructor
final int additionalParamCount = 1 + scope.getReferencedLocalVariablesCount();
List<Parameter> parameters = new ArrayList<Parameter>(expressions.size() + additionalParamCount);
// superCallArguments = arguments for the super call == the constructor call arguments
List<Expression> superCallArguments = new ArrayList<Expression>(expressions.size());
// first we add a super() call for all expressions given in the
// constructor call expression
int pCount = additionalParamCount;
for (Expression expr : expressions) {
pCount++;
// add one parameter for each expression in the
// constructor call
Parameter param = new Parameter(ClassHelper.OBJECT_TYPE, "p" + pCount);
parameters.add(param);
// add to super call
superCallArguments.add(new VariableExpression(param));
}
// add the super call
ConstructorCallExpression cce = new ConstructorCallExpression(ClassNode.SUPER, new TupleExpression(superCallArguments));
block.addStatement(new ExpressionStatement(cce));
// we need to add "this" to access unknown methods/properties
// this is saved in a field named this$0
pCount = 0;
expressions.add(pCount, VariableExpression.THIS_EXPRESSION);
boolean isStatic = isStaticThis(innerClass, scope);
ClassNode outerClassType = getClassNode(outerClass, isStatic);
if (!isStatic && inClosure)
outerClassType = ClassHelper.CLOSURE_TYPE;
outerClassType = outerClassType.getPlainNodeReference();
Parameter thisParameter = new Parameter(outerClassType, "p" + pCount);
parameters.add(pCount, thisParameter);
thisField = innerClass.addField("this$0", PUBLIC_SYNTHETIC, outerClassType, null);
addFieldInit(thisParameter, thisField, block);
// for each shared variable we add a reference and save it as field
for (Iterator it = scope.getReferencedLocalVariablesIterator(); it.hasNext(); ) {
pCount++;
org.codehaus.groovy.ast.Variable var = (org.codehaus.groovy.ast.Variable) it.next();
VariableExpression ve = new VariableExpression(var);
ve.setClosureSharedVariable(true);
ve.setUseReferenceDirectly(true);
expressions.add(pCount, ve);
ClassNode rawReferenceType = ClassHelper.REFERENCE_TYPE.getPlainNodeReference();
Parameter p = new Parameter(rawReferenceType, "p" + pCount);
parameters.add(pCount, p);
p.setOriginType(var.getOriginType());
final VariableExpression initial = new VariableExpression(p);
initial.setSynthetic(true);
initial.setUseReferenceDirectly(true);
final FieldNode pField = innerClass.addFieldFirst(ve.getName(), PUBLIC_SYNTHETIC, rawReferenceType, initial);
pField.setHolder(true);
pField.setOriginType(ClassHelper.getWrapper(var.getOriginType()));
}
innerClass.addConstructor(ACC_SYNTHETIC, parameters.toArray(new Parameter[parameters.size()]), ClassNode.EMPTY_ARRAY, block);
}
Aggregations