本文整理汇总了Java中com.sun.source.tree.NewArrayTree类的典型用法代码示例。如果您正苦于以下问题:Java NewArrayTree类的具体用法?Java NewArrayTree怎么用?Java NewArrayTree使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NewArrayTree类属于com.sun.source.tree包,在下文中一共展示了NewArrayTree类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: rewriteNewArrayTree
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
private void rewriteNewArrayTree(WorkingCopy copy, TreeMaker mk, TreePath natPath, TypeMirror compType) {
NewArrayTree nat = (NewArrayTree)natPath.getLeaf();
TypeMirror existing = copy.getTrees().getTypeMirror(natPath);
int existingDim = numberOfDimensions(existing);
int newDim = numberOfDimensions(compType);
if (existingDim == newDim + 1 /* newDim is counted from component type, lacks the enclosing array */) {
// simple, number of dimensions does not change
copy.rewrite(nat.getType(), mk.Type(compType));
return;
}
List<ExpressionTree> l = new ArrayList<ExpressionTree>(nat.getDimensions().subList(
0, Math.min(newDim + 1, nat.getDimensions().size())));
Tree replacement = mk.NewArray(mk.Type(compType), l, null);
GeneratorUtilities.get(copy).copyComments(nat, replacement, true);
GeneratorUtilities.get(copy).copyComments(nat, replacement, false);
copy.rewrite(nat, replacement);
}
示例2: findArrayValue
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public static List<AnnotationTree> findArrayValue(AnnotationTree at, String name) {
ExpressionTree fixesArray = findValue(at, name);
List<AnnotationTree> fixes = new LinkedList<AnnotationTree>();
if (fixesArray != null && fixesArray.getKind() == Kind.NEW_ARRAY) {
NewArrayTree trees = (NewArrayTree) fixesArray;
for (ExpressionTree fix : trees.getInitializers()) {
if (fix.getKind() == Kind.ANNOTATION) {
fixes.add((AnnotationTree) fix);
}
}
}
if (fixesArray != null && fixesArray.getKind() == Kind.ANNOTATION) {
fixes.add((AnnotationTree) fixesArray);
}
return fixes;
}
示例3: 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;
}
示例4: getArrayType
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
private ArrayType getArrayType(NewArrayTree arg0, Type type, int depth, EvaluationContext evaluationContext) {
String arrayClassName;
if (depth < BRACKETS.length()/2) {
arrayClassName = type.name() + BRACKETS.substring(0, 2*depth);
} else {
arrayClassName = type.name() + BRACKETS;
for (int i = BRACKETS.length()/2; i < depth; i++) {
arrayClassName += "[]"; // NOI18N
}
}
ReferenceType rt = getOrLoadClass(type.virtualMachine(), arrayClassName, evaluationContext);
if (rt == null) {
Assert.error(arg0, "unknownType", arrayClassName);
}
return (ArrayType) rt;
}
示例5: rowLength
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
static int rowLength(List<? extends ExpressionTree> row) {
int size = 0;
for (ExpressionTree tree : row) {
if (tree.getKind() != NEW_ARRAY) {
size++;
continue;
}
NewArrayTree array = (NewArrayTree) tree;
if (array.getInitializers() == null) {
size++;
continue;
}
size += rowLength(array.getInitializers());
}
return size;
}
示例6: matches
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
@Override
public boolean matches(AnnotationTree annotationTree, VisitorState state) {
ExpressionTree expressionTree = AnnotationMatcherUtils.getArgument(annotationTree, element);
if (expressionTree == null) {
return false;
}
expressionTree = ASTHelpers.stripParentheses(expressionTree);
if (expressionTree instanceof NewArrayTree) {
NewArrayTree arrayTree = (NewArrayTree) expressionTree;
for (ExpressionTree elementTree : arrayTree.getInitializers()) {
if (valueMatcher.matches(elementTree, state)) {
return true;
}
}
return false;
}
return valueMatcher.matches(expressionTree, state);
}
示例7: annotationsFromArrayCreation
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public final static List<? extends AnnotationMirror> annotationsFromArrayCreation(NewArrayTree node, int level) {
assert node instanceof JCNewArray;
final JCNewArray newArray = ((JCNewArray) node);
if (level == -1) {
return annotationsFromTypeAnnotationTrees(newArray.annotations);
}
if (newArray.dimAnnotations.length() > 0
&& (level >= 0)
&& (level < newArray.dimAnnotations.size()))
return annotationsFromTypeAnnotationTrees(newArray.dimAnnotations.get(level));
return Collections.emptyList();
}
示例8: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
@Override
public Void visitNewArray(NewArrayTree node, Void p) {
AnnotatedArrayType type = atypeFactory.getAnnotatedType(node);
AnnotatedTypeMirror componentType = type.getComponentType();
if (componentType.hasEffectiveAnnotation(NONNULL)
&& !isNewArrayAllZeroDims(node)
&& !isNewArrayInToArray(node)
&& !TypesUtils.isPrimitive(componentType.getUnderlyingType())
&& checker.getLintOption("arrays:forbidnonnullcomponents",
false)) {
checker.report(
Result.failure("new.array.type.invalid",
componentType.getAnnotations(), type.toString()),
node);
}
return super.visitNewArray(node, p);
}
示例9: 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;
}
示例10: annotateArrayAsArray
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
private void annotateArrayAsArray(AnnotatedArrayType result, NewArrayTree node, AnnotatedTypeFactory f) {
// Copy annotations from the type.
AnnotatedTypeMirror treeElem = f.fromTypeTree(node.getType());
boolean hasInit = node.getInitializers() != null;
AnnotatedTypeMirror typeElem = descendBy(result,
hasInit ? 1 : node.getDimensions().size());
while (true) {
typeElem.addAnnotations(treeElem.getAnnotations());
if (!(treeElem instanceof AnnotatedArrayType)) break;
assert typeElem instanceof AnnotatedArrayType;
treeElem = ((AnnotatedArrayType)treeElem).getComponentType();
typeElem = ((AnnotatedArrayType)typeElem).getComponentType();
}
// Add all dimension annotations.
int idx = 0;
AnnotatedTypeMirror level = result;
while (level.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType array = (AnnotatedArrayType)level;
List<? extends AnnotationMirror> annos = InternalUtils.annotationsFromArrayCreation(node, idx++);
array.addAnnotations(annos);
level = array.getComponentType();
}
// Add top-level annotations.
result.addAnnotations(InternalUtils.annotationsFromArrayCreation(node, -1));
}
示例11: annotationsFromArrayCreation
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public static final List<? extends AnnotationMirror> annotationsFromArrayCreation(
NewArrayTree node, int level) {
assert node instanceof JCNewArray;
final JCNewArray newArray = ((JCNewArray) node);
if (level == -1) {
return annotationsFromTypeAnnotationTrees(newArray.annotations);
}
if (newArray.dimAnnotations.length() > 0
&& (level >= 0)
&& (level < newArray.dimAnnotations.size()))
return annotationsFromTypeAnnotationTrees(newArray.dimAnnotations.get(level));
return Collections.emptyList();
}
示例12: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
@Override
public Tree visitNewArray(NewArrayTree tree, Void p) {
NewArrayTree n = make.NewArray(tree.getType(), tree.getDimensions(), tree.getInitializers());
model.setType(n, model.getType(tree));
comments.copyComments(tree, n);
model.setPos(n, model.getPos(tree));
return n;
}
示例13: visitNewArray
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public Boolean visitNewArray(NewArrayTree node, TreePath p) {
if (p == null)
return super.visitNewArray(node, p);
NewArrayTree t = (NewArrayTree) p.getLeaf();
if (!checkLists(node.getDimensions(), t.getDimensions(), p))
return false;
if (!checkLists(node.getInitializers(), t.getInitializers(), p))
return false;
return scan(node.getType(), t.getType(), p);
}
示例14: test120768
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public void test120768() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"\n" +
"public class Test {\n" +
" byte[] test = new byte[10000];\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"public class Test {\n" +
" byte[] test = new byte[WHAT_A_F];\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
VariableTree var = (VariableTree) clazz.getMembers().get(1);
NewArrayTree nat = (NewArrayTree) var.getInitializer();
workingCopy.rewrite(nat.getDimensions().get(0), make.Identifier("WHAT_A_F"));
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}
示例15: test162485a
import com.sun.source.tree.NewArrayTree; //导入依赖的package包/类
public void test162485a() throws Exception {
testFile = new File(getWorkDir(), "Test.java");
TestUtilities.copyStringToFile(testFile,
"package hierbas.del.litoral;\n" +
"\n" +
"public class Test {\n" +
" Object test = new int[2];\n" +
"}\n"
);
String golden =
"package hierbas.del.litoral;\n" +
"\n" +
"public class Test {\n" +
" Object test = {{1}};\n" +
"}\n";
JavaSource src = getJavaSource(testFile);
Task<WorkingCopy> task = new Task<WorkingCopy>() {
public void run(WorkingCopy workingCopy) throws IOException {
workingCopy.toPhase(Phase.RESOLVED);
CompilationUnitTree cut = workingCopy.getCompilationUnit();
TreeMaker make = workingCopy.getTreeMaker();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
VariableTree var = (VariableTree) clazz.getMembers().get(1);
NewArrayTree nat = (NewArrayTree) var.getInitializer();
NewArrayTree dim2 = make.NewArray(null, Collections.<ExpressionTree>emptyList(), Collections.singletonList(make.Literal(Integer.valueOf(1))));
NewArrayTree newTree = make.NewArray(null, Collections.<ExpressionTree>emptyList(), Collections.<ExpressionTree>singletonList(dim2));
workingCopy.rewrite(nat, newTree);
}
};
src.runModificationTask(task).commit();
String res = TestUtilities.copyFileToString(testFile);
System.err.println(res);
assertEquals(golden, res);
}