private String fqnFor(Tree t) { Element el = ASTService.getElementImpl(t); if (el != null) { if (el.getKind().isClass() || el.getKind().isInterface() || el.getKind() == ElementKind.PACKAGE) { return ((QualifiedNameable) el).getQualifiedName().toString(); } else { Logger.getLogger(ElementOverlay.class.getName()).log(Level.SEVERE, "Not a QualifiedNameable: {0}", el); return null; } } else if (t instanceof QualIdentTree) { return ((QualIdentTree) t).getFQN(); } else if (t.getKind() == Kind.PARAMETERIZED_TYPE) { return fqnFor(((ParameterizedTypeTree) t).getType()); } else { Logger.getLogger(ElementOverlay.class.getName()).log(Level.FINE, "No element and no QualIdent"); return null; } }
@Override protected void performRewrite(TransformationContext ctx) { WorkingCopy copy = ctx.getWorkingCopy(); TreePath tp = ctx.getPath(); if (tp.getLeaf().getKind() != Tree.Kind.NEW_CLASS) { //XXX: warning return ; } NewClassTree nct = (NewClassTree) tp.getLeaf(); if (nct.getIdentifier().getKind() != Tree.Kind.PARAMETERIZED_TYPE) { //XXX: warning return ; } TreeMaker make = copy.getTreeMaker(); ParameterizedTypeTree ptt = (ParameterizedTypeTree) nct.getIdentifier(); ParameterizedTypeTree nue = make.ParameterizedType(ptt.getType(), Collections.<Tree>emptyList()); copy.rewrite(ptt, nue); }
private static ClassTree createGetClasses(WorkingCopy workingCopy, TreeMaker maker, ClassTree newTree, RestSupport restSupport, List<String> providerClasses) { ModifiersTree modifiersTree = maker.Modifiers( EnumSet.of(Modifier.PUBLIC), Collections.singletonList( maker.Annotation(maker.QualIdent( Override.class.getCanonicalName()), Collections.<ExpressionTree>emptyList()))); WildcardTree wildCard = maker.Wildcard(Tree.Kind.UNBOUNDED_WILDCARD, null); ParameterizedTypeTree wildClass = maker.ParameterizedType( maker.QualIdent(Class.class.getCanonicalName()), Collections.singletonList(wildCard)); ParameterizedTypeTree wildSet = maker.ParameterizedType( maker.QualIdent(Set.class.getCanonicalName()), Collections.singletonList(wildClass)); MethodTree methodTree = maker.Method(modifiersTree, RestConstants.GET_CLASSES, wildSet, Collections.<TypeParameterTree>emptyList(), Collections.<VariableTree>emptyList(), Collections.<ExpressionTree>emptyList(), createBodyForGetClassesMethod(restSupport, providerClasses), null); return maker.addClassMember(newTree, methodTree); }
/** * Gets the symbol declared by a tree. Returns null if {@code tree} does not declare a symbol or * is null. */ public static Symbol getDeclaredSymbol(Tree tree) { if (tree instanceof AnnotationTree) { return getSymbol(((AnnotationTree) tree).getAnnotationType()); } if (tree instanceof PackageTree) { return getSymbol((PackageTree) tree); } if (tree instanceof ParameterizedTypeTree) { return getSymbol(((ParameterizedTypeTree) tree).getType()); } if (tree instanceof TypeParameterTree) { Type type = ((JCTypeParameter) tree).type; return type == null ? null : type.tsym; } if (tree instanceof ClassTree) { return getSymbol((ClassTree) tree); } if (tree instanceof MethodTree) { return getSymbol((MethodTree) tree); } if (tree instanceof VariableTree) { return getSymbol((VariableTree) tree); } return null; }
private static Optional<Fix> linkedListFix(Tree tree, VisitorState state) { Type type = getTargetType(state); if (type == null) { return Optional.empty(); } Types types = state.getTypes(); for (String replacement : ImmutableList.of("java.util.ArrayList", "java.util.ArrayDeque")) { Symbol sym = state.getSymbolFromString(replacement); if (sym == null) { continue; } if (types.isAssignable(types.erasure(sym.asType()), types.erasure(type))) { SuggestedFix.Builder fix = SuggestedFix.builder(); while (tree instanceof ParameterizedTypeTree) { tree = ((ParameterizedTypeTree) tree).getType(); } fix.replace(tree, SuggestedFixes.qualifyType(state, fix, sym)); return Optional.of(fix.build()); } } return Optional.empty(); }
/** * Checks that the annotations on the type arguments supplied to a type or a * method invocation are within the bounds of the type variables as * declared, and issues the "type.argument.type.incompatible" error if they are * not. * * This method used to be visitParameterizedType, which incorrectly handles * the main annotation on generic types. */ protected Void visitParameterizedType(AnnotatedDeclaredType type, ParameterizedTypeTree tree) { // System.out.printf("TypeValidator.visitParameterizedType: type: %s, tree: %s\n", // type, tree); if (TreeUtils.isDiamondTree(tree)) return null; final TypeElement element = (TypeElement) type.getUnderlyingType().asElement(); if (checker.shouldSkipUses(element)) return null; List<AnnotatedTypeVariable> typevars = atypeFactory.typeVariablesFromUse(type, element); visitor.checkTypeArguments(tree, typevars, type.getTypeArguments(), tree.getTypeArguments()); return null; }
@Override public AnnotatedTypeMirror visitParameterizedType( ParameterizedTypeTree node, AnnotatedTypeFactory f) { List<AnnotatedTypeMirror> args = new LinkedList<AnnotatedTypeMirror>(); for (Tree t : node.getTypeArguments()) { args.add(visit(t, f)); } AnnotatedTypeMirror result = f.type(node); // use creator? AnnotatedTypeMirror atype = visit(node.getType(), f); result.addAnnotations(atype.getAnnotations()); // new ArrayList<>() type is AnnotatedExecutableType for some reason if (result instanceof AnnotatedDeclaredType) { assert result instanceof AnnotatedDeclaredType : node + " --> " + result; ((AnnotatedDeclaredType)result).setTypeArguments(args); } return result; }
@Override public Tree visitParameterizedType(ParameterizedTypeTree tree, Void p) { ParameterizedTypeTree n = make.ParameterizedType(tree.getType(), tree.getTypeArguments()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
public Boolean visitParameterizedType(ParameterizedTypeTree node, TreePath p) { if (p == null) return super.visitParameterizedType(node, p); ParameterizedTypeTree t = (ParameterizedTypeTree) p.getLeaf(); if (!scan(node.getType(), t.getType(), p)) return false; return checkLists(node.getTypeArguments(), t.getTypeArguments(), p); }
@Override public Number visitParameterizedType(ParameterizedTypeTree node, Void p) { List<? extends ExpressionTree> typeArgs = (List<? extends ExpressionTree>) resolveMultiParameters(node.getTypeArguments()); ParameterizedTypeTree nue = make.ParameterizedType(node.getType(), typeArgs); rewrite(node, nue); return super.visitParameterizedType(node, p); }
private static List<? extends TypeMirror> computeNewClass(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { NewClassTree nct = (NewClassTree) parent.getLeaf(); boolean errorInRealArguments = false; for (Tree param : nct.getArguments()) { errorInRealArguments |= param == error; } if (errorInRealArguments) { List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>(); int[] proposedIndex = new int[1]; List<ExecutableElement> ee = fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex); if (ee.isEmpty()) { //cannot be resolved return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return proposedTypes; } Tree id = nct.getIdentifier(); if (id.getKind() == Kind.PARAMETERIZED_TYPE) { id = ((ParameterizedTypeTree) id).getType(); } if (id == error) { return resolveType(EnumSet.noneOf(ElementKind.class), info, parent.getParentPath(), nct, offset, null, null); } return null; }
private static String simpleName(Tree t) { switch (t.getKind()) { case PARAMETERIZED_TYPE: return simpleName(((ParameterizedTypeTree) t).getType()); case IDENTIFIER: return ((IdentifierTree) t).getName().toString(); case MEMBER_SELECT: return ((MemberSelectTree) t).getIdentifier().toString(); default: return "";//XXX } }
@Override public Void visitParameterizedType(ParameterizedTypeTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingElement(below); addCorrespondingType(below); addCorrespondingComments(below); super.visitParameterizedType(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
private static List<? extends TypeMirror> computeNewClass(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { NewClassTree nct = (NewClassTree) parent.getLeaf(); boolean errorInRealArguments = false; for (Tree param : nct.getArguments()) { errorInRealArguments |= param == error; } if (errorInRealArguments) { List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>(); int[] proposedIndex = new int[1]; List<ExecutableElement> ee = org.netbeans.modules.editor.java.Utilities.fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex); if (ee.isEmpty()) { //cannot be resolved return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return proposedTypes; } Tree id = nct.getIdentifier(); if (id.getKind() == Kind.PARAMETERIZED_TYPE) { id = ((ParameterizedTypeTree) id).getType(); } if (id == error) { return resolveType(EnumSet.noneOf(ElementKind.class), info, parent.getParentPath(), nct, offset, null, null); } return null; }
private void filterByKind(Tree toFilter, Set<ElementKind> acceptedKinds, Set<ElementKind> notAcceptedKinds) { if (toFilter == null) return; switch (toFilter.getKind()) { case IDENTIFIER: hints.add(new KindHint(((IdentifierTree) toFilter).getName().toString(), acceptedKinds, notAcceptedKinds)); break; case PARAMETERIZED_TYPE: filterByKind(((ParameterizedTypeTree) toFilter).getType(), acceptedKinds, notAcceptedKinds); break; } }
@Override public Void visitParameterizedType(ParameterizedTypeTree node, Void unused) { sync(node); if (node.getTypeArguments().isEmpty()) { scan(node.getType(), null); token("<"); token(">"); } else { builder.open(plusFour); scan(node.getType(), null); token("<"); builder.breakOp(); builder.open(ZERO); boolean first = true; for (Tree typeArgument : node.getTypeArguments()) { if (!first) { token(","); // TODO(cushon): unify breaks builder.breakToFill(" "); } scan(typeArgument, null); first = false; } builder.close(); builder.close(); token(">"); } return null; }
private String createType(VariableTree tree) { Tree returnType = tree.getType(); if (returnType instanceof ParameterizedTypeTree || returnType instanceof ArrayTypeTree) { return returnType.toString(); } else { return TreeUtils.getTokenFromIdentifierTree(returnType).getImage(); } }
private String createDataType(Tree returnType) { if (returnType instanceof ParameterizedTypeTree || returnType instanceof ArrayTypeTree) { return returnType.toString(); } else { return TreeUtils.getTokenFromIdentifierTree(returnType).getImage(); } }
@Override public Pair<ASTRecord, Integer> visitParameterizedType(ParameterizedTypeTree node, Insertion ins) { Tree parent = parent(node); dbug.debug("TypePositionFinder.visitParameterizedType %s parent=%s%n", node, parent); Integer pos = getBaseTypePosition(((JCTypeApply) node).getType()).b; return Pair.of(astRecord(node), pos); }
private boolean checkTypePath(int i, Tree typeTree) { try { loop: while (typeTree != null && i < astPath.size()) { ASTPath.ASTEntry entry = astPath.get(i); Tree.Kind kind = entry.getTreeKind(); switch (kind) { case ANNOTATED_TYPE: typeTree = ((AnnotatedTypeTree) typeTree) .getUnderlyingType(); continue; case ARRAY_TYPE: typeTree = ((ArrayTypeTree) typeTree).getType(); break; case MEMBER_SELECT: typeTree = ((MemberSelectTree) typeTree).getExpression(); break; case PARAMETERIZED_TYPE: if (entry.childSelectorIs(ASTPath.TYPE_ARGUMENT)) { int arg = entry.getArgument(); typeTree = ((ParameterizedTypeTree) typeTree) .getTypeArguments().get(arg); } else { // TYPE typeTree = ((ParameterizedTypeTree) typeTree).getType(); } break; default: if (isWildcard(kind)) { return ++i == astPath.size(); // ??? } break loop; } ++i; } } catch (RuntimeException ex) {} return false; }
@Override @Nullable public Unifier visitParameterizedType( ParameterizedTypeTree typeApply, @Nullable Unifier unifier) { unifier = getType().unify(typeApply.getType(), unifier); return Unifier.unifyList(unifier, getTypeArguments(), typeApply.getTypeArguments()); }
@Override public Void visitParameterizedType(final ParameterizedTypeTree param, final AnnotatedTypeMirror atm) { TreePath path = atypeFactory.getPath(param); if (path != null) { final TreePath parentPath = path.getParentPath(); final Tree parentNode = parentPath.getLeaf(); if (!parentNode.getKind().equals(Kind.NEW_CLASS)) { variableAnnotator.visit(atm, param); } } return null; }
@Override public Void visitParameterizedType(ParameterizedTypeTree tree, VisitorState visitorState) { VisitorState state = visitorState.withPath(getCurrentPath()); for (ParameterizedTypeTreeMatcher matcher : parameterizedTypeMatchers) { if (!isSuppressed(matcher, state)) { try { reportMatch(matcher.matchParameterizedType(tree, state), tree, state); } catch (Throwable t) { handleError(matcher, t); } } } return super.visitParameterizedType(tree, state); }
@Override @Nullable public Choice<Unifier> visitParameterizedType( ParameterizedTypeTree typeApply, @Nullable Unifier unifier) { Choice<Unifier> choice = getType().unify(typeApply.getType(), unifier); if (getTypeArguments().isEmpty()) { // the template uses diamond syntax; accept anything except raw return choice.condition(typeApply.getTypeArguments() != null); } else { return choice.thenChoose(unifications(getTypeArguments(), typeApply.getTypeArguments())); } }
@Override public Description matchVariable(VariableTree tree, VisitorState state) { Type type = ASTHelpers.getType(tree); VarSymbol symbol = ASTHelpers.getSymbol(tree); if (type == null || symbol == null) { return NO_MATCH; } if (symbol.getKind() != ElementKind.PARAMETER) { return NO_MATCH; } if (!isSameType(type, state.getSymtab().iterableType, state)) { return NO_MATCH; } if (type.getTypeArguments().isEmpty()) { return NO_MATCH; } if (!isSameType( wildBound(getOnlyElement(type.getTypeArguments())), state.getTypeFromString(Path.class.getName()), state)) { return NO_MATCH; } Description.Builder description = buildDescription(tree); if (tree.getType() instanceof ParameterizedTypeTree) { description.addFix( SuggestedFix.builder() .addImport("java.util.Collection") .replace(((ParameterizedTypeTree) tree.getType()).getType(), "Collection") .build()); } return description.build(); }
/** * Returns true if the tree is of a diamond type. * In contrast to the implementation in TreeInfo, this version * works on Trees. * * @see com.sun.tools.javac.tree.TreeInfo#isDiamond(JCTree) */ public static final boolean isDiamondTree(Tree tree) { switch (tree.getKind()) { case ANNOTATED_TYPE: return isDiamondTree(((AnnotatedTypeTree)tree).getUnderlyingType()); case PARAMETERIZED_TYPE: return ((ParameterizedTypeTree)tree).getTypeArguments().isEmpty(); case NEW_CLASS: return isDiamondTree(((NewClassTree)tree).getIdentifier()); default: return false; } }
@Override public Void visitParameterizedType(ParameterizedTypeTree tree, Void p) { if (locations) { for (@SuppressWarnings("unused") Tree t : tree.getTypeArguments()) System.out.println("parameterized type"); } return super.visitParameterizedType(tree, p); }
@Override public JType visitParameterizedType(ParameterizedTypeTree pt, Void __) { JClass base = (JClass)visit(pt.getType()); List<JClass> args = new ArrayList<>(); for (Tree arg : pt.getTypeArguments()) { args.add((JClass)visit(arg)); } return base.narrow(args); }
@Override public String rawName() { if (type instanceof ParameterizedTypeTree) { ParameterizedTypeTree parameterizedType = (ParameterizedTypeTree) type; return ((TypeElement)((JCTree.JCTypeApply) parameterizedType).type.asElement()).getQualifiedName().toString(); } return null; }
/** * Returns true if the tree is of a diamond type. In contrast to the implementation in TreeInfo, * this version works on Trees. * * @see com.sun.tools.javac.tree.TreeInfo#isDiamond(JCTree) */ public static final boolean isDiamondTree(Tree tree) { switch (tree.getKind()) { case ANNOTATED_TYPE: return isDiamondTree(((AnnotatedTypeTree) tree).getUnderlyingType()); case PARAMETERIZED_TYPE: return ((ParameterizedTypeTree) tree).getTypeArguments().isEmpty(); case NEW_CLASS: return isDiamondTree(((NewClassTree) tree).getIdentifier()); default: return false; } }
@Override public Void visitParameterizedType(ParameterizedTypeTree expected, Tree actual) { Optional<ParameterizedTypeTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getType(), other.get().getType()); parallelScan(expected.getTypeArguments(), other.get().getTypeArguments()); return null; }
@Override public Wrapper visitParameterizedType(ParameterizedTypeTree arg0, Void arg1) { return new GenericTypeWrapper(arg0, visit(arg0.getType(), ExpressionWrapper.class), visitList( arg0.getTypeArguments(), ExpressionWrapper.class)); }
public Boolean visitParameterizedType(ParameterizedTypeTree node, ConstructorData p) { super.visitParameterizedType(node, p); return null; }
private String simpleName(Tree t) { if (t == null) { return Bundle.DisplayName_Unknown(); } if (t.getKind() == Kind.IDENTIFIER) { return ((IdentifierTree) t).getName().toString(); } if (t.getKind() == Kind.MEMBER_SELECT) { return ((MemberSelectTree) t).getIdentifier().toString(); } if (t.getKind() == Kind.METHOD_INVOCATION) { return scan(t, null); } if (t.getKind() == Kind.PARAMETERIZED_TYPE) { return simpleName(((ParameterizedTypeTree) t).getType()) + "<...>"; // NOI18N } if (t.getKind() == Kind.ARRAY_ACCESS) { return simpleName(((ArrayAccessTree) t).getExpression()) + "[]"; //NOI18N } if (t.getKind() == Kind.PARENTHESIZED) { return "(" + simpleName(((ParenthesizedTree)t).getExpression()) + ")"; //NOI18N } if (t.getKind() == Kind.TYPE_CAST) { return simpleName(((TypeCastTree)t).getType()); } if (t.getKind() == Kind.ARRAY_TYPE) { return simpleName(((ArrayTypeTree)t).getType()); } if (t.getKind() == Kind.PRIMITIVE_TYPE) { return ((PrimitiveTypeTree) t).getPrimitiveTypeKind().name().toLowerCase(); } throw new IllegalStateException("Currently unsupported kind of tree: " + t.getKind()); // NOI18N }
@Override public List<? extends TypeMirror> visitParameterizedType(ParameterizedTypeTree node, Object p) { return null; }
@Override public Void visitParameterizedType(ParameterizedTypeTree node, Map<String, Object> p) { scan(node.getType(), p); scan(node.getTypeArguments(), new HashMap<String, Object>()); return null; }
private static String varNameForTree(Tree et) { if (et == null) return null; switch (et.getKind()) { case IDENTIFIER: return ((IdentifierTree) et).getName().toString(); case MEMBER_SELECT: return ((MemberSelectTree) et).getIdentifier().toString(); case METHOD_INVOCATION: return varNameForTree(((MethodInvocationTree) et).getMethodSelect()); case NEW_CLASS: return firstToLower(varNameForTree(((NewClassTree) et).getIdentifier())); case PARAMETERIZED_TYPE: return firstToLower(varNameForTree(((ParameterizedTypeTree) et).getType())); case STRING_LITERAL: String name = guessLiteralName((String) ((LiteralTree) et).getValue()); if (name == null) { return firstToLower(String.class.getSimpleName()); } else { return firstToLower(name); } case VARIABLE: return ((VariableTree) et).getName().toString(); case ARRAY_ACCESS: name = varNameForTree(((ArrayAccessTree)et).getExpression()); if (name != null) { String singular = getSingular(name); if (singular != null) { return singular; } } return null; case ASSIGNMENT: if (((AssignmentTree)et).getExpression() != null) { return varNameForTree(((AssignmentTree)et).getExpression()); } return null; default: return null; } }
@Override public List<Tree> visitParameterizedType(ParameterizedTypeTree node, ExpressionScanner.ExpressionsInfo p) { return null; }
@Override public Mirror visitParameterizedType(ParameterizedTypeTree arg0, EvaluationContext evaluationContext) { return arg0.getType().accept(this, evaluationContext); }