本文整理匯總了Java中com.sun.source.tree.VariableTree類的典型用法代碼示例。如果您正苦於以下問題:Java VariableTree類的具體用法?Java VariableTree怎麽用?Java VariableTree使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
VariableTree類屬於com.sun.source.tree包,在下文中一共展示了VariableTree類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getArguments
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
List<ExpressionTree> getArguments() {
VariableTree var;
ExpressionTree lambda;
Tree lambdaBody;
if (this.correspondingTree.getKind() == Tree.Kind.BLOCK) {
lambdaBody = this.correspondingTree;
} else {
if (this.opType == OperationType.FILTER || this.opType == OperationType.ANYMATCH || this.opType == OperationType.NONEMATCH) {
lambdaBody = ((IfTree) this.correspondingTree).getCondition();
} else if (this.opType == OperationType.MAP) {
lambdaBody = getLambdaForMap();
} else if (this.opType == OperationType.FOREACH) {
lambdaBody = blockify(castToStatementTree(this.correspondingTree));
} else //if(this.opType== OperationType.REDUCE)
{
return getArgumentsForReducer();
}
}
var = getLambdaArguments();
lambda = treeMaker.LambdaExpression(Arrays.asList(var), lambdaBody);
List<ExpressionTree> args = new ArrayList<ExpressionTree>();
args.add(lambda);
return args;
}
示例2: createConstructor
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
/**
* Creates a class constructor.
*
* @param clazz the class to create the constructor for
* @param fields fields to be initialized by the constructor
* @return the constructor
* @since 0.20
*/
public MethodTree createConstructor(ClassTree clazz, Iterable<? extends VariableTree> fields) {
assert clazz != null && fields != null;
TreeMaker make = copy.getTreeMaker();
CodeStyle cs = DiffContext.getCodeStyle(copy);
Set<Modifier> mods = EnumSet.of(copy.getTreeUtilities().isEnum(clazz) ? Modifier.PRIVATE : Modifier.PUBLIC);
List<VariableTree> parameters = new ArrayList<VariableTree>();
List<StatementTree> statements = new ArrayList<StatementTree>();
ModifiersTree parameterModifiers = make.Modifiers(EnumSet.noneOf(Modifier.class));
for (VariableTree vt : fields) {
String paramName = addParamPrefixSuffix(removeFieldPrefixSuffix(vt, cs), cs);
parameters.add(make.Variable(parameterModifiers, paramName, vt.getType(), null));
statements.add(make.ExpressionStatement(make.Assignment(make.MemberSelect(make.Identifier("this"), vt.getName()), make.Identifier(paramName)))); //NOI18N
}
BlockTree body = make.Block(statements, false);
return make.Method(make.Modifiers(mods), "<init>", null, Collections.<TypeParameterTree> emptyList(), parameters, Collections.<ExpressionTree>emptyList(), body, null); //NOI18N
}
示例3: createGetter
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
/**
* Creates a getter method for a field.
*
* @param field field to create getter for
* @return the getter method
* @since 0.20
*/
public MethodTree createGetter(VariableTree field) {
assert field != null;
TreeMaker make = copy.getTreeMaker();
CodeStyle cs = DiffContext.getCodeStyle(copy);
Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);
boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC);
if (isStatic) {
mods.add(Modifier.STATIC);
}
Tree type = field.getType();
boolean isBoolean = type.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) type).getPrimitiveTypeKind() == TypeKind.BOOLEAN;
String getterName = CodeStyleUtils.computeGetterName(field.getName(), isBoolean, isStatic, cs);
BlockTree body = make.Block(Collections.singletonList(make.Return(make.Identifier(field.getName()))), false);
return make.Method(make.Modifiers(mods), getterName, type, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), body, null);
}
示例4: createSetter
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
/**
* Creates a setter method for a field.
*
* @param clazz the class to create the setter within
* @param field field to create setter for
* @return the setter method
* @since 0.20
*/
public MethodTree createSetter(ClassTree clazz, VariableTree field) {
assert clazz != null && field != null;
TreeMaker make = copy.getTreeMaker();
Set<Modifier> mods = EnumSet.of(Modifier.PUBLIC);
boolean isStatic = field.getModifiers().getFlags().contains(Modifier.STATIC);
if (isStatic)
mods.add(Modifier.STATIC);
CharSequence name = field.getName();
assert name.length() > 0;
CodeStyle cs = DiffContext.getCodeStyle(copy);
String propName = removeFieldPrefixSuffix(field, cs);
String setterName = CodeStyleUtils.computeSetterName(field.getName(), isStatic, cs);
String paramName = addParamPrefixSuffix(propName, cs);
List<VariableTree> params = Collections.singletonList(make.Variable(make.Modifiers(EnumSet.noneOf(Modifier.class)), paramName, field.getType(), null));
BlockTree body = make.Block(Collections.singletonList(make.ExpressionStatement(make.Assignment(make.MemberSelect(isStatic? make.Identifier(clazz.getSimpleName()) : make.Identifier("this"), name), make.Identifier(paramName)))), false); //NOI18N
return make.Method(make.Modifiers(mods), setterName, make.Type(copy.getTypes().getNoType(TypeKind.VOID)), Collections.<TypeParameterTree>emptyList(), params, Collections.<ExpressionTree>emptyList(), body, null);
}
示例5: testTypeMirrorHandleUnion
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
public void testTypeMirrorHandleUnion() throws Exception {
prepareTest();
writeIntoFile(testSource, "package test; public class Test { void t() { try { throw new Exception(); } catch (java.io.IOException | javax.swing.text.BadLocationException e) { } } }");
ClassPath empty = ClassPathSupport.createClassPath(new URL[0]);
JavaSource js = JavaSource.create(ClasspathInfo.create(ClassPathSupport.createClassPath(SourceUtilsTestUtil.getBootClassPath().toArray(new URL[0])), empty, empty), testSource);
js.runUserActionTask(new Task<CompilationController>() {
public void run(final CompilationController info) throws Exception {
info.toPhase(Phase.RESOLVED);
new ErrorAwareTreePathScanner<Void, Void>() {
@Override public Void visitVariable(VariableTree node, Void p) {
if (node.getName().contentEquals("e")) {
TypeMirror tm = info.getTrees().getTypeMirror(getCurrentPath());
assertEquals(TypeKind.UNION, tm.getKind());
assertTrue(info.getTypes().isSameType(tm, TypeMirrorHandle.create(tm).resolve(info)));
}
return super.visitVariable(node, p);
}
}.scan(info.getCompilationUnit(), null);
}
}, true);
}
示例6: testFieldChangeInInitValue
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
/**
* copy the initialization 'new String("NetBeers")' to 'new String()'.
* It tests only change in expression, no expression swap.
*/
public void testFieldChangeInInitValue() throws IOException {
System.err.println("testFieldChangeInInitValue");
process(
new Transformer<Void, Object>() {
public Void visitVariable(VariableTree node, Object p) {
super.visitVariable(node, p);
if ("initialValueReplacer".contentEquals(node.getName())) {
if (Tree.Kind.NEW_CLASS.equals(node.getInitializer().getKind())) {
NewClassTree nct = (NewClassTree) node.getInitializer();
NewClassTree njuNct = make.NewClass(
nct.getEnclosingExpression(),
(List<ExpressionTree>) nct.getTypeArguments(),
nct.getIdentifier(),
Collections.singletonList(make.Literal("NetBeans")),
nct.getClassBody()
);
copy.rewrite(nct, njuNct);
}
}
return null;
}
}
);
assertFiles("testFieldChangeInInitValue.pass");
}
示例7: implement
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
@Override
public ChangeInfo implement() throws Exception {
final FileObject file = handle.getFileObject();
final JTextComponent comp = EditorRegistry.lastFocusedComponent();
if (file != null && file == getFileObject(comp)) {
final int[] pos = new int[]{-1};
JavaSource.forFileObject(file).runUserActionTask(new Task<CompilationController>(){
@Override
public void run(CompilationController info) throws Exception {
info.toPhase(JavaSource.Phase.PARSED);
final TreePath tp = handle.resolve(info);
if (tp != null && tp.getLeaf().getKind() == Tree.Kind.VARIABLE) {
pos[0] = (int) info.getTrees().getSourcePositions().getEndPosition(
tp.getCompilationUnit(),
((VariableTree)tp.getLeaf()).getType()) + 1;
}
}
}, true);
invokeRefactoring (comp, pos[0]);
}
return null;
}
示例8: visitVariable
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
@Override
public Void visitVariable(VariableTree tree, Void d) {
Element el = info.getTrees().getElement(getCurrentPath());
if (toFind.equals(el)) {
try {
int[] span = treeUtils.findNameSpan(tree);
if(span != null) {
MutablePositionRegion region = createRegion(doc, span[0], span[1]);
usages.add(region);
}
} catch (BadLocationException ex) {
Exceptions.printStackTrace(ex);
}
}
if (el != null && el.getKind().isField()) {
handleJavadoc(getCurrentPath());
}
return super.visitVariable(tree, d);
}
示例9: getAllFields
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
public static List<VariableTree> getAllFields(JavaSource source) {
final List<VariableTree> allFields = new ArrayList<VariableTree>();
try {
source.runUserActionTask(new AbstractTask<CompilationController>() {
public void run(CompilationController controller) throws IOException {
controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
TypeElement classElement = getTopLevelClassElement(controller);
List<VariableElement> fields = ElementFilter.fieldsIn(classElement.getEnclosedElements());
for (VariableElement field : fields) {
allFields.add((VariableTree) controller.getTrees().getTree(field));
}
}
}, true);
} catch (IOException ex) {
}
return allFields;
}
示例10: getModifierTree
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
private ModifiersTree getModifierTree(TreePath treePath) {
Kind kind = treePath.getLeaf().getKind();
switch (kind) {
case ANNOTATION_TYPE:
case CLASS:
case ENUM:
case INTERFACE:
return ((ClassTree) treePath.getLeaf()).getModifiers();
case VARIABLE:
return ((VariableTree) treePath.getLeaf()).getModifiers();
case METHOD:
return ((MethodTree) treePath.getLeaf()).getModifiers();
default:
throw new UnsupportedOperationException("kind " + kind + " not yet suppported");
}
}
示例11: checkZeroSizeArray
import com.sun.source.tree.VariableTree; //導入依賴的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;
}
示例12: elementOf
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
private Element elementOf(Node n) {
Tree t = n.getTree();
if (t instanceof ExpressionTree) {
return TreeUtils.elementFromUse((ExpressionTree)t);
} else if (t instanceof MethodInvocationTree) {
return TreeUtils.elementFromUse((MethodInvocationTree)t);
} else if (t instanceof NewClassTree) {
return TreeUtils.elementFromUse((NewClassTree)t);
} else if (t instanceof VariableTree) {
return TreeUtils.elementFromDeclaration((VariableTree)t);
} else if (t instanceof MethodTree) {
return TreeUtils.elementFromDeclaration((MethodTree)t);
} else if (t instanceof ClassTree) {
return TreeUtils.elementFromDeclaration((ClassTree)t);
} else {
throw new RuntimeException("Unsupported tree type " + t.getClass());
}
}
示例13: createPropertyGetterMethod
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
/**
* Creates a new public property getter method.
*
* @param modifiersTree the method modifiers; cannot be null.
* @param propertyType the property type; cannot be null.
* @param propertyName the property name; cannot be null.
* @return the new method; never null.
*/
public MethodTree createPropertyGetterMethod(ModifiersTree modifiersTree, String propertyName, Tree propertyType) {
Parameters.notNull("modifiersTree", modifiersTree); // NOI18N
Parameters.javaIdentifier("propertyName", propertyName); // NOI18N
Parameters.notNull("propertyType", propertyType); // NOI18N
return getTreeMaker().Method(
modifiersTree,
createPropertyAccessorName(propertyName, true),
propertyType,
Collections.<TypeParameterTree>emptyList(),
Collections.<VariableTree>emptyList(),
Collections.<ExpressionTree>emptyList(),
"{ return " + propertyName + "; }", // NOI18N
null);
}
示例14: testNewClassWithEnclosing
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
@Test
void testNewClassWithEnclosing() throws IOException {
final String theString = "Test.this.new d()";
String code = "package test; class Test { " +
"class d {} private void method() { " +
"Object o = " + theString + "; } }";
JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, null, null,
null, Arrays.asList(new MyFileObject(code)));
CompilationUnitTree cut = ct.parse().iterator().next();
SourcePositions pos = Trees.instance(ct).getSourcePositions();
ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
ExpressionTree est =
((VariableTree) ((MethodTree) clazz.getMembers().get(1)).getBody().getStatements().get(0)).getInitializer();
final int spos = code.indexOf(theString);
final int epos = spos + theString.length();
assertEquals("testNewClassWithEnclosing",
spos, pos.getStartPosition(cut, est));
assertEquals("testNewClassWithEnclosing",
epos, pos.getEndPosition(cut, est));
}
示例15: createMethodSignature
import com.sun.source.tree.VariableTree; //導入依賴的package包/類
public static String createMethodSignature(MethodTree m, boolean skipName) {
String sign = "";
if (!skipName) {
sign += m.getName();
}
sign += "(";
if (m.getParameters() != null) {
for (VariableTree p : m.getParameters()) {
sign += getShortTypeName(p.getType()) + ",";
}
if (m.getParameters().size() > 0) {
sign = sign.substring(0, sign.length() - 1);
}
}
sign += ")";
return sign;
}