@Override public Pair<ASTRecord, Integer> visitArrayType(ArrayTypeTree node, Insertion ins) { dbug.debug("TypePositionFinder.visitArrayType(%s)%n", node); JCArrayTypeTree att = (JCArrayTypeTree) node; dbug.debug("TypePositionFinder.visitArrayType(%s) preferred = %s%n", node, att.getPreferredPosition()); // If the code has a type like "String[][][]", then this gets called // three times: for String[][][], String[][], and String[] // respectively. For each of the three, call String[][][] "largest". ArrayTypeTree largest = largestContainingArray(node); int largestLevels = arrayLevels(largest); int levels = arrayLevels(node); int start = arrayContentType(att).getPreferredPosition() + 1; int end = att.getEndPosition(tree.endPositions); int pos = arrayInsertPos(start, end); dbug.debug(" levels=%d largestLevels=%d%n", levels, largestLevels); for (int i=levels; i<largestLevels; i++) { pos = getFirstInstanceAfter('[', pos+1); dbug.debug(" pos %d at i=%d%n", pos, i); } return Pair.of(astRecord(node), pos); }
private int dimension(JCTree t, int afterPos) { if (t.getKind() != Kind.ARRAY_TYPE) { return 0; } int add; if (afterPos >= 0) { final int[] bounds = getBounds(t); add = afterPos < bounds[1] ? 1 : 0; } else { add = 1; } return add + dimension (((JCTree.JCArrayTypeTree)t).getType(), afterPos); }
protected int diffTypeArray(JCArrayTypeTree oldT, JCArrayTypeTree newT, int[] bounds) { int localPointer = bounds[0]; int[] elemtypeBounds = getBounds(oldT.elemtype); localPointer = diffTree(oldT.elemtype, newT.elemtype, elemtypeBounds); copyTo(localPointer, bounds[1]); return bounds[1]; }
private int dims(JCExpression vartype) { if (vartype instanceof JCArrayTypeTree) { return 1 + dims(((JCArrayTypeTree) vartype).elemtype); } return 0; }
private void printVarDef0(JCVariableDecl tree) { boolean varargs = (tree.mods.flags & VARARGS) != 0; if (varargs && tree.vartype instanceof JCArrayTypeTree) { print(((JCArrayTypeTree) tree.vartype).elemtype); print("..."); } else { print(tree.vartype); } print(" "); print(tree.name); if (tree.init != null) { print(" = "); print(tree.init); } }
@Override public void visitNewArray(JCNewArray tree) { JCTree elem = tree.elemtype; int dims = 0; if (elem != null) { print("new "); while (elem instanceof JCArrayTypeTree) { dims++; elem = ((JCArrayTypeTree) elem).elemtype; } print(elem); for (JCExpression expr : tree.dims) { print("["); print(expr); print("]"); } } for (int i = 0; i < dims; i++) print("[]"); if (tree.elems != null) { if (elem != null) print("[] "); print("{"); print(tree.elems, ", "); print("}"); } }
public void visitTypeArray(JCArrayTypeTree that) { try { print("JCArrayTypeTree:"); } catch (Exception e) { } super.visitTypeArray(that); }
public void visitVarDef(JCVariableDecl tree) { try { boolean suppressSemi = suppressFinalAndSemicolonsInTry; if (getJavadocFor(tree) != null) { println(); align(); } printDocComment(tree); if ((tree.mods.flags & ENUM) != 0) { printEnumMember(tree); } else { printExpr(tree.mods); if ((tree.mods.flags & VARARGS) != 0) { printExpr(((JCArrayTypeTree) tree.vartype).elemtype); print("... " + tree.name); } else { printExpr(tree.vartype); print(" " + tree.name); } if (tree.init != null) { print(" = "); printExpr(tree.init); } if (prec == TreeInfo.notExpression && !suppressSemi) print(";"); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitNewArray(JCNewArray tree) { try { if (tree.elemtype != null) { print("new "); JCTree elem = tree.elemtype; if (elem instanceof JCArrayTypeTree) printBaseElementType((JCArrayTypeTree) elem); else printExpr(elem); for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { print("["); printExpr(l.head); print("]"); } if (elem instanceof JCArrayTypeTree) printBrackets((JCArrayTypeTree) elem); } if (tree.elems != null) { if (tree.elemtype != null) print("[]"); print("{"); printExprs(tree.elems); print("}"); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public void visitTypeArray(JCArrayTypeTree tree) { try { printBaseElementType(tree); printBrackets(tree); } catch (IOException e) { throw new UncheckedIOException(e); } }
private void printBaseElementType(JCArrayTypeTree tree) throws IOException { JCTree elem = tree.elemtype; while (elem instanceof JCWildcard) elem = ((JCWildcard) elem).inner; if (elem instanceof JCArrayTypeTree) printBaseElementType((JCArrayTypeTree) elem); else printExpr(elem); }
private JCTree arrayContentType(JCArrayTypeTree att) { JCTree node = att; do { node = ((JCArrayTypeTree) node).getType(); } while (node.getKind() == Tree.Kind.ARRAY_TYPE); return node; }
private static int countDims(JCExpression type) { int dims = 0; while (type instanceof JCArrayTypeTree) { type = ((JCArrayTypeTree) type).elemtype; dims++; } return dims; }
@Override public void visitTypeArray(JCArrayTypeTree node) { TypeReference ref = (TypeReference) toTree(node.getType(), FlagKey.TYPE_REFERENCE); int currentDim = ref.astArrayDimensions(); ref.astArrayDimensions(currentDim + 1); setConversionPositionInfo(ref, "[]" + currentDim, getPosition(node)); set(node, ref); }
public void visitNewArray(JCNewArray tree) { try { if (tree.elemtype != null) { print("new "); JCTree elem = tree.elemtype; if (elem.getTag() == JCTree.TYPEARRAY) printBaseElementType((JCArrayTypeTree) elem); else printExpr(elem); for (List<JCExpression> l = tree.dims; l.nonEmpty(); l = l.tail) { print("["); printExpr(l.head); print("]"); } if (elem instanceof JCArrayTypeTree) printBrackets((JCArrayTypeTree) elem); } if (tree.elems != null) { if (tree.elemtype != null) print("[]"); print("{"); printExprs(tree.elems); print("}"); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public static Matcher<MethodTree> methodReturns(final Type returnType) { return new Matcher<MethodTree>() { @Override public boolean matches(MethodTree methodTree, VisitorState state) { Tree returnTree = methodTree.getReturnType(); Type methodReturnType = null; if (returnTree == null) { // This is a constructor, it has no return type. return false; } switch (returnTree.getKind()) { case ARRAY_TYPE: methodReturnType = ((JCArrayTypeTree)returnTree).type; break; case PRIMITIVE_TYPE: methodReturnType = ((JCPrimitiveTypeTree)returnTree).type; break; case PARAMETERIZED_TYPE: methodReturnType = ((JCTypeApply)returnTree).type; break; default: return false; } return state.getTypes().isSameType(methodReturnType, returnType); } }; }
public void visitVarDef(JCVariableDecl tree) { try { if (docComments != null && docComments.get(tree) != null) { println(); align(); } printDocComment(tree); if ((tree.mods.flags & ENUM) != 0) { printEnumMember(tree); } else { printExpr(tree.mods); if ((tree.mods.flags & VARARGS) != 0) { printExpr(((JCArrayTypeTree) tree.vartype).elemtype); print("... " + tree.name); } else { printExpr(tree.vartype); print(" " + tree.name); } if (tree.init != null) { print(" = "); printExpr(tree.init); } if (prec == TreeInfo.notExpression) print(";"); } } catch (IOException e) { throw new UncheckedIOException(e); } }
public JCArrayTypeTree TypeArray(JCExpression elemtype) { return invoke(TypeArray, elemtype); }
@Override public void visitTypeArray(JCArrayTypeTree tree) { JCTree elem = tree.elemtype; while (elem instanceof JCWildcard) elem = ((JCWildcard) elem).inner; print(elem); print("[]"); }
static JCMethodDecl createToString(JavacNode typeNode, Collection<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) { JavacTreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(genJavaLangTypeRef(typeNode, "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); JCExpression returnType = genJavaLangTypeRef(typeNode, "String"); boolean first = true; String typeName = getTypeName(typeNode); String infix = ", "; String suffix = ")"; String prefix; if (callSuper) { prefix = typeName + "(super="; } else if (fields.isEmpty()) { prefix = typeName + "()"; } else if (includeFieldNames) { prefix = typeName + "(" + ((JCVariableDecl)fields.iterator().next().get()).name.toString() + "="; } else { prefix = typeName + "("; } JCExpression current = maker.Literal(prefix); if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil()); current = maker.Binary(CTC_PLUS, current, callToSuper); first = false; } for (JavacNode fieldNode : fields) { JCExpression expr; JCExpression fieldAccessor = createFieldAccessor(maker, fieldNode, fieldAccess); JCExpression fieldType = getFieldType(fieldNode, fieldAccess); // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option. boolean fieldIsPrimitive = fieldType instanceof JCPrimitiveTypeTree; boolean fieldIsPrimitiveArray = fieldType instanceof JCArrayTypeTree && ((JCArrayTypeTree) fieldType).elemtype instanceof JCPrimitiveTypeTree; boolean fieldIsObjectArray = !fieldIsPrimitiveArray && fieldType instanceof JCArrayTypeTree; @SuppressWarnings("unused") boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray; if (fieldIsPrimitiveArray || fieldIsObjectArray) { JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString"); expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(fieldAccessor)); } else expr = fieldAccessor; if (first) { current = maker.Binary(CTC_PLUS, current, expr); first = false; continue; } if (includeFieldNames) { current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + fieldNode.getName() + "=")); } else { current = maker.Binary(CTC_PLUS, current, maker.Literal(infix)); } current = maker.Binary(CTC_PLUS, current, expr); } if (!first) current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix)); JCStatement returnStatement = maker.Return(current); JCBlock body = maker.Block(0, List.of(returnStatement)); return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("toString"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source, typeNode.getContext()); }
private static JCExpression cloneType0(JavacTreeMaker maker, JCTree in) { if (in == null) return null; if (in instanceof JCPrimitiveTypeTree) return (JCExpression) in; if (in instanceof JCIdent) { return maker.Ident(((JCIdent) in).name); } if (in instanceof JCFieldAccess) { JCFieldAccess fa = (JCFieldAccess) in; return maker.Select(cloneType0(maker, fa.selected), fa.name); } if (in instanceof JCArrayTypeTree) { JCArrayTypeTree att = (JCArrayTypeTree) in; return maker.TypeArray(cloneType0(maker, att.elemtype)); } if (in instanceof JCTypeApply) { JCTypeApply ta = (JCTypeApply) in; ListBuffer<JCExpression> lb = new ListBuffer<JCExpression>(); for (JCExpression typeArg : ta.arguments) { lb.append(cloneType0(maker, typeArg)); } return maker.TypeApply(cloneType0(maker, ta.clazz), lb.toList()); } if (in instanceof JCWildcard) { JCWildcard w = (JCWildcard) in; JCExpression newInner = cloneType0(maker, w.inner); TypeBoundKind newKind; switch (w.getKind()) { case SUPER_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.SUPER); break; case EXTENDS_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.EXTENDS); break; default: case UNBOUNDED_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.UNBOUND); break; } return maker.Wildcard(newKind, newInner); } // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline. return (JCExpression) in; }
public AJCArrayTypeTree(JCArrayTypeTree ltree) { super(ltree.elemtype); }
public AJCArrayTypeTree(JCArrayTypeTree ltree, String lcomment) { this(ltree); setComment(lcomment); }
@Override public JCArrayTypeTree inline(Inliner inliner) throws CouldNotResolveImportException { return inliner.maker().TypeArray(getType().inline(inliner)); }
@Override public void visitTypeArray(JCArrayTypeTree tree) { printNode(tree); child("elemtype", tree.elemtype); indent--; }
private JCArrayTypeTree bracketsOptCont(JCExpression t, int pos) { accept(RBRACKET); t = bracketsOpt(t); return toP(F.at(pos).TypeArray(t)); }
public void visitTypeArray(JCArrayTypeTree tree) { Type etype = attribType(tree.elemtype, env); Type type = new ArrayType(etype, syms.arrayClass); result = check(tree, type, TYP, pkind, pt); }
private JCMethodDecl createToString(JavacNode typeNode, List<JavacNode> fields, boolean includeFieldNames, boolean callSuper, FieldAccess fieldAccess, JCTree source) { TreeMaker maker = typeNode.getTreeMaker(); JCAnnotation overrideAnnotation = maker.Annotation(chainDots(typeNode, "java", "lang", "Override"), List.<JCExpression>nil()); JCModifiers mods = maker.Modifiers(Flags.PUBLIC, List.of(overrideAnnotation)); JCExpression returnType = chainDots(typeNode, "java", "lang", "String"); boolean first = true; String typeName = getTypeName(typeNode); String infix = ", "; String suffix = ")"; String prefix; if (callSuper) { prefix = typeName + "(super="; } else if (fields.isEmpty()) { prefix = typeName + "()"; } else if (includeFieldNames) { prefix = typeName + "(" + ((JCVariableDecl)fields.iterator().next().get()).name.toString() + "="; } else { prefix = typeName + "("; } JCExpression current = maker.Literal(prefix); if (callSuper) { JCMethodInvocation callToSuper = maker.Apply(List.<JCExpression>nil(), maker.Select(maker.Ident(typeNode.toName("super")), typeNode.toName("toString")), List.<JCExpression>nil()); current = maker.Binary(CTC_PLUS, current, callToSuper); first = false; } for (JavacNode fieldNode : fields) { JCExpression expr; JCExpression fieldAccessor = createFieldAccessor(maker, fieldNode, fieldAccess); JCExpression fieldType = getFieldType(fieldNode, fieldAccess); // The distinction between primitive and object will be useful if we ever add a 'hideNulls' option. boolean fieldIsPrimitive = fieldType instanceof JCPrimitiveTypeTree; boolean fieldIsPrimitiveArray = fieldType instanceof JCArrayTypeTree && ((JCArrayTypeTree) fieldType).elemtype instanceof JCPrimitiveTypeTree; boolean fieldIsObjectArray = !fieldIsPrimitiveArray && fieldType instanceof JCArrayTypeTree; @SuppressWarnings("unused") boolean fieldIsObject = !fieldIsPrimitive && !fieldIsPrimitiveArray && !fieldIsObjectArray; if (fieldIsPrimitiveArray || fieldIsObjectArray) { JCExpression tsMethod = chainDots(typeNode, "java", "util", "Arrays", fieldIsObjectArray ? "deepToString" : "toString"); expr = maker.Apply(List.<JCExpression>nil(), tsMethod, List.<JCExpression>of(fieldAccessor)); } else expr = fieldAccessor; if (first) { current = maker.Binary(CTC_PLUS, current, expr); first = false; continue; } if (includeFieldNames) { current = maker.Binary(CTC_PLUS, current, maker.Literal(infix + fieldNode.getName() + "=")); } else { current = maker.Binary(CTC_PLUS, current, maker.Literal(infix)); } current = maker.Binary(CTC_PLUS, current, expr); } if (!first) current = maker.Binary(CTC_PLUS, current, maker.Literal(suffix)); JCStatement returnStatement = maker.Return(current); JCBlock body = maker.Block(0, List.of(returnStatement)); return recursiveSetGeneratedBy(maker.MethodDef(mods, typeNode.toName("toString"), returnType, List.<JCTypeParameter>nil(), List.<JCVariableDecl>nil(), List.<JCExpression>nil(), body, null), source); }
private static JCExpression cloneType0(TreeMaker maker, JCTree in) { if (in == null) return null; if (in instanceof JCPrimitiveTypeTree) return (JCExpression) in; if (in instanceof JCIdent) { return maker.Ident(((JCIdent) in).name); } if (in instanceof JCFieldAccess) { JCFieldAccess fa = (JCFieldAccess) in; return maker.Select(cloneType0(maker, fa.selected), fa.name); } if (in instanceof JCArrayTypeTree) { JCArrayTypeTree att = (JCArrayTypeTree) in; return maker.TypeArray(cloneType0(maker, att.elemtype)); } if (in instanceof JCTypeApply) { JCTypeApply ta = (JCTypeApply) in; ListBuffer<JCExpression> lb = ListBuffer.lb(); for (JCExpression typeArg : ta.arguments) { lb.append(cloneType0(maker, typeArg)); } return maker.TypeApply(cloneType0(maker, ta.clazz), lb.toList()); } if (in instanceof JCWildcard) { JCWildcard w = (JCWildcard) in; JCExpression newInner = cloneType0(maker, w.inner); TypeBoundKind newKind; switch (w.getKind()) { case SUPER_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.SUPER); break; case EXTENDS_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.EXTENDS); break; default: case UNBOUNDED_WILDCARD: newKind = maker.TypeBoundKind(BoundKind.UNBOUND); break; } return maker.Wildcard(newKind, newInner); } // This is somewhat unsafe, but it's better than outright throwing an exception here. Returning null will just cause an exception down the pipeline. return (JCExpression) in; }