private static JCExpression getDefaultExpr(JavacTreeMaker maker, JCExpression type) { if (type instanceof JCPrimitiveTypeTree) { switch (((JCPrimitiveTypeTree) type).getPrimitiveTypeKind()) { case BOOLEAN: return maker.Literal(CTC_BOOLEAN, 0); case CHAR: return maker.Literal(CTC_CHAR, 0); default: case BYTE: case SHORT: case INT: return maker.Literal(CTC_INT, 0); case LONG: return maker.Literal(CTC_LONG, 0L); case FLOAT: return maker.Literal(CTC_FLOAT, 0F); case DOUBLE: return maker.Literal(CTC_DOUBLE, 0D); } } return maker.Literal(CTC_BOT, null); }
public void visitTypeIdent(JCPrimitiveTypeTree tree) { TypeTag typetag = typeTag(tree); try { if (CTC_BYTE.equals(typetag)) print ("byte"); else if (CTC_CHAR.equals(typetag)) print ("char"); else if (CTC_SHORT.equals(typetag)) print ("short"); else if (CTC_INT.equals(typetag)) print ("int"); else if (CTC_LONG.equals(typetag)) print ("long"); else if (CTC_FLOAT.equals(typetag)) print ("float"); else if (CTC_DOUBLE.equals(typetag)) print ("double"); else if (CTC_BOOLEAN.equals(typetag)) print ("boolean"); else if (CTC_VOID.equals(typetag)) print ("void"); else print("error"); } catch (IOException e) { throw new UncheckedIOException(e); } }
@Override public void visit(GenericTypeWrapper w) { JCExpression eouter = generate(w.getOuter()); List<JCExpression> params = generate(w.getTypeParameters(), JCExpression.class); ListBuffer<JCExpression> newList = new ListBuffer<JCExpression>(); for (JCExpression param : params) { JCExpression p2 = param; if (param.getKind() == Kind.PRIMITIVE_TYPE) { p2 = builder.boxPrimitiveType((JCPrimitiveTypeTree) param); } newList.append(p2); } result = builder.getTreeMaker().TypeApply(eouter, newList.toList()); }
protected int diffTypeIdent(JCPrimitiveTypeTree oldT, JCPrimitiveTypeTree newT, int[] bounds) { if (oldT.typetag != newT.typetag) { printer.print(newT); } else { copyTo(bounds[0], bounds[1]); } return bounds[1]; }
@Override public void visitTypeIdent(JCPrimitiveTypeTree tree) { TypeTag typeTag = typeTag(tree); if (CTC_BYTE.equals(typeTag)) print("byte"); else if (CTC_CHAR.equals(typeTag)) print("char"); else if (CTC_SHORT.equals(typeTag)) print("short"); else if (CTC_INT.equals(typeTag)) print("int"); else if (CTC_LONG.equals(typeTag)) print("long"); else if (CTC_FLOAT.equals(typeTag)) print("float"); else if (CTC_DOUBLE.equals(typeTag)) print("double"); else if (CTC_BOOLEAN.equals(typeTag)) print("boolean"); else if (CTC_VOID.equals(typeTag)) print("void"); else print("error"); }
public void visitTypeIdent(JCPrimitiveTypeTree that) { try { print("JCPrimitiveTypeTree:"); } catch (Exception e) { } super.visitTypeIdent(that); }
public void visitTypeIdent(JCPrimitiveTypeTree tree) { try { switch(tree.typetag) { case TypeTags.BYTE: print("byte"); break; case TypeTags.CHAR: print("char"); break; case TypeTags.SHORT: print("short"); break; case TypeTags.INT: print("int"); break; case TypeTags.LONG: print("long"); break; case TypeTags.FLOAT: print("float"); break; case TypeTags.DOUBLE: print("double"); break; case TypeTags.BOOLEAN: print("boolean"); break; case TypeTags.VOID: print("void"); break; default: print("error"); break; } } 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 JCPrimitiveTypeTree TypeIdent(TypeTag typetag) { return invoke(TypeIdent, typetag.value); }
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 AJCPrimitiveTypeTree(JCPrimitiveTypeTree ltree) { super(ltree.typetag); }
public AJCPrimitiveTypeTree(JCPrimitiveTypeTree ltree, String lcomment) { this(ltree); setComment(lcomment); }
@Override public void visitTypeIdent(JCPrimitiveTypeTree tree) { printNode(tree); property("typetag", literalName(tree.typetag)); indent--; }
@Override public UPrimitiveTypeTree visitPrimitiveType(PrimitiveTypeTree tree, Void v) { return UPrimitiveTypeTree.create(((JCPrimitiveTypeTree) tree).typetag); }
/** BasicType = BYTE | SHORT | CHAR | INT | LONG | FLOAT | DOUBLE | BOOLEAN */ JCPrimitiveTypeTree basicType() { JCPrimitiveTypeTree t = to(F.at(S.pos()).TypeIdent(typetag(S.token()))); S.nextToken(); return t; }
public void visitTypeIdent(JCPrimitiveTypeTree tree) { result = check(tree, syms.typeOfTag[tree.typetag], 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; }
public JCExpression boxPrimitiveType(JCPrimitiveTypeTree primitiveType) { Name n = syms.boxedName[primitiveType.typetag]; return maker.Ident(reader.enterClass(n)); }
@Override public void visitTypeIdent(JCPrimitiveTypeTree node) { String primitiveType = JcTreeBuilder.PRIMITIVES.inverse().get(node.typetag); if (primitiveType == null) throw new IllegalArgumentException("Uknown primitive type tag: " + node.typetag); TypeReferencePart part = setPos(node, new TypeReferencePart().astIdentifier(setPos(node, new Identifier().astValue(primitiveType)))); set(node, new TypeReference().astParts().addToEnd(part)); }