/**Checks whether the given expression is a compile-time constant, as per JLS 15.28. * * @param expression the expression to check * @return true if and only if the given expression represents a compile-time constant value * @since 0.91 */ public boolean isCompileTimeConstantExpression(TreePath expression) { TypeMirror attributeTree = info.getTrees().getTypeMirror(expression); Type attributeTreeImpl = (Type) attributeTree; return attributeTreeImpl != null && attributeTreeImpl.constValue() != null; }
/** * Returns the qualified Package Name of the given Tree object or null if the package could not be determined */ protected String getQualifiedPackageName(Tree tree) { TypeMirror typeMirror = trees.getTypeMirror(getCurrentPath()); if (typeMirror == null) { return null; } if (typeMirror.getKind() != TypeKind.DECLARED && typeMirror.getKind() != TypeKind.TYPEVAR) { return null; } Element typeMirrorElement = types.asElement(typeMirror); if (typeMirrorElement == null) { throw new IllegalStateException("Could not get Element for type '" + typeMirror + "'"); } PackageElement pakkage = elements.getPackageOf(typeMirrorElement); return pakkage.getQualifiedName().toString(); }
public Void visitThrow(ThrowTree node, Set<TypeMirror> p) { super.visitThrow(node, p); TypeMirror tm = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), node.getExpression())); if (tm != null) { if (tm.getKind() == TypeKind.DECLARED) p.add(tm); else if (tm.getKind() == TypeKind.UNION) p.addAll(((UnionType)tm).getAlternatives()); } return null; }
String postfix = SPACE; if (tp != null) { if (tp.getLeaf().getKind() == Tree.Kind.METHOD) { Tree rt = ((MethodTree) tp.getLeaf()).getReturnType(); if (rt == null || (rt.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) rt).getPrimitiveTypeKind() == TypeKind.VOID)) { postfix = SEMI; TypeMirror tm = env.getController().getTrees().getTypeMirror(tp); if (tm != null && tm.getKind() == TypeKind.DECLARED) { ExecutableType dt = env.getController().getTypeUtilities().getDescriptorType((DeclaredType) tm); if (dt != null && dt.getReturnType().getKind() == TypeKind.VOID) { postfix = SEMI; boolean bAdded = false; while (tp != null && !(cAdded && bAdded)) { switch (tp.getLeaf().getKind()) { case DO_WHILE_LOOP: case ENHANCED_FOR_LOOP:
private boolean typeMatches(TreePath currentPath, String placeholderName) { TypeMirror designed = designedTypeHack != null ? designedTypeHack.get(placeholderName) : null; boolean bind; if (designed != null && designed.getKind() != TypeKind.ERROR) { TypeMirror real = info.getTrees().getTypeMirror(currentPath); if (real != null && !IGNORE_KINDS.contains(real.getKind())) { // special hack: if the designed type is DECLARED (assuming a boxed primitive) and the real type is // not DECLARED or is null (assuming a real primitive), do not treat them as assignable. // this will stop matching constraint to boxed types against primitive subexpressions. Exclude j.l.Object // which will allow to match raw type parameters if (designed.getKind() == TypeKind.DECLARED && real.getKind().ordinal() <= TypeKind.DOUBLE.ordinal() && !((TypeElement)((DeclaredType)designed).asElement()).getQualifiedName().contentEquals("java.lang.Object")) { //NOI18N bind = false; } else { bind = info.getTypes().isAssignable(real, designed); } } else { bind = false; } } else { bind = designed == null; } return bind; }
int j = 0; for (Tree t : argTypes) { types[j++] = controller.getTrees().getTypeMirror(new TreePath(path, t)); path = new TreePath(path, mid); switch (mid.getKind()) { case MEMBER_SELECT: { ExpressionTree exp = ((MemberSelectTree) mid).getExpression(); path = new TreePath(path, exp); final Trees trees = controller.getTrees(); final TypeMirror type = trees.getTypeMirror(path); final Element element = trees.getElement(path); final boolean isStatic = element != null && (element.getKind().isClass() || element.getKind().isInterface() || element.getKind() == TYPE_PARAMETER); final Trees trees = controller.getTrees(); final TypeElement enclClass = scope.getEnclosingClass(); final boolean isStatic = enclClass != null ? (tu.isStaticContext(scope) || (env.getPath().getLeaf().getKind() == Tree.Kind.BLOCK && ((BlockTree) env.getPath().getLeaf()).isStatic())) : false; final Map<Name, ? extends Element> illegalForwardRefs = env.getForwardReferences(); final ExecutableElement method = scope.getEnclosingMethod();
@Override public Boolean visitThrow(ThrowTree tree, Stack<Tree> d) { addToExceptionsMap(info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), tree.getExpression())), tree); super.visitThrow(tree, d); return Boolean.TRUE; }
int j = 0; for (Tree t : argTypes) { types[j++] = controller.getTrees().getTypeMirror(new TreePath(path, t)); path = new TreePath(path, nct.getIdentifier()); final Trees trees = controller.getTrees(); final TypeMirror type = trees.getTypeMirror(path); final Element el = trees.getElement(path); final Scope scope = env.getScope();
public Void visitMethod(MethodTree node, Set<TypeMirror> p) { Set<TypeMirror> s = new LinkedHashSet<TypeMirror>(); scan(node.getBody(), s); for (ExpressionTree et : node.getThrows()) { TypeMirror t = info.getTrees().getTypeMirror(new TreePath(getCurrentPath(), et)); if (t != null && t.getKind() != TypeKind.ERROR) { for (Iterator<TypeMirror> it = s.iterator(); it.hasNext();) if (info.getTypes().isSubtype(it.next(), t)) it.remove(); } } p.addAll(s); return null; }
methodName = "<init>"; TreePath iPath = TreePath.getPath(cu, identifier); TypeMirror type = trees.getTypeMirror(iPath); if (type.getKind() == TypeKind.ERROR) { assert type.getKind() == TypeKind.DECLARED; TypeElement te = (TypeElement) types.asElement(type); methodClassType = ElementUtilities.getBinaryName(te); ExpressionTree exp = ((MemberSelectTree) identifier).getExpression(); TreePath expPath = TreePath.getPath(cu, exp); TypeMirror type = trees.getTypeMirror(expPath); if (type.getKind() == TypeKind.ERROR) {
String postfix = SPACE; if (tp != null) { if (tp.getLeaf().getKind() == Tree.Kind.METHOD) { Tree rt = ((MethodTree) tp.getLeaf()).getReturnType(); if (rt == null || (rt.getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) rt).getPrimitiveTypeKind() == TypeKind.VOID)) { postfix = SEMI; TypeMirror tm = env.getController().getTrees().getTypeMirror(tp); if (tm != null && tm.getKind() == TypeKind.DECLARED) { ExecutableType dt = env.getController().getTypeUtilities().getDescriptorType((DeclaredType) tm); if (dt != null && dt.getReturnType().getKind() == TypeKind.VOID) { postfix = SEMI; TreePath tp = env.getPath(); while (tp != null) { switch (tp.getLeaf().getKind()) { case SWITCH: CaseTree lastCase = null;
@Override public Boolean visitCatch(CatchTree tree, Stack<Tree> d) { TypeMirror type1 = info.getTrees().getTypeMirror(new TreePath(new TreePath(getCurrentPath(), tree.getParameter()), tree.getParameter().getType())); Types t = info.getTypes(); if (type1 != null) { Set<TypeMirror> toRemove = new HashSet<TypeMirror>(); Map<TypeMirror, List<Tree>> exceptions2Highlights = exceptions2HighlightsStack.peek(); if (exceptions2Highlights != null) { for (TypeMirror type2 : exceptions2Highlights.keySet()) { if (t.isAssignable(type2, type1)) { toRemove.add(type2); } } for (TypeMirror type : toRemove) { exceptions2Highlights.remove(type); } } } scan(tree.getParameter(), d); return scan(tree.getBlock(), d); }
static String getFQN(CompilationInfo cinfo, Tree tree) { return cinfo.getTrees().getTypeMirror(getTreePath(cinfo, tree)).toString(); }
public Void visitTry(TryTree node, Set<TypeMirror> p) { Set<TypeMirror> s = new LinkedHashSet<TypeMirror>(); Trees trees = info.getTrees(); Types types = info.getTypes(); Elements elements = info.getElements(); for (Tree res : node.getResources()) { TypeMirror resType = trees.getTypeMirror(new TreePath(getCurrentPath(), res)); if (resType != null && resType.getKind() == TypeKind.DECLARED) { for (ExecutableElement method : ElementFilter.methodsIn(elements.getAllMembers((TypeElement)((DeclaredType)resType).asElement()))) { if ("close".contentEquals(method.getSimpleName()) //NOI18N Set<TypeMirror> c = new LinkedHashSet<TypeMirror>(); for (CatchTree ct : node.getCatches()) { TypeMirror t = trees.getTypeMirror(new TreePath(getCurrentPath(), ct.getParameter().getType())); if (t != null) { if (t.getKind() == TypeKind.UNION) { for (TypeMirror tm : ((UnionType)t).getAlternatives()) { if (tm != null && tm.getKind() != TypeKind.ERROR) c.add(tm);
return null; // No path to the identifier... TypeMirror type = trees.getTypeMirror(iPath); if (type == null || type.getKind() == TypeKind.ERROR) { assert type.getKind() == TypeKind.DECLARED; TypeElement te = (TypeElement) types.asElement(type); methodClassType = ElementUtilities.getBinaryName(te); return null; // No path to the expression... TypeMirror type = trees.getTypeMirror(expPath); if (type == null || type.getKind() == TypeKind.ERROR) {
int lastBracket = Utilities.findLastBracket(methoddeclorblock, cu, info.getTrees().getSourcePositions(), document); return null; TypeMirror m = info.getTrees().getTypeMirror(TreePath.getPath(cu, t));
private @NonNull VerifyResult fullVerifyElements(TreePath node, TreePath p) { Element nodeEl = info.getTrees().getElement(node); Element pEl = info.getTrees().getElement(p); if (p.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT && node.getLeaf().getKind() == Tree.Kind.MEMBER_SELECT) { Tree selector = ((MemberSelectTree)p.getLeaf()).getExpression(); if (getWildcardTreeName(selector) != null) { Element nodeSelector = info.getTrees().getElement(new TreePath(node, ((MemberSelectTree)node.getLeaf()).getExpression())); if (nodeSelector != null && (nodeSelector.getKind().isClass() || nodeSelector.getKind().isInterface())) { matchingResult = VerifyResult.NO_MATCH; TypeMirror nodeTM = info.getTrees().getTypeMirror(node); if (nodeTM == null || nodeTM.getKind() == TypeKind.ERROR) { return VerifyResult.NO_MATCH_CONTINUE; TypeMirror pTM = info.getTrees().getTypeMirror(p); if (pTM == null || pTM.getKind() == TypeKind.ERROR) { return VerifyResult.NO_MATCH_CONTINUE;
private void insideUnionType(Env env) throws IOException { TreePath path = env.getPath(); UnionTypeTree dtt = (UnionTypeTree) path.getLeaf(); CompilationController controller = env.getController(); TokenSequence<JavaTokenId> last = findLastNonWhitespaceToken(env, dtt, env.getOffset()); Types types = controller.getTypes(); for (Tree t : dtt.getTypeAlternatives()) { TypeMirror tm = trees.getTypeMirror(new TreePath(path, t)); if (tm != null && tm.getKind() != TypeKind.ERROR) { for (Iterator<TypeMirror> it = exs.iterator(); it.hasNext();) { if (types.isSubtype(tm, it.next())) { if (ex.getKind() == TypeKind.DECLARED && startsWith(env, ((DeclaredType) ex).asElement().getSimpleName().toString()) && (Utilities.isShowDeprecatedMembers() || !elements.isDeprecated(((DeclaredType) ex).asElement())) && !Utilities.isExcluded(((TypeElement)((DeclaredType) ex).asElement()).getQualifiedName())) {
private void insideMemberReference(Env env) throws IOException { TreePath path = env.getPath(); MemberReferenceTree mr = (MemberReferenceTree) path.getLeaf(); TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, mr, env.getOffset()); if (ts != null) { CompilationController controller = env.getController(); ExpressionTree exp = mr.getQualifierExpression(); TreePath expPath = new TreePath(path, exp); Trees trees = controller.getTrees(); TypeMirror type = trees.getTypeMirror(expPath); if (type != null && type.getKind() == TypeKind.TYPEVAR) { while (type != null && type.getKind() == TypeKind.TYPEVAR) { type = ((TypeVariable) type).getUpperBound(); if (type != null && (type.getKind() == TypeKind.DECLARED || type.getKind() == TypeKind.ARRAY || type.getKind() == TypeKind.TYPEVAR)) { Element e = trees.getElement(expPath);
if (isMultistatementWildcardTree(p.getLeaf())) { return true; if (p != null && p.getLeaf().getKind() == Kind.IDENTIFIER) { treeName = ((IdentifierTree) p.getLeaf()).getName().toString(); } else if (p != null && p.getLeaf().getKind() == Kind.TYPE_PARAMETER && ((TypeParameterTree) p.getLeaf()).getBounds().isEmpty()) { treeName = ((TypeParameterTree) p.getLeaf()).getName().toString(); p = new TreePath(p, ptt.getType()); bindState.multiVariables.put(getWildcardTreeName(ptt.getTypeArguments().get(0)).toString(), Collections.<TreePath>emptyList()); TreePath currentPath = new TreePath(getCurrentPath(), node); boolean bind = typeMatches(currentPath, treeName); if (bind) { Element remappable = info.getTrees().getElement(p); TreePath currPath = new TreePath(getCurrentPath(), node); TypeMirror currType = info.getTrees().getTypeMirror(currPath); TypeMirror pType = ((VariableElement) remappable).asType();