private Node toVariableDefinition(java.util.List<JCVariableDecl> decls, Map<FlagKey, Object> keys) {
boolean createDeclaration = !keys.containsKey(FlagKey.VARDEF_IS_DEFINITION);
if (decls == null || decls.isEmpty()) {
VariableDefinition def = new VariableDefinition();
return createDeclaration ? new VariableDeclaration().astDefinition(def) : def;
}
JCVariableDecl first = decls.get(0);
int startPosFirst = first.pos;
JCExpression baseType = first.vartype;
while (baseType instanceof JCArrayTypeTree) {
// if written as int[] a[], b; then the base Type is *NOT* a's type, but a's type with any number of JCATT's dewrapped.
// The only way to tell the difference is by checking positions, unfortunately: If end pos of type is after start pos of decl, it's a split.
int endPosType = baseType.getEndPosition(endPosTable);
if (endPosType != -1 && startPosFirst != -1 && endPosType > startPosFirst) {
baseType = ((JCArrayTypeTree) baseType).elemtype;
} else {
break;
}
}
VariableDefinition def = new VariableDefinition();
def.astModifiers((Modifiers) toTree(first.mods));
setPos(decls.get(decls.size()-1), def);
if (decls.size() > 1) def.setPosition(new Position(startPosFirst, def.getPosition().getEnd()));
int baseDims = countDims(baseType);
if ((first.mods.flags & Flags.VARARGS) != 0) {
def.astVarargs(true);
setConversionPositionInfo(def, "...", getPosition(baseType));
if (baseType instanceof JCArrayTypeTree) baseType = ((JCArrayTypeTree) baseType).elemtype;
}
def.rawTypeReference(toTree(baseType, FlagKey.TYPE_REFERENCE));
def.astVarargs((first.mods.flags & Flags.VARARGS) != 0);
for (JCVariableDecl varDecl : decls) {
int extraDims = countDims(varDecl.vartype) - baseDims;
VariableDefinitionEntry entry = new VariableDefinitionEntry();
entry.astArrayDimensions(extraDims);
entry.astName(setPos(varDecl, new Identifier().astValue(varDecl.name.toString())));
entry.rawInitializer(toTree(varDecl.init));
setPos(varDecl, entry);
if (extraDims > 0) {
JCArrayTypeTree arrayType = (JCArrayTypeTree) varDecl.vartype;
for (int i = 0; i < extraDims; i++) {
if (arrayType != null) setConversionPositionInfo(entry, "[]" + (extraDims - i - 1), getPosition(arrayType));
arrayType = arrayType.elemtype instanceof JCArrayTypeTree ? (JCArrayTypeTree) arrayType.elemtype : null;
}
}
def.astVariables().addToEnd(entry);
}
if (createDeclaration) {
VariableDeclaration decl = new VariableDeclaration().astDefinition(def);
decl.setPosition(def.getPosition());
addJavadoc(decl, first.mods);
return decl;
}
return def;