Java Code Examples for org.netbeans.api.java.source.CompilationInfo#getDiagnostics()
The following examples show how to use
org.netbeans.api.java.source.CompilationInfo#getDiagnostics() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RemoveInvalidModifier.java From netbeans with Apache License 2.0 | 5 votes |
/** * Returns the diagnostics entry * @param compilationInfo * @param start * @param codes * @return */ private Diagnostic getDiagnostic(CompilationInfo compilationInfo, long start, Set<String> errorCodes) { Diagnostic result = null; for (Diagnostic d : compilationInfo.getDiagnostics()) { if (start != d.getStartPosition()) { continue; } if (!errorCodes.contains(d.getCode())) { continue; } result=d; } return result; }
Example 2
Source File: HintTest.java From netbeans with Apache License 2.0 | 5 votes |
private void ensureCompilable(FileObject file) throws IOException, AssertionError, IllegalArgumentException { CompilationInfo info = parse(file); assertNotNull(info); for (Diagnostic d : info.getDiagnostics()) { if (d.getKind() == Diagnostic.Kind.ERROR) throw new AssertionError(d.getLineNumber() + ":" + d.getColumnNumber() + " " + d.getMessage(null)); } }
Example 3
Source File: HintTestBase.java From netbeans with Apache License 2.0 | 5 votes |
private void ensureCompilable(FileObject file) throws IOException, AssertionError, IllegalArgumentException { CompilationInfo info = parse(file); assertNotNull(info); for (Diagnostic d : info.getDiagnostics()) { if (d.getKind() == Diagnostic.Kind.ERROR) throw new AssertionError(d.getLineNumber() + ":" + d.getColumnNumber() + " " + d.getMessage(null)); } }
Example 4
Source File: BiAnalyser.java From netbeans with Apache License 2.0 | 4 votes |
/** Creates Bean Info analyser which contains all patterns from PatternAnalyser */ BiAnalyser ( PatternAnalyser pa, CompilationInfo javac ) throws GenerateBeanException { int index; this.isBeanBroken = false; for (Diagnostic d : javac.getDiagnostics()) { isBeanBroken |= d.getKind() == Diagnostic.Kind.ERROR; } // Try to find and analyse existing bean info bis = new BeanInfoSource( pa.getFileObject() ); olderVersion = (bis.isNbBeanInfo() && bis.getMethodsSection() == null); superClassVersion = (bis.isNbSuperclass() || !bis.exists()); TypeElement classElement = pa.getClassElementHandle().resolve(javac); this.classfqn = classElement.getQualifiedName().toString(); // Fill Descriptor list (only in case we have new templates) descriptor = new ArrayList<BiFeature.Descriptor>(); descriptor.add(new BiFeature.Descriptor(classElement, this)); // Fill methods list (only in case we have new templates) methods = new ArrayList<BiFeature.Method>(); if (!olderVersion) { for (ExecutableElement method : BeanUtils.methodsIn(classElement, javac)) { methods.add(new BiFeature.Method(method, pa, javac, this)); } } // Fill properties list List<PropertyPattern> propertyPatterns = pa.getPropertyPatterns(); properties = new ArrayList<BiFeature.Property>(propertyPatterns.size()); for (PropertyPattern pp : propertyPatterns) { properties.add(new BiFeature.Property(pp, javac, this)); removeMethods(methods, pp.getGetterMethod(), pp.getSetterMethod()); } // Fill indexed properties list List<IdxPropertyPattern> idxPropertyPatterns = pa.getIdxPropertyPatterns(); idxProperties = new ArrayList<BiFeature.IdxProperty>(idxPropertyPatterns.size()); for (IdxPropertyPattern ipp : idxPropertyPatterns) { TypeMirror type = ipp.getType().resolve(javac); TypeMirror idxtype = ipp.getIndexedType().resolve(javac); if (type.getKind() != TypeKind.ARRAY || !javac.getTypes().isSameType(((ArrayType) type).getComponentType(), idxtype)) { continue; } idxProperties.add(new BiFeature.IdxProperty(ipp, javac, this)); removeMethods(methods, ipp.getGetterMethod(), ipp.getSetterMethod(), ipp.getIndexedGetterMethod(), ipp.getIndexedSetterMethod()); } // Fill event sets list List<EventSetPattern> eventSetPatterns = pa.getEventSetPatterns(); eventSets = new ArrayList<BiFeature.EventSet>(eventSetPatterns.size()); for (EventSetPattern esp : eventSetPatterns) { eventSets.add(new BiFeature.EventSet(esp, javac, this)); removeMethods(methods, esp.getAddListenerMethod(), esp.getRemoveListenerMethod()); } Collections.sort(methods); try { isUpdateMode = false; analyzeBeanInfoSource(); } finally { isUpdateMode = true; } }
Example 5
Source File: ImplementAllAbstractMethods.java From netbeans with Apache License 2.0 | 4 votes |
public List<Fix> run(final CompilationInfo info, String diagnosticKey, final int offset, TreePath treePath, Data<Object> data) { TreePath path = deepTreePath(info, offset); if (path == null) { return null; } Map<Tree, Object> holder = data == null ? null : (Map)data.getData(); Object saved = null; if (holder != null) { saved = holder.get(path.getLeaf()); } if (Boolean.TRUE == saved) { return null; } Element e = info.getTrees().getElement(path); final Tree leaf = path.getLeaf(); boolean isUsableElement = e != null && (e.getKind().isClass() || e.getKind().isInterface()); boolean containsDefaultMethod = saved == Boolean.FALSE; boolean completingAnonymous = e != null && e.getKind() == ElementKind.CONSTRUCTOR && leaf.getKind() == Tree.Kind.NEW_CLASS; TypeElement tel = findTypeElement(info, path); if (!Utilities.isValidElement(tel)) { return null; } List<Fix> fixes = new ArrayList<>(); if (TreeUtilities.CLASS_TREE_KINDS.contains(leaf.getKind()) || leaf.getKind().toString().equals(RECORD)) { CompilationUnitTree cut = info.getCompilationUnit(); // do not offer for class declarations without body long start = info.getTrees().getSourcePositions().getStartPosition(cut, leaf); long end = info.getTrees().getSourcePositions().getEndPosition(cut, leaf); for (Diagnostic d : info.getDiagnostics()) { long position = d.getPosition(); if (d.getCode().equals(PREMATURE_EOF_CODE) && position > start && position < end) { return null; } } } if (completingAnonymous) { //if the parent of path.getLeaf is an error, the situation probably is like: //new Runnable {} //(missing '()' for constructor) //do not propose the hint in this case: final boolean[] parentError = new boolean[] {false}; new ErrorAwareTreePathScanner() { @Override public Object visitNewClass(NewClassTree nct, Object o) { if (leaf == nct) { parentError[0] = getCurrentPath().getParentPath().getLeaf().getKind() == Kind.ERRONEOUS; } return super.visitNewClass(nct, o); } }.scan(path.getParentPath(), null); if (parentError[0]) { // ignore return null; } fixes.add(new ImplementAbstractMethodsFix(info, path, tel, containsDefaultMethod)); } boolean someAbstract = false; X: if (isUsableElement) { for (ExecutableElement ee : ElementFilter.methodsIn(e.getEnclosedElements())) { if (ee.getModifiers().contains(Modifier.ABSTRACT)) { // make class abstract. In case of enums, suggest to implement the // abstract methods on all enum values. if (e.getKind() == ElementKind.ENUM) { // cannot make enum abstract, but can generate abstract methods skeleton // to all enum members fixes.add(new ImplementOnEnumValues2(info, tel, containsDefaultMethod)); // avoid other possible fixes: break X; } else if (e.getKind().isClass()) { someAbstract = true; break; } } } // offer to fix all abstract methods if (!someAbstract) { fixes.add(new ImplementAbstractMethodsFix(info, path, tel, containsDefaultMethod)); } if (e.getKind() == ElementKind.CLASS && e.getSimpleName() != null && !e.getSimpleName().contentEquals("")) { fixes.add(new MakeAbstractFix(info, path, e.getSimpleName().toString()).toEditorFix()); } } if (e != null && e.getKind() == ElementKind.ENUM_CONSTANT) { fixes.add(new ImplementAbstractMethodsFix(info, path, tel, containsDefaultMethod)); } return fixes; }
Example 6
Source File: CreateSubclass.java From netbeans with Apache License 2.0 | 4 votes |
@TriggerTreeKind({Tree.Kind.CLASS, Tree.Kind.INTERFACE}) public static ErrorDescription check(HintContext context) { TreePath tp = context.getPath(); ClassTree cls = (ClassTree) tp.getLeaf(); CompilationInfo info = context.getInfo(); SourcePositions sourcePositions = info.getTrees().getSourcePositions(); long startPos = sourcePositions.getStartPosition(tp.getCompilationUnit(), cls); if (startPos > Integer.MAX_VALUE) { return null; } int[] bodySpan = info.getTreeUtilities().findBodySpan(cls); if (bodySpan == null || bodySpan[0] <= startPos) { return null; } int caret = context.getCaretLocation(); if (startPos < 0 || caret < 0 || caret < startPos || caret >= bodySpan[0]) { return null; } // #222487 // If there is a compile-time error on the class, then don't offer to // create a subclass. List<Diagnostic> errors = info.getDiagnostics(); if (!errors.isEmpty()) { for (Diagnostic d : errors) { if (d.getKind() != Diagnostic.Kind.ERROR) { continue; } // Check that the error's start position is within the class header // Note: d.getEndPosition() is not used because, for example, // a "compiler.err.does.not.override.abstract" error ends at // the end of the class tree. if (startPos <= d.getStartPosition() && d.getStartPosition() <= bodySpan[0]) { return null; } } } TypeElement typeElement = (TypeElement) info.getTrees().getElement(tp); if (typeElement == null || typeElement.getModifiers().contains(Modifier.FINAL)) return null; Element outer = typeElement.getEnclosingElement(); // do not offer the hint for non-static inner classes. Permit for classes nested into itnerface - no enclosing instance if (outer != null && outer.getKind() != ElementKind.PACKAGE && outer.getKind() != ElementKind.INTERFACE) { if (outer.getKind() != ElementKind.CLASS && outer.getKind() != ElementKind.ENUM) { return null; } if (!typeElement.getModifiers().contains(Modifier.STATIC)) { return null; } } ClassPath cp = info.getClasspathInfo().getClassPath(PathKind.SOURCE); FileObject root = cp.findOwnerRoot(info.getFileObject()); if (root == null) { //File not part of any project return null; } PackageElement packageElement = (PackageElement) info.getElementUtilities().outermostTypeElement(typeElement).getEnclosingElement(); CreateSubclassFix fix = new CreateSubclassFix(info, root, packageElement.getQualifiedName().toString(), typeElement.getSimpleName().toString() + "Impl", typeElement); //NOI18N return ErrorDescriptionFactory.forTree(context, context.getPath(), NbBundle.getMessage(CreateSubclass.class, typeElement.getKind() == ElementKind.CLASS ? typeElement.getModifiers().contains(Modifier.ABSTRACT) ? "ERR_ImplementAbstractClass" : "ERR_CreateSubclass" : "ERR_ImplementInterface"), fix); //NOI18N }