本文整理汇总了Java中com.sun.source.tree.NewArrayTree.getDimensions方法的典型用法代码示例。如果您正苦于以下问题:Java NewArrayTree.getDimensions方法的具体用法?Java NewArrayTree.getDimensions怎么用?Java NewArrayTree.getDimensions使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.tree.NewArrayTree
的用法示例。
在下文中一共展示了NewArrayTree.getDimensions方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkZeroSizeArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
private static boolean checkZeroSizeArray(CompilationInfo info, TreePath val) {
if (val.getLeaf().getKind() != Tree.Kind.VARIABLE) {
return false;
}
VariableTree vt = (VariableTree)val.getLeaf();
ExpressionTree xpr = vt.getInitializer();
if (xpr == null) {
return false;
}
if (xpr.getKind() == Tree.Kind.NEW_ARRAY) {
NewArrayTree nat = (NewArrayTree)xpr;
List<? extends ExpressionTree> dims = nat.getDimensions();
if (dims != null && !dims.isEmpty()) {
Object size = ArithmeticUtilities.compute(info,
new TreePath(
new TreePath(val, xpr),
dims.get(dims.size() -1)),
true);
return ArithmeticUtilities.isRealValue(size) && Integer.valueOf(0).equals(size);
} else {
return nat.getInitializers() != null && nat.getInitializers().isEmpty();
}
}
return false;
}
示例2: isNewArrayAllZeroDims
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
/**
* Determine whether all dimensions given in a new array expression have
* zero as length. For example "new Object[0][0];". Also true for empty
* dimensions, as in "new Object[] {...}".
*/
private static boolean isNewArrayAllZeroDims(NewArrayTree node) {
boolean isAllZeros = true;
for (ExpressionTree dim : node.getDimensions()) {
if (dim instanceof LiteralTree) {
Object val = ((LiteralTree) dim).getValue();
if (!(val instanceof Number) || !(new Integer(0).equals(val))) {
isAllZeros = false;
break;
}
} else {
isAllZeros = false;
break;
}
}
return isAllZeros;
}
示例3: computeNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
private static List<? extends TypeMirror> computeNewArray(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
NewArrayTree nat = (NewArrayTree) parent.getLeaf();
if (nat.getType() == error) {
types.add(ElementKind.CLASS);
types.add(ElementKind.ENUM);
types.add(ElementKind.INTERFACE);
return null;
}
for (Tree dimension : nat.getDimensions()) {
if (dimension == error) {
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
}
}
for (Tree init : nat.getInitializers()) {
if (init == error) {
TypeMirror whole = info.getTrees().getTypeMirror(parent);
if (whole == null || whole.getKind() != TypeKind.ARRAY)
return null;
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return Collections.singletonList(((ArrayType) whole).getComponentType());
}
}
return null;
}
示例4: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public List<? extends TypeMirror> visitNewArray(NewArrayTree node, Object p) {
if (node.getDimensions() == null) {
return null;
}
if (theExpression == null && node.getDimensions().size() == 1) {
initExpression(node.getDimensions().get(0));
} else if (!node.getDimensions().contains(theExpression.getLeaf())) {
return null;
}
return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT));
}
示例5: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public Void visitNewArray(NewArrayTree node, Void unused) {
if (node.getType() != null) {
builder.open(plusFour);
token("new");
builder.space();
TypeWithDims extractedDims = DimensionHelpers.extractDims(node.getType(), SortedDims.YES);
Tree base = extractedDims.node;
Deque<ExpressionTree> dimExpressions = new ArrayDeque<>(node.getDimensions());
Deque<List<AnnotationTree>> annotations = new ArrayDeque<>();
// TODO: 22-Jul-17 missing method java8
// annotations.add(ImmutableList.copyOf(node.getAnnotations()));
// annotations.addAll((List<List<AnnotationTree>>) node.getDimAnnotations());
annotations.addAll(extractedDims.dims);
scan(base, null);
builder.open(ZERO);
maybeAddDims(dimExpressions, annotations);
builder.close();
builder.close();
}
if (node.getInitializers() != null) {
if (node.getType() != null) {
builder.space();
}
visitArrayInitializer(node.getInitializers());
}
return null;
}
示例6: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public Node visitNewArray(NewArrayTree tree, Void p) {
// see JLS 15.10
ArrayType type = (ArrayType)InternalUtils.typeOf(tree);
TypeMirror elemType = type.getComponentType();
List<? extends ExpressionTree> dimensions = tree.getDimensions();
List<? extends ExpressionTree> initializers = tree
.getInitializers();
List<Node> dimensionNodes = new ArrayList<Node>();
if (dimensions != null) {
for (ExpressionTree dim : dimensions) {
dimensionNodes.add(unaryNumericPromotion(scan(dim, p)));
}
}
List<Node> initializerNodes = new ArrayList<Node>();
if (initializers != null) {
for (ExpressionTree init : initializers) {
initializerNodes.add(assignConvert(scan(init, p), elemType));
}
}
Node node = new ArrayCreationNode(tree, type, dimensionNodes,
initializerNodes);
return extendWithNode(node);
}
示例7: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public Void visitNewArray(NewArrayTree tree, Void p) {
if (locations) {
System.out.println("new array");
for (@SuppressWarnings("unused") Tree t : tree.getDimensions())
System.out.println("new array dimension");
}
return super.visitNewArray(tree, p);
}
示例8: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public Node visitNewArray(NewArrayTree tree, Void p) {
// see JLS 15.10
ArrayType type = (ArrayType) InternalUtils.typeOf(tree);
TypeMirror elemType = type.getComponentType();
List<? extends ExpressionTree> dimensions = tree.getDimensions();
List<? extends ExpressionTree> initializers = tree.getInitializers();
List<Node> dimensionNodes = new ArrayList<Node>();
if (dimensions != null) {
for (ExpressionTree dim : dimensions) {
dimensionNodes.add(unaryNumericPromotion(scan(dim, p)));
}
}
List<Node> initializerNodes = new ArrayList<Node>();
if (initializers != null) {
for (ExpressionTree init : initializers) {
initializerNodes.add(assignConvert(scan(init, p), elemType));
}
}
Node node = new ArrayCreationNode(tree, type, dimensionNodes, initializerNodes);
return extendWithNode(node);
}
示例9: checkNull
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
private boolean checkNull(List<Tree> path, int ix) {
Tree node = path.get(path.size()-1);
int last = astPath.size() - 1;
ASTPath.ASTEntry entry = astPath.get(ix);
Tree.Kind kind = entry.getTreeKind();
switch (kind) {
// case ANNOTATION:
// case INTERFACE:
case CLASS: // "extends" clause?
return ASTPath.isClassEquiv(kind)
&& ix == last && entry.getArgument() == -1
&& entry.childSelectorIs(ASTPath.BOUND);
case TYPE_PARAMETER:
return node.getKind() == Tree.Kind.TYPE_PARAMETER
&& ix == last && entry.getArgument() == 0
&& entry.childSelectorIs(ASTPath.BOUND);
case METHOD: // nullary constructor? receiver?
if (node.getKind() != Tree.Kind.METHOD) { return false; }
MethodTree method = (MethodTree) node;
List<? extends VariableTree> params = method.getParameters();
if ("<init>".equals(method.getName().toString())) {
if (ix == last) { return true; }
ASTPath.ASTEntry next = astPath.get(++ix);
String selector = next.getChildSelector();
Tree typeTree =
ASTPath.TYPE_PARAMETER.equals(selector)
? method.getTypeParameters().get(next.getArgument())
: ASTPath.PARAMETER.equals(selector)
? params.get(next.getArgument()).getType()
: null;
return typeTree != null && checkTypePath(ix, typeTree);
} else if (entry.childSelectorIs(ASTPath.PARAMETER)
&& entry.getArgument() == -1) {
if (ix == last) { return true; }
VariableTree rcvrParam = method.getReceiverParameter();
if (rcvrParam == null) { // TODO
// ClassTree clazz = methodReceiverType(path);
// return checkReceiverType(ix,
// ((JCTree.JCClassDecl) clazz).type);
} else {
return checkTypePath(ix+1, rcvrParam.getType());
}
}
return false;
case NEW_ARRAY:
if (node.getKind() != Tree.Kind.NEW_ARRAY) { return false; }
NewArrayTree newArray = (NewArrayTree) node;
int arg = entry.getArgument();
if (entry.childSelectorIs(ASTPath.TYPE)) {
if (ix == last) { return true; }
// Tree t = newArray.getType();
// int depth = 1;
// while (t.getKind() == Tree.Kind.ARRAY_TYPE) {
// t = ((ArrayTypeTree) t).getType();
// ++depth;
// }
return arg == arrayDepth(newArray);
} else {
List<? extends ExpressionTree> typeTrees =
entry.childSelectorIs(ASTPath.DIMENSION)
? newArray.getDimensions()
: entry.childSelectorIs(ASTPath.INITIALIZER)
? newArray.getInitializers()
: null;
return typeTrees != null && arg < typeTrees.size()
&& checkTypePath(ix+1, typeTrees.get(arg));
}
case UNBOUNDED_WILDCARD:
return isBoundableWildcard(path, path.size()-1);
default: // TODO: casts?
return false;
}
}
示例10: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入方法依赖的package包/类
@Override
public Void visitNewArray(NewArrayTree tree, AnnotatedTypeMirror type) {
TreePath path = getPath(tree);
if (path.getLeaf().getKind() != Tree.Kind.CLASS) {
List<? extends ExpressionTree> dimensions = tree
.getDimensions();
List<? extends ExpressionTree> initializers = tree
.getInitializers();
// Dimensions provided
if (!dimensions.isEmpty()) {
handleDimensions(dimensions, (AnnotatedArrayType) type);
} else {
// Initializer used
int length = initializers.size();
HashSet<Integer> value = new HashSet<Integer>();
// If length is 0, so there is no initializer, and the
// kind of getType is not an ARRAY_TYPE, so the array is
// single-dimensional
if (length == 0)
// && tree.getType().getKind() != Tree.Kind.ARRAY_TYPE) -
// this caused an error on annotation with empty arrays
{
value.add(length);
}
// Check to ensure single-dimensionality by checking if
// the first initializer element is a list; either all
// elements must be lists or none so we only need to check
// the first
else if (length > 0
&& !initializers.get(0).getClass()
.equals(List.class)) {
value.add(length);
}
AnnotationMirror newQual;
String typeString = type.getUnderlyingType().toString();
if (typeString.equals("byte[]")
|| typeString.equals("char[]")) {
boolean allLiterals = true;
char[] chars = new char[initializers.size()];
for (int i = 0; i < chars.length && allLiterals; i++) {
ExpressionTree e = initializers.get(i);
if (e.getKind() == Tree.Kind.INT_LITERAL) {
chars[i] = (char) (((Integer) ((LiteralTree) initializers
.get(i)).getValue()).intValue());
} else {
allLiterals = false;
}
}
if (allLiterals) {
HashSet<String> stringFromChars = new HashSet<String>(1);
stringFromChars.add(new String(chars));
newQual = createAnnotation(
"org.checkerframework.common.value.qual.StringVal",
stringFromChars);
type.replaceAnnotation(newQual);
return null;
}
}
newQual = createAnnotation("org.checkerframework.common.value.qual.ArrayLen",
value);
type.replaceAnnotation(newQual);
}
}
return super.visitNewArray(tree, type);
}