本文整理匯總了Java中com.sun.source.tree.VariableTree.getType方法的典型用法代碼示例。如果您正苦於以下問題:Java VariableTree.getType方法的具體用法?Java VariableTree.getType怎麽用?Java VariableTree.getType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.sun.source.tree.VariableTree
的用法示例。
在下文中一共展示了VariableTree.getType方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: explicitParameterTypes
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
@Hint(displayName="#DN_addExplicitLambdaParameters", description="#DESC_addExplicitLambdaParameters", category="suggestions", hintKind=Hint.Kind.ACTION)
@Messages({
"DN_addExplicitLambdaParameters=Convert Lambda to Use Explicit Parameter Types",
"DESC_addExplicitLambdaParameters=Converts lambdas to use explicit parameter types",
"ERR_addExplicitLambdaParameters=",
"FIX_addExplicitLambdaParameters=Use explicit parameter types"
})
@TriggerTreeKind(Kind.LAMBDA_EXPRESSION)
public static ErrorDescription explicitParameterTypes(HintContext ctx) {
LambdaExpressionTree let = (LambdaExpressionTree) ctx.getPath().getLeaf();
boolean hasSyntheticParameterName = false;
for (VariableTree var : let.getParameters()) {
hasSyntheticParameterName |= var.getType() == null || ctx.getInfo().getTreeUtilities().isSynthetic(TreePath.getPath(ctx.getPath(), var.getType()));
}
if (!hasSyntheticParameterName) {
return null;
}
return ErrorDescriptionFactory.forName(ctx, ctx.getPath(), Bundle.ERR_addExplicitLambdaParameters(), new AddExplicitLambdaParameterTypes(ctx.getInfo(), ctx.getPath()).toEditorFix());
}
示例2: visitToDeclare
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private void visitToDeclare(
DeclarationKind kind,
Direction annotationsDirection,
VariableTree node,
Optional<ExpressionTree> initializer,
String equals,
Optional<String> trailing) {
sync(node);
boolean varargs = VarArgsOrNot.fromVariable(node).isYes();
List<? extends AnnotationTree> varargsAnnotations = ImmutableList.of();
Tree type = node.getType();
declareOne(
kind,
annotationsDirection,
Optional.of(node.getModifiers()),
type,
VarArgsOrNot.valueOf(varargs),
varargsAnnotations,
node.getName(),
"",
equals,
initializer,
trailing,
Optional.<ExpressionTree>absent(),
Optional.<TypeWithDims>absent());
}
示例3: createHashCodeLineForField
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private String createHashCodeLineForField(VariableTree field) {
Name fieldName = field.getName();
Tree fieldType = field.getType();
if (fieldType.getKind() == Tree.Kind.PRIMITIVE_TYPE) {
if (((PrimitiveTypeTree) fieldType).getPrimitiveTypeKind() == TypeKind.BOOLEAN) {
return "hash += (" + fieldName + " ? 1 : 0"; // NOI18N
}
return "hash += (int)" + fieldName + ";"; // NOI18N
}
return "hash += (" + fieldName + " != null ? " + fieldName + ".hashCode() : 0);"; // NOI18N
}
示例4: createEqualsLineForField
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private String createEqualsLineForField(VariableTree field) {
Name fieldName = field.getName();
Tree fieldType = field.getType();
if (fieldType.getKind() == Tree.Kind.PRIMITIVE_TYPE) {
return "if (this." + fieldName + " != other." + fieldName + ") return false;"; // NOI18N
}
return "if ((this." + fieldName + " == null && other." + fieldName + " != null) || " + "(this." + fieldName +
" != null && !this." + fieldName + ".equals(other." + fieldName + "))) return false;"; // NOI18N
}
示例5: ReplaceConstructorWithBuilderUI
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private ReplaceConstructorWithBuilderUI(TreePathHandle constructor, CompilationInfo info) {
this.refactoring = new ReplaceConstructorWithBuilderRefactoring(constructor);
ExecutableElement contructorElement = (ExecutableElement) constructor.resolveElement(info);
this.name = contructorElement.getSimpleName().toString();
MethodTree constTree = (MethodTree) constructor.resolve(info).getLeaf();
paramaterNames = new ArrayList<String>();
parameterTypes = new ArrayList<String>();
parameterTypeVars = new ArrayList<Boolean>();
boolean varargs = contructorElement.isVarArgs();
List<? extends VariableElement> parameterElements = contructorElement.getParameters();
List<? extends VariableTree> parameters = constTree.getParameters();
for (int i = 0; i < parameters.size(); i++) {
VariableTree var = parameters.get(i);
paramaterNames.add(var.getName().toString());
String type = contructorElement.getParameters().get(i).asType().toString();
if(varargs && i+1 == parameters.size()) {
if(var.getType().getKind() == Tree.Kind.ARRAY_TYPE) {
ArrayTypeTree att = (ArrayTypeTree) var.getType();
type = att.getType().toString();
type += "..."; //NOI18N
}
}
parameterTypes.add(type);
parameterTypeVars.add(parameterElements.get(i).asType().getKind() == TypeKind.TYPEVAR);
}
TypeElement typeEl = (TypeElement) contructorElement.getEnclosingElement();
if(typeEl.getNestingKind() != NestingKind.TOP_LEVEL) {
PackageElement packageOf = info.getElements().getPackageOf(typeEl);
builderFQN = packageOf.toString() + "." + typeEl.getSimpleName().toString();
} else {
builderFQN = typeEl.getQualifiedName().toString();
}
buildMethodName = "create" + typeEl.getSimpleName();
}
示例6: computeVariableDeclaration
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private static List<? extends TypeMirror> computeVariableDeclaration(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
VariableTree vt = (VariableTree) parent.getLeaf();
if (vt.getInitializer() == error) {
types.add(ElementKind.PARAMETER);
types.add(ElementKind.LOCAL_VARIABLE);
types.add(ElementKind.FIELD);
return Collections.singletonList(info.getTrees().getTypeMirror(new TreePath(parent, vt.getType())));
}
TreePath context = parent.getParentPath();
if (vt.getType() != error || context == null) {
return null;
}
switch (context.getLeaf().getKind()) {
case ENHANCED_FOR_LOOP:
ExpressionTree iterableTree = ((EnhancedForLoopTree) context.getLeaf()).getExpression();
TreePath iterablePath = new TreePath(context, iterableTree);
TypeMirror type = getIterableGenericType(info, iterablePath);
types.add(ElementKind.LOCAL_VARIABLE);
return Collections.singletonList(type);
default:
types.add(ElementKind.CLASS);
return Collections.<TypeMirror>emptyList();
}
}
示例7: visitVariable
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
@Override
public List<? extends TypeMirror> visitVariable(VariableTree node, Object p) {
if (theExpression == null) {
if (node.getInitializer() == null) {
return null;
}
initExpression(node.getInitializer());
}
if (theExpression.getLeaf() == node.getInitializer()) {
// the expression must be assiganble to the variable.
this.expectedTree = new TreePath(getCurrentPath(), node.getType());
return Collections.singletonList(info.getTrees().getTypeMirror(getCurrentPath()));
}
return null;
}
示例8: visitUnionType
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
/**
* Formats a union type declaration in a catch clause.
*/
private void visitUnionType(VariableTree declaration) {
UnionTypeTree type = (UnionTypeTree) declaration.getType();
builder.open(ZERO);
sync(declaration);
visitAndBreakModifiers(
declaration.getModifiers(), Direction.HORIZONTAL, Optional.<BreakTag>absent());
List<? extends Tree> union = type.getTypeAlternatives();
boolean first = true;
for (int i = 0; i < union.size() - 1; i++) {
if (!first) {
builder.breakOp(" ");
token("|");
builder.space();
} else {
first = false;
}
scan(union.get(i), null);
}
builder.breakOp(" ");
token("|");
builder.space();
Tree last = union.get(union.size() - 1);
declareOne(
DeclarationKind.NONE,
Direction.HORIZONTAL,
Optional.<ModifiersTree>absent(),
last,
VarArgsOrNot.NO, // VarArgsOrNot.valueOf(declaration.isVarargs()),
ImmutableList.<AnnotationTree>of(), // declaration.varargsAnnotations(),
declaration.getName(),
"",
// declaration.extraDimensions(),
"=",
Optional.fromNullable(declaration.getInitializer()),
Optional.<String>absent(),
Optional.<ExpressionTree>absent(),
Optional.<TypeWithDims>absent());
builder.close();
}
示例9: visitVariable
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
@Override
public Void visitVariable(VariableTree tree, Map<String, Object> p) {
scan(tree.getModifiers(), p);
if (tree.getType() != null && tree.getType().getKind() == Kind.IDENTIFIER) {
p.put("request", null);
}
scan(tree.getType(), p);
Union2<String, DeclaredType> leftSide = (Union2<String, DeclaredType>) p.remove("result");
p.remove("request");
Union2<String, DeclaredType> rightSide = null;
if (leftSide != null && tree.getInitializer() != null) {
Element el = info.getTrees().getElement(new TreePath(getCurrentPath(),tree.getInitializer()));
TypeMirror rightType = el != null ? el.asType() : null;
// System.err.println("rightType = " + rightType );
// System.err.println("tree.getInitializer()=" + tree.getInitializer());
// System.err.println("rightType.getKind()=" + rightType.getKind());
// System.err.println("INVALID_TYPES.contains(rightType.getKind())=" + INVALID_TYPES.contains(rightType.getKind()));
if (rightType != null && rightType.getKind() == TypeKind.DECLARED) {
rightSide = Union2.<String, DeclaredType>createSecond((DeclaredType) rightType);
} else {
if (tree.getInitializer().getKind() == Kind.NEW_CLASS || tree.getInitializer().getKind() == Kind.NEW_ARRAY) {
p.put("request", null);
}
}
}
scan(tree.getInitializer(), p);
rightSide = rightSide == null ? (Union2<String, DeclaredType>) p.remove("result") : rightSide;
p.remove("result");
// System.err.println("rightSide = " + rightSide );
p.remove("request");
if (leftSide != null && rightSide != null) {
if (!(leftSide instanceof TypeMirror) || !(rightSide instanceof TypeMirror)) {
hints.add(new TypeHint(leftSide, rightSide));
}
}
return null;
}
示例10: processVariables
import com.sun.source.tree.VariableTree; //導入方法依賴的package包/類
private List<Snippet> processVariables(String userSource, List<? extends Tree> units, String compileSource, ParseTask pt) {
List<Snippet> snippets = new ArrayList<>();
TreeDissector dis = TreeDissector.createByFirstClass(pt);
for (Tree unitTree : units) {
VariableTree vt = (VariableTree) unitTree;
String name = vt.getName().toString();
String typeName = EvalPretty.prettyExpr((JCTree) vt.getType(), false);
Tree baseType = vt.getType();
TreeDependencyScanner tds = new TreeDependencyScanner();
tds.scan(baseType); // Not dependent on initializer
StringBuilder sbBrackets = new StringBuilder();
while (baseType instanceof ArrayTypeTree) {
//TODO handle annotations too
baseType = ((ArrayTypeTree) baseType).getType();
sbBrackets.append("[]");
}
Range rtype = dis.treeToRange(baseType);
Range runit = dis.treeToRange(vt);
runit = new Range(runit.begin, runit.end - 1);
ExpressionTree it = vt.getInitializer();
Range rinit = null;
int nameMax = runit.end - 1;
SubKind subkind;
if (it != null) {
subkind = SubKind.VAR_DECLARATION_WITH_INITIALIZER_SUBKIND;
rinit = dis.treeToRange(it);
nameMax = rinit.begin - 1;
} else {
subkind = SubKind.VAR_DECLARATION_SUBKIND;
}
int nameStart = compileSource.lastIndexOf(name, nameMax);
if (nameStart < 0) {
throw new AssertionError("Name '" + name + "' not found");
}
int nameEnd = nameStart + name.length();
Range rname = new Range(nameStart, nameEnd);
Wrap guts = Wrap.varWrap(compileSource, rtype, sbBrackets.toString(), rname, rinit);
DiagList modDiag = modifierDiagnostics(vt.getModifiers(), dis, true);
Snippet snip = new VarSnippet(state.keyMap.keyForVariable(name), userSource, guts,
name, subkind, typeName,
tds.declareReferences(), modDiag);
snippets.add(snip);
}
return snippets;
}