本文整理汇总了Java中com.sun.source.util.Trees.getTypeMirror方法的典型用法代码示例。如果您正苦于以下问题:Java Trees.getTypeMirror方法的具体用法?Java Trees.getTypeMirror怎么用?Java Trees.getTypeMirror使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.source.util.Trees
的用法示例。
在下文中一共展示了Trees.getTypeMirror方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitClass
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitClass(ClassTree node, Element elementToFind) {
if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
return super.visitClass(node, elementToFind);
}
Trees trees = workingCopy.getTrees();
Types types = workingCopy.getTypes();
TypeMirror type2 = elementToFind.asType();
type2 = types.erasure(type2);
if (recursive) {
TypeMirror type1 = trees.getTypeMirror(getCurrentPath());
if (type1 != null) {
type1 = types.erasure(type1);
if (isSubtype(type1, type2)) {
addUsage(getCurrentPath());
}
}
} else {
TypeElement el = (TypeElement) workingCopy.getTrees().getElement(getCurrentPath());
if (el != null && el.getSuperclass()!=null && types.isSameType(types.erasure(el.getSuperclass()), type2) || containsType(el.getInterfaces(), type2)) {
addUsage(getCurrentPath());
}
}
return super.visitClass(node, elementToFind);
}
示例2: visitLambdaExpression
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitLambdaExpression(LambdaExpressionTree node, Element elementToFind) {
if (workingCopy.getTreeUtilities().isSynthetic(getCurrentPath())) {
return super.visitLambdaExpression(node, elementToFind);
}
Trees trees = workingCopy.getTrees();
Types types = workingCopy.getTypes();
TypeMirror type1 = trees.getTypeMirror(getCurrentPath());
if(type1 == null) {
return super.visitLambdaExpression(node, elementToFind);
}
type1 = types.erasure(type1);
TypeMirror type2 = elementToFind.asType();
type2 = types.erasure(type2);
if (types.isSameType(type1, type2) || (recursive && isSubtype(type1, type2))) {
addUsage(getCurrentPath());
}
return super.visitLambdaExpression(node, elementToFind);
}
示例3: visitIdentifier
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitIdentifier(IdentifierTree that, Trees trees) {
TypeMirror type = trees.getTypeMirror(this.getCurrentPath());
if (type == null /* will work even with error types || type.getKind() == TypeKind.ERROR */) {
return super.visitIdentifier(that, trees);
}
if (type.getKind().isPrimitive()) {
this.varToType.put(that.getName(), workingCopy.getTypes().boxedClass((PrimitiveType) type).toString());
} else {
this.varToType.put(that.getName(), type.toString());
}
TreePath currentTreePath = this.getCurrentPath();
Element el = trees.getElement(currentTreePath);
if (el != null && isExternalNEF(el, that)) {
checkIfRefactorableMutation(currentTreePath, that);
}
return super.visitIdentifier(that, trees);
}
示例4: createFinallyCloseBlockStatement
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private StatementTree createFinallyCloseBlockStatement(VariableTree origDeclaration) {
Trees trees = info.getTrees();
TypeMirror tm = trees.getTypeMirror(statement);
ElementUtilities elUtils = info.getElementUtilities();
Iterable iterable = elUtils.getMembers(tm, new ElementAcceptor() {
public boolean accept(Element e, TypeMirror type) {
return e.getKind() == ElementKind.METHOD && "close".equals(e.getSimpleName().toString()); // NOI18N
}
});
boolean throwsIO = false;
for (Iterator iter = iterable.iterator(); iter.hasNext(); ) {
ExecutableElement elem = (ExecutableElement) iter.next();
if (!elem.getParameters().isEmpty()) {
continue;
} else {
for (TypeMirror typeMirror : elem.getThrownTypes()) {
if ("java.io.IOException".equals(typeMirror.toString())) { // NOI18N
throwsIO = true;
break;
}
}
}
}
CharSequence name = origDeclaration.getName();
StatementTree close = make.ExpressionStatement(make.MethodInvocation(Collections.<ExpressionTree>emptyList(), make.MemberSelect(make.Identifier(name), "close"), Collections.<ExpressionTree>emptyList()));
StatementTree result = close;
if (throwsIO) {
result = make.Try(make.Block(Collections.singletonList(close), false), Collections.singletonList(createCatch(info, make, statement, inferName(info, statement), info.getElements().getTypeElement("java.io.IOException").asType())), null);
}
return result;
}
示例5: allowDiamond
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private boolean allowDiamond(CompilationInfo info, int offset, DeclaredType type) {
TreeUtilities tu = info.getTreeUtilities();
TreePath path = tu.pathFor(offset);
while (path != null && !(path.getLeaf() instanceof StatementTree)) {
path = path.getParentPath();
}
if (path != null) {
Trees trees = info.getTrees();
int pos = (int)trees.getSourcePositions().getStartPosition(path.getCompilationUnit(), path.getLeaf().getKind() == Tree.Kind.VARIABLE ? ((VariableTree)path.getLeaf()).getType() : path.getLeaf());
if (pos >= 0) {
Scope scope = tu.scopeFor(pos);
String stmt = info.getText().substring(pos, offset);
StringBuilder sb = new StringBuilder();
sb.append('{').append(stmt).append(Utilities.getTypeName(info, type, true)).append("();}"); //NOI18N;
SourcePositions[] sp = new SourcePositions[1];
StatementTree st = tu.parseStatement(sb.toString(), sp);
tu.attributeTree(st, scope);
TreePath tp = tu.pathFor(new TreePath(path, st), offset - pos, sp[0]);
TypeMirror tm = tp != null ? trees.getTypeMirror(tp) : null;
sb = new StringBuilder();
sb.append('{').append(stmt).append(((TypeElement)type.asElement()).getQualifiedName()).append("<>();}"); //NOI18N
st = tu.parseStatement(sb.toString(), sp);
tu.attributeTree(st, scope);
tp = tu.pathFor(new TreePath(path, st), offset - pos, sp[0]);
TypeMirror tmd = tp != null ? trees.getTypeMirror(tp) : null;
return tm != null && tmd != null && info.getTypes().isSameType(tm, tmd);
}
}
return false;
}
示例6: visitAssignment
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitAssignment(AssignmentTree node, Element p) {
if (!refactoring.getReplace()) {
return super.visitAssignment(node, p);
}
ExpressionTree variable = node.getVariable();
boolean isArray = false;
while (variable.getKind() == Tree.Kind.ARRAY_ACCESS) {
isArray = true;
// int[] a; a[a[0]][a[1]] = 0; // scan also array indices
scan(((ArrayAccessTree) variable).getIndex(), p);
variable = ((ArrayAccessTree) variable).getExpression();
}
Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), variable));
if(el != null) {
Element enclosingElement = el.getEnclosingElement();
if (enclosingElement != null && enclosingElement.equals(p)
&& (isArray || checkAssignmentInsideExpression())) {
ElementHandle<Element> handle = ElementHandle.create(el);
String[] getterSetter = getterSetterMap.get(handle); // TODO: Check for null and solve !
if (getterSetter != null) {
if (isArray) {
ExpressionTree invkgetter = createGetterInvokation(variable, getterSetter[0]);
rewrite(variable, invkgetter);
} else {
ExpressionTree setter = createMemberSelection(variable, getterSetter[1]);
// resolve types
Trees trees = workingCopy.getTrees();
ExpressionTree expTree = node.getExpression();
ExpressionTree newExpTree;
TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), variable);
TreePath expPath = trees.getPath(workingCopy.getCompilationUnit(), expTree);
TypeMirror varType = trees.getTypeMirror(varPath);
TypeMirror expType = trees.getTypeMirror(expPath);
if (workingCopy.getTypes().isSubtype(expType, varType)) {
newExpTree = expTree;
} else {
newExpTree = make.TypeCast(make.Type(varType), expTree);
}
MethodInvocationTree invksetter = make.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
setter,
Collections.singletonList(newExpTree));
rewrite(node, invksetter);
}
}
}
}
return scan(node.getExpression(), p);
}
示例7: visitUnary
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitUnary(UnaryTree node, Element p) {
if (!refactoring.getReplace()) {
return super.visitUnary(node, p);
}
ExpressionTree t = node.getExpression();
Tree.Kind kind = node.getKind();
boolean isArrayOrImmutable = kind != Tree.Kind.POSTFIX_DECREMENT
&& kind != Tree.Kind.POSTFIX_INCREMENT
&& kind != Tree.Kind.PREFIX_DECREMENT
&& kind != Tree.Kind.PREFIX_INCREMENT;
while (t.getKind() == Tree.Kind.ARRAY_ACCESS) {
isArrayOrImmutable = true;
t = ((ArrayAccessTree) t).getExpression();
}
Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), t));
if (el != null) {
Element enclosingElement = el.getEnclosingElement();
if (enclosingElement != null && enclosingElement.equals(p)
&& (isArrayOrImmutable || checkAssignmentInsideExpression())) {
ElementHandle<Element> handle = ElementHandle.create(el);
String[] getterSetter = getterSetterMap.get(handle); // TODO: Check for null and solve !
// check (++field + 3)
if (getterSetter != null) {
ExpressionTree invkgetter = createGetterInvokation(t, getterSetter[0]);
if (isArrayOrImmutable) {
rewrite(t, invkgetter);
} else {
ExpressionTree setter = createMemberSelection(node.getExpression(), getterSetter[1]);
Tree.Kind operator = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.PREFIX_INCREMENT
? Tree.Kind.PLUS
: Tree.Kind.MINUS;
// resolve types
Trees trees = workingCopy.getTrees();
ExpressionTree expTree = node.getExpression();
TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), expTree);
TypeMirror varType = trees.getTypeMirror(varPath);
TypeMirror expType = workingCopy.getTypes().getPrimitiveType(TypeKind.INT);
ExpressionTree newExpTree = make.Binary(operator, invkgetter, make.Literal(1));
if (!workingCopy.getTypes().isSubtype(expType, varType)) {
newExpTree = make.TypeCast(make.Type(varType), make.Parenthesized(newExpTree));
}
MethodInvocationTree invksetter = make.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
setter,
Collections.singletonList(newExpTree));
rewrite(node, invksetter);
}
}
}
}
return null;
}
示例8: visitAssignment
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitAssignment(AssignmentTree node, Element field) {
ExpressionTree variable = node.getVariable();
boolean isArray = false;
while (variable.getKind() == Tree.Kind.ARRAY_ACCESS) {
isArray = true;
// int[] a; a[a[0]][a[1]] = 0; // scan also array indices
scan(((ArrayAccessTree) variable).getIndex(), field);
variable = ((ArrayAccessTree) variable).getExpression();
}
Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), variable));
EncapsulateDesc desc = fields.get(el);
if (desc != null && desc.useAccessors && desc.refactoring.getSetterName() != null
// check (field = 3) == 3
&& (isArray || checkAssignmentInsideExpression())
&& !isInConstructorOfFieldClass(getCurrentPath(), desc.field)
&& !isInGetterSetter(getCurrentPath(), desc.currentGetter, desc.currentSetter)) {
if (isArray) {
ExpressionTree invkgetter = createGetterInvokation(variable, desc.refactoring.getGetterName());
rewrite(variable, invkgetter);
} else {
ExpressionTree setter = createMemberSelection(variable, desc.refactoring.getSetterName());
// resolve types
Trees trees = workingCopy.getTrees();
ExpressionTree expTree = node.getExpression();
ExpressionTree newExpTree;
TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), variable);
TreePath expPath = trees.getPath(workingCopy.getCompilationUnit(), expTree);
TypeMirror varType = trees.getTypeMirror(varPath);
TypeMirror expType = trees.getTypeMirror(expPath);
if (workingCopy.getTypes().isSubtype(expType, varType)) {
newExpTree = expTree;
} else {
newExpTree = make.TypeCast(make.Type(varType), expTree);
}
MethodInvocationTree invksetter = make.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
setter,
Collections.singletonList(newExpTree));
rewrite(node, invksetter);
setterUsed = true;
}
}
return scan(node.getExpression(), field);
}
示例9: visitUnary
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@Override
public Tree visitUnary(UnaryTree node, Element field) {
ExpressionTree t = node.getExpression();
Kind kind = node.getKind();
boolean isArrayOrImmutable = kind != Kind.POSTFIX_DECREMENT
&& kind != Kind.POSTFIX_INCREMENT
&& kind != Kind.PREFIX_DECREMENT
&& kind != Kind.PREFIX_INCREMENT;
while (t.getKind() == Tree.Kind.ARRAY_ACCESS) {
isArrayOrImmutable = true;
t = ((ArrayAccessTree) t).getExpression();
}
Element el = workingCopy.getTrees().getElement(new TreePath(getCurrentPath(), t));
EncapsulateDesc desc = el == null ? null : fields.get(el);
if (desc != null && desc.useAccessors
&& desc.refactoring.getGetterName() != null
&& (isArrayOrImmutable || checkAssignmentInsideExpression())
&& !isInConstructorOfFieldClass(getCurrentPath(), desc.field)
&& !isInGetterSetter(getCurrentPath(), desc.currentGetter, desc.currentSetter)) {
// check (++field + 3)
ExpressionTree invkgetter = createGetterInvokation(t, desc.refactoring.getGetterName());
if (isArrayOrImmutable) {
rewrite(t, invkgetter);
} else if (desc.refactoring.getSetterName() != null) {
ExpressionTree setter = createMemberSelection(node.getExpression(), desc.refactoring.getSetterName());
Tree.Kind operator = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.PREFIX_INCREMENT
? Tree.Kind.PLUS
: Tree.Kind.MINUS;
// resolve types
Trees trees = workingCopy.getTrees();
ExpressionTree expTree = node.getExpression();
TreePath varPath = trees.getPath(workingCopy.getCompilationUnit(), expTree);
TypeMirror varType = trees.getTypeMirror(varPath);
TypeMirror expType = workingCopy.getTypes().getPrimitiveType(TypeKind.INT);
ExpressionTree newExpTree = make.Binary(operator, invkgetter, make.Literal(1));
if (!workingCopy.getTypes().isSubtype(expType, varType)) {
newExpTree = make.TypeCast(make.Type(varType), make.Parenthesized(newExpTree));
}
MethodInvocationTree invksetter = make.MethodInvocation(
Collections.<ExpressionTree>emptyList(),
setter,
Collections.singletonList(newExpTree));
rewrite(node, invksetter);
}
}
return null;
}
示例10: checkMethodInvocation
import com.sun.source.util.Trees; //导入方法依赖的package包/类
private static boolean checkMethodInvocation(HintContext ctx, TreePath invPath, TreePath valPath) {
Trees trees = ctx.getInfo().getTrees();
Tree invLeaf = invPath.getLeaf();
List<? extends ExpressionTree> arguments;
TypeMirror m;
switch (invLeaf.getKind()) {
case METHOD_INVOCATION: {
MethodInvocationTree mit = (MethodInvocationTree)invLeaf;
arguments = mit.getArguments();
m = trees.getTypeMirror(new TreePath(invPath, mit.getMethodSelect()));
break;
}
case NEW_CLASS: {
NewClassTree nct = (NewClassTree)invLeaf;
arguments = nct.getArguments();
Element e = trees.getElement(invPath);
TypeMirror cl = trees.getTypeMirror(invPath);
if (!Utilities.isValidType(cl) || cl.getKind().isPrimitive()) {
return false;
}
m = ctx.getInfo().getTypes().asMemberOf((DeclaredType)cl, e);
break;
}
default:
return false;
}
if (!Utilities.isValidType(m) || m.getKind() != TypeKind.EXECUTABLE) {
return false;
}
ExecutableType execType = (ExecutableType)m;
int idx = arguments.indexOf(ctx.getPath().getLeaf());
if (idx < 0 || idx >= execType.getParameterTypes().size()) {
return false;
}
TypeMirror paramType = execType.getParameterTypes().get(idx);
TypeMirror curType = trees.getTypeMirror(ctx.getPath());
TypeMirror valType = trees.getTypeMirror(valPath);
if (!paramType.getKind().isPrimitive() && valType.getKind().isPrimitive()) {
valType = ctx.getInfo().getTypes().boxedClass((PrimitiveType)valType).asType();
// ensure that the passed INSTANCE type will not change when the boxing is removed
if (!ctx.getInfo().getTypes().isSameType(curType, valType)) {
return false;
}
}
return Utilities.checkAlternativeInvocation(ctx.getInfo(), invPath, ctx.getPath(), valPath, null);
}
示例11: run
import com.sun.source.util.Trees; //导入方法依赖的package包/类
@TriggerPatterns({
@TriggerPattern(value="$left == $right", constraints={@ConstraintVariableType(variable="$left", type=STRING_TYPE),
@ConstraintVariableType(variable="$right", type=STRING_TYPE)}),
@TriggerPattern(value="$left != $right", constraints={@ConstraintVariableType(variable="$left", type=STRING_TYPE),
@ConstraintVariableType(variable="$right", type=STRING_TYPE)})
})
public static ErrorDescription run(HintContext ctx) {
CompilationInfo info = ctx.getInfo();
TreePath treePath = ctx.getPath();
Tree t = treePath.getLeaf();
BinaryTree bt = (BinaryTree) t;
TreePath left = new TreePath(treePath, bt.getLeftOperand() );
TreePath right = new TreePath(treePath, bt.getRightOperand() );
Trees trees = info.getTrees();
TypeMirror leftType = left == null ? null : trees.getTypeMirror(left);
TypeMirror rightType = right == null ? null : trees.getTypeMirror(right);
if ( leftType != null && rightType != null &&
STRING_TYPE.equals(leftType.toString()) &&
STRING_TYPE.equals(rightType.toString())) {
if (checkInsideGeneratedEquals(ctx, treePath, left.getLeaf(), right.getLeaf())) {
return null;
}
FileObject file = info.getFileObject();
TreePathHandle tph = TreePathHandle.create(treePath, info);
ArrayList<Fix> fixes = new ArrayList<Fix>();
boolean reverseOperands = false;
if (bt.getLeftOperand().getKind() != Tree.Kind.STRING_LITERAL) {
if (bt.getRightOperand().getKind() == Tree.Kind.STRING_LITERAL) {
if (getStringLiteralsFirst(ctx.getPreferences())) {
reverseOperands = true;
} else {
fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.NULL_CHECK).toEditorFix());
}
} else {
fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.ternaryNullCheck(getTernaryNullCheck(ctx.getPreferences()))).toEditorFix());
}
}
fixes.add(new WrongStringComparisonFix(tph, WrongStringComparisonFix.Kind.reverseOperands(reverseOperands)).toEditorFix());
return ErrorDescriptionFactory.forTree(
ctx,
t,
NbBundle.getMessage(WrongStringComparison.class, "LBL_WrongStringComparison"),
fixes.toArray(new Fix[0]));
}
return null;
}