@Override public List<ErrorDescription> run(CompilationInfo info, TreePath treePath) { Tree t = treePath.getLeaf(); JTextComponent editor = EditorRegistry.lastFocusedComponent(); Document doc = editor.getDocument(); SourcePositions sp = info.getTrees().getSourcePositions(); int start = (int) sp.getStartPosition(info.getCompilationUnit(), t); int end = (int) sp.getEndPosition(info.getCompilationUnit(), t); int endLine = info.getText().indexOf("\n", start); String line = info.getText().substring(start, endLine).trim(); if (!Suppression.isSuppressed(line)) { return Collections.<ErrorDescription>singletonList( ErrorDescriptionFactory.createErrorDescription( Severity.HINT, ERROR_DESCRIPTION, createFixes(doc, start, endLine), info.getFileObject(), start, end)); } else { return null; } }
private static List<? extends TypeMirror> computeReturn( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { ReturnTree rt = (ReturnTree) parent.getLeaf(); if (rt.getExpression() == error) { TreePath method = findMethod(parent); if (method == null) { return null; } Element el = info.getTrees().getElement(method); if (el == null || el.getKind() != ElementKind.METHOD) { return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(((ExecutableElement) el).getReturnType()); } return null; }
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(); } }
private static List<? extends TypeMirror> computeEnhancedForLoop( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { EnhancedForLoopTree efl = (EnhancedForLoopTree) parent.getLeaf(); if (efl.getExpression() != error) { return null; } TypeMirror argument = info.getTrees() .getTypeMirror( new TreePath(new TreePath(parent, efl.getVariable()), efl.getVariable().getType())); if (argument == null) return null; if (argument.getKind().isPrimitive()) { types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(info.getTypes().getArrayType(argument)); } TypeElement iterable = info.getElements().getTypeElement("java.lang.Iterable"); // NOI18N if (iterable == null) { return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(info.getTypes().getDeclaredType(iterable, argument)); }
private static List<? extends TypeMirror> computeParametrizedType( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset, TypeMirror[] typeParameterBound, int[] numTypeParameters) { ParameterizedTypeTree ptt = (ParameterizedTypeTree) parent.getLeaf(); if (ptt.getType() == error) { Tree gpt = parent.getParentPath().getLeaf(); if (TreeUtilities.CLASS_TREE_KINDS.contains(gpt.getKind()) && ((ClassTree) gpt).getExtendsClause() == ptt) { types.add(ElementKind.CLASS); } else if (TreeUtilities.CLASS_TREE_KINDS.contains(gpt.getKind()) && ((ClassTree) gpt).getImplementsClause().contains(ptt)) { types.add(ElementKind.INTERFACE); } else { types.add(ElementKind.CLASS); types.add(ElementKind.INTERFACE); } if (numTypeParameters != null) { numTypeParameters[0] = ptt.getTypeArguments().size(); } return null; } TypeMirror resolved = info.getTrees().getTypeMirror(parent); DeclaredType resolvedDT = null; if (resolved != null && resolved.getKind() == TypeKind.DECLARED) { resolvedDT = (DeclaredType) resolved; } int index = 0; for (Tree t : ptt.getTypeArguments()) { if (t == error) { if (resolvedDT != null && typeParameterBound != null) { List<? extends TypeMirror> typeArguments = ((DeclaredType) resolvedDT.asElement().asType()).getTypeArguments(); if (typeArguments.size() > index) { typeParameterBound[0] = ((TypeVariable) typeArguments.get(index)).getUpperBound(); } } types.add(ElementKind.CLASS); // XXX: class/interface/enum/annotation? return null; } index++; } return null; }
/* (non-Javadoc) * @see com.sun.source.util.TreeScanner#visitClass(com.sun.source.tree.ClassTree, java.lang.Object) */ @Override public Void visitClass(ClassTree t, Void v) { Element el = info.getTrees().getElement(getCurrentPath()); if (el == null) { StatusDisplayer.getDefault().setStatusText("Cannot resolve class!"); } else { typeElement = (TypeElement) el; } return null; }
private static List<? extends TypeMirror> computeAssignment( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { AssignmentTree at = (AssignmentTree) parent.getLeaf(); TypeMirror type = null; if (at.getVariable() == error) { type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression())); if (type != null) { // anonymous class? type = org.netbeans.modules.java.hints.errors.Utilities.convertIfAnonymous(type); if (type.getKind() == TypeKind.EXECUTABLE) { // TODO: does not actualy work, attempt to solve situations like: // t = Collections.emptyList() // t = Collections.<String>emptyList(); // see also testCreateFieldMethod1 and testCreateFieldMethod2 tests: type = ((ExecutableType) type).getReturnType(); } } } if (at.getExpression() == error) { type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable())); } // class or field: if (type == null) { if (ErrorHintsProvider.ERR.isLoggable(ErrorManager.INFORMATIONAL)) { ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "offset=" + offset); ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "errorTree=" + error); ErrorHintsProvider.ERR.log(ErrorManager.INFORMATIONAL, "type=null"); } return null; } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(type); }
private static List<? extends TypeMirror> computeBinaryOperator( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { BinaryTree bt = (BinaryTree) parent.getLeaf(); TreePath typeToResolve = null; if (bt.getLeftOperand() == error) { typeToResolve = new TreePath(parent, bt.getRightOperand()); } if (bt.getRightOperand() == error) { typeToResolve = new TreePath(parent, bt.getLeftOperand()); } types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return typeToResolve != null ? Collections.singletonList(info.getTrees().getTypeMirror(typeToResolve)) : null; }
private static List<? extends TypeMirror> computeNewArray( Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) { NewArrayTree nat = (NewArrayTree) parent.getLeaf(); if (nat.getType() == error) { types.add(ElementKind.CLASS); types.add(ElementKind.ENUM); types.add(ElementKind.INTERFACE); return null; } for (Tree dimension : nat.getDimensions()) { if (dimension == error) { types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(info.getTypes().getPrimitiveType(TypeKind.INT)); } } for (Tree init : nat.getInitializers()) { if (init == error) { TypeMirror whole = info.getTrees().getTypeMirror(parent); if (whole == null || whole.getKind() != TypeKind.ARRAY) return null; types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); return Collections.singletonList(((ArrayType) whole).getComponentType()); } } return null; }
private static List<? extends TypeMirror> computeMethod( Set<ElementKind> types, CompilationInfo info, TreePath parent, TypeMirror[] typeParameterBound, Tree error, int offset) { // class or field: // check the error is in the body: // #92419: check for abstract method/method without body: MethodTree mt = (MethodTree) parent.getLeaf(); if (mt.getReturnType() == error) { types.add(ElementKind.CLASS); types.add(ElementKind.INTERFACE); types.add(ElementKind.ENUM); } List<? extends ExpressionTree> throwList = mt.getThrows(); if (throwList != null && !throwList.isEmpty()) { for (ExpressionTree t : throwList) { if (t == error) { types.add(ElementKind.CLASS); typeParameterBound[0] = info.getElements().getTypeElement("java.lang.Exception").asType(); break; } } } if (mt.getBody() == null) { return null; } try { Document doc = info.getDocument(); if (doc != null) { // XXX int bodyStart = Utilities.findBodyStart( parent.getLeaf(), info.getCompilationUnit(), info.getTrees().getSourcePositions(), doc); int bodyEnd = (int) info.getTrees() .getSourcePositions() .getEndPosition(info.getCompilationUnit(), parent.getLeaf()); types.add(ElementKind.PARAMETER); types.add(ElementKind.LOCAL_VARIABLE); types.add(ElementKind.FIELD); if (bodyStart <= offset && offset <= bodyEnd) return Collections.singletonList( info.getElements().getTypeElement("java.lang.Object").asType()); } } catch (IOException ex) { Logger.getLogger("global").log(Level.INFO, ex.getMessage(), ex); } return null; }