com.sun.tools.javac.tree.JCTree.JCArrayTypeTree Java Examples

The following examples show how to use com.sun.tools.javac.tree.JCTree.JCArrayTypeTree. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TreeFinder.java    From annotation-tools with MIT License 6 votes vote down vote up
@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);
}
 
Example #2
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #3
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #4
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
public void visitTypeArray(JCArrayTypeTree tree) {
	try {
		printBaseElementType(tree);
		printBrackets(tree);
	} catch (IOException e) {
		throw new UncheckedIOException(e);
	}
}
 
Example #5
Source File: PrettyCommentsPrinter.java    From EasyMPermission with MIT License 5 votes vote down vote up
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);
}
 
Example #6
Source File: TreeFinder.java    From annotation-tools with MIT License 5 votes vote down vote up
private JCTree arrayContentType(JCArrayTypeTree att) {
  JCTree node = att;
  do {
    node = ((JCArrayTypeTree) node).getType();
  } while (node.getKind() == Tree.Kind.ARRAY_TYPE);
  return node;
}
 
Example #7
Source File: CRTable.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitTypeArray(JCArrayTypeTree tree) {
    SourceRange sr = new SourceRange(startPos(tree), endPos(tree));
    sr.mergeWith(csp(tree.elemtype));
    result = sr;
}
 
Example #8
Source File: TransTypes.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public void visitTypeArray(JCArrayTypeTree tree) {
    tree.elemtype = translate(tree.elemtype, null);
    tree.type = erasure(tree.type);
    result = tree;
}
 
Example #9
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void visitTypeArray(JCArrayTypeTree tree) {
    validateTree(tree.elemtype, checkRaw, isOuter);
}
 
Example #10
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void visitTypeArray(JCArrayTypeTree tree) {
    scan(tree.elemtype);
}
 
Example #11
Source File: JavacTreeMaker.java    From EasyMPermission with MIT License 4 votes vote down vote up
public JCArrayTypeTree TypeArray(JCExpression elemtype) {
	return invoke(TypeArray, elemtype);
}
 
Example #12
Source File: HandleToString.java    From EasyMPermission with MIT License 4 votes vote down vote up
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());
}
 
Example #13
Source File: JavacHandlerUtil.java    From EasyMPermission with MIT License 4 votes vote down vote up
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;
}
 
Example #14
Source File: UArrayTypeTree.java    From Refaster with Apache License 2.0 4 votes vote down vote up
@Override
public JCArrayTypeTree inline(Inliner inliner) throws CouldNotResolveImportException {
  return inliner.maker().TypeArray(getType().inline(inliner));
}