Search in sources :

Example 1 with MarkerData

use of org.bndtools.build.api.MarkerData in project bndtools by bndtools.

the class BundleVersionErrorHandler method generateMarkerData.

@Override
public List<MarkerData> generateMarkerData(IProject project, Project model, Location location) throws Exception {
    List<MarkerData> result = new LinkedList<MarkerData>();
    IFile bndFile = null;
    LineLocation loc = null;
    BundleInfo info = (BundleInfo) location.details;
    try (ProjectBuilder pb = model.getBuilder(null)) {
        for (Builder builder : pb.getSubBuilders()) {
            if (builder.getBsn().equals(info.bsn)) {
                File propsFile = builder.getPropertiesFile();
                // Try to find in the sub-bundle file
                if (propsFile != null) {
                    bndFile = project.getWorkspace().getRoot().getFileForLocation(new Path(propsFile.getAbsolutePath()));
                    if (bndFile != null) {
                        loc = findBundleVersionHeader(bndFile);
                    }
                }
                if (loc == null) {
                    // Not found in sub-bundle file, try bnd.bnd
                    bndFile = project.getFile(Project.BNDFILE);
                    loc = findBundleVersionHeader(bndFile);
                }
                if (loc == null) {
                    // Not found in bnd.bnd, try build.bnd. Marker will appear on bnd.bnd
                    IFile buildFile = Central.getWorkspaceBuildFile();
                    loc = findBundleVersionHeader(buildFile);
                    if (loc != null) {
                        loc = new LineLocation();
                        loc.lineNum = 1;
                        loc.start = 1;
                        loc.end = 1;
                    }
                }
                if (loc == null) {
                    // Not found in build.bnd, try included files. Marker will appear on bnd.bnd
                    List<File> extensions = Central.getWorkspace().getIncluded();
                    if (extensions != null) {
                        for (File extension : extensions) {
                            loc = findBundleVersionHeader(Central.toResource(extension));
                            if (loc != null) {
                                loc = new LineLocation();
                                loc.lineNum = 1;
                                loc.start = 1;
                                loc.end = 1;
                                break;
                            }
                        }
                    }
                }
                if (loc != null) {
                    Map<String, Object> attribs = new HashMap<String, Object>();
                    attribs.put(IMarker.MESSAGE, location.message);
                    attribs.put(IMarker.LINE_NUMBER, loc.lineNum);
                    attribs.put(IMarker.CHAR_START, loc.start);
                    attribs.put(IMarker.CHAR_END, loc.end);
                    String qualifier = null;
                    String currentVersion = builder.getUnprocessedProperty(Constants.BUNDLE_VERSION, "");
                    if (currentVersion != null) {
                        Matcher m = VERSION_ACCEPTING_MACRO.matcher(currentVersion);
                        if (m.matches()) {
                            qualifier = m.group(4);
                        }
                    }
                    attribs.put(PROP_SUGGESTED_VERSION, info.suggestedVersion.toString() + (qualifier != null ? '.' + qualifier : ""));
                    result.add(new MarkerData(bndFile, attribs, true, BndtoolsConstants.MARKER_JAVA_BASELINE));
                }
            }
        }
    }
    return result;
}
Also used : Path(org.eclipse.core.runtime.Path) MarkerData(org.bndtools.build.api.MarkerData) IFile(org.eclipse.core.resources.IFile) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ProjectBuilder(aQute.bnd.build.ProjectBuilder) Builder(aQute.bnd.osgi.Builder) LinkedList(java.util.LinkedList) BundleInfo(aQute.bnd.differ.Baseline.BundleInfo) ProjectBuilder(aQute.bnd.build.ProjectBuilder) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Example 2 with MarkerData

use of org.bndtools.build.api.MarkerData in project bndtools by bndtools.

the class BaselineErrorHandler method generateMarkerData.

@Override
public List<MarkerData> generateMarkerData(IProject project, Project model, Location location) throws Exception {
    List<MarkerData> result = new LinkedList<MarkerData>();
    Info baselineInfo = (Info) location.details;
    IJavaProject javaProject = JavaCore.create(project);
    result.addAll(generatePackageInfoMarkers(baselineInfo, javaProject, location.message));
    result.addAll(generateStructuralChangeMarkers(baselineInfo, javaProject));
    return result;
}
Also used : MarkerData(org.bndtools.build.api.MarkerData) IJavaProject(org.eclipse.jdt.core.IJavaProject) Info(aQute.bnd.differ.Baseline.Info) LinkedList(java.util.LinkedList)

Example 3 with MarkerData

use of org.bndtools.build.api.MarkerData in project bndtools by bndtools.

the class BaselineErrorHandler method generateRemovedMethodMarker.

List<MarkerData> generateRemovedMethodMarker(IJavaProject javaProject, final String className, final String methodName, final Delta requiresDelta) throws JavaModelException {
    final List<MarkerData> markers = new LinkedList<MarkerData>();
    final CompilationUnit ast = createAST(javaProject, className);
    if (ast != null) {
        ast.accept(new ASTVisitor() {

            @Override
            public boolean visit(TypeDeclaration typeDecl) {
                ITypeBinding typeBinding = typeDecl.resolveBinding();
                if (typeBinding != null) {
                    if (typeBinding.getBinaryName().equals(className)) {
                        Map<String, Object> attribs = new HashMap<String, Object>();
                        SimpleName nameNode = typeDecl.getName();
                        attribs.put(IMarker.CHAR_START, nameNode.getStartPosition());
                        attribs.put(IMarker.CHAR_END, nameNode.getStartPosition() + nameNode.getLength());
                        String message = String.format("The method '%s' was removed, which requires a %s change to the package.", methodName, requiresDelta);
                        attribs.put(IMarker.MESSAGE, message);
                        markers.add(new MarkerData(ast.getJavaElement().getResource(), attribs, false));
                        return false;
                    }
                }
                return true;
            }
        });
    }
    return markers;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) MarkerData(org.bndtools.build.api.MarkerData) SimpleName(org.eclipse.jdt.core.dom.SimpleName) LinkedList(java.util.LinkedList) ASTVisitor(org.eclipse.jdt.core.dom.ASTVisitor) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) Map(java.util.Map) HashMap(java.util.HashMap)

Example 4 with MarkerData

use of org.bndtools.build.api.MarkerData in project bndtools by bndtools.

the class BaselineErrorHandler method generatePackageInfoMarkers.

List<MarkerData> generatePackageInfoMarkers(Info baselineInfo, IJavaProject javaProject, String message) throws JavaModelException {
    List<MarkerData> markers = new LinkedList<>();
    for (IClasspathEntry entry : javaProject.getRawClasspath()) {
        if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
            IPath entryPath = entry.getPath();
            IPath pkgPath = entryPath.append(baselineInfo.packageName.replace('.', '/'));
            // Find in packageinfo file
            IPath pkgInfoPath = pkgPath.append(PACKAGEINFO);
            IFile pkgInfoFile = javaProject.getProject().getWorkspace().getRoot().getFile(pkgInfoPath);
            if (pkgInfoFile != null && pkgInfoFile.exists()) {
                Map<String, Object> attribs = new HashMap<String, Object>();
                attribs.put(IMarker.MESSAGE, message.trim());
                attribs.put(PROP_SUGGESTED_VERSION, baselineInfo.suggestedVersion.toString());
                LineLocation lineLoc = findVersionLocation(pkgInfoFile.getLocation().toFile());
                if (lineLoc != null) {
                    attribs.put(IMarker.LINE_NUMBER, lineLoc.lineNum);
                    attribs.put(IMarker.CHAR_START, lineLoc.start);
                    attribs.put(IMarker.CHAR_END, lineLoc.end);
                }
                markers.add(new MarkerData(pkgInfoFile, attribs, true));
            }
            // Find in package-info.java
            IPackageFragment pkg = javaProject.findPackageFragment(pkgPath);
            if (pkg != null) {
                ICompilationUnit pkgInfoJava = pkg.getCompilationUnit(PACKAGEINFOJAVA);
                if (pkgInfoJava != null && pkgInfoJava.exists()) {
                    ISourceRange range = findPackageInfoJavaVersionLocation(baselineInfo.packageName, pkgInfoJava);
                    Map<String, Object> attribs = new HashMap<String, Object>();
                    attribs.put(IMarker.MESSAGE, message.trim());
                    attribs.put(IJavaModelMarker.ID, 8088);
                    attribs.put(PROP_SUGGESTED_VERSION, baselineInfo.suggestedVersion.toString());
                    if (range != null) {
                        attribs.put(IMarker.CHAR_START, range.getOffset());
                        attribs.put(IMarker.CHAR_END, range.getOffset() + range.getLength());
                        markers.add(new MarkerData(pkgInfoJava.getResource(), attribs, true, BndtoolsConstants.MARKER_JAVA_BASELINE));
                    }
                }
            }
        }
    }
    return markers;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) MarkerData(org.bndtools.build.api.MarkerData) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 5 with MarkerData

use of org.bndtools.build.api.MarkerData in project bndtools by bndtools.

the class BaselineErrorHandler method generateStructuralChangeMarkers.

List<MarkerData> generateStructuralChangeMarkers(Info baselineInfo, IJavaProject javaProject) throws JavaModelException {
    List<MarkerData> markers = new LinkedList<MarkerData>();
    Delta packageDelta = baselineInfo.packageDiff.getDelta();
    // Iterate into the package member diffs
    for (Diff pkgMemberDiff : baselineInfo.packageDiff.getChildren()) {
        // Skip deltas that have lesser significance than the overall package delta
        if (pkgMemberDiff.getDelta().ordinal() < packageDelta.ordinal())
            continue;
        if (Delta.ADDED == pkgMemberDiff.getDelta()) {
            @SuppressWarnings("unused") Tree pkgMember = pkgMemberDiff.getNewer();
        // markers.addAll(generateAddedTypeMarker(javaProject, pkgMember.getName(), pkgMember.ifAdded()));
        } else if (Delta.REMOVED == pkgMemberDiff.getDelta()) {
        } else {
            Tree pkgMember = pkgMemberDiff.getOlder();
            if (pkgMember != null && (Type.INTERFACE == pkgMember.getType() || Type.CLASS == pkgMember.getType())) {
                String className = pkgMember.getName();
                // Iterate into the class member diffs
                for (Diff classMemberDiff : pkgMemberDiff.getChildren()) {
                    // Skip deltas that have lesser significance than the overall package delta (again)
                    if (classMemberDiff.getDelta().ordinal() < packageDelta.ordinal())
                        continue;
                    if (Delta.ADDED == classMemberDiff.getDelta()) {
                        Tree classMember = classMemberDiff.getNewer();
                        if (Type.METHOD == classMember.getType())
                            markers.addAll(generateAddedMethodMarker(javaProject, className, classMember.getName(), classMember.ifAdded()));
                    } else if (Delta.REMOVED == classMemberDiff.getDelta()) {
                        Tree classMember = classMemberDiff.getOlder();
                        if (Type.METHOD == classMember.getType()) {
                            markers.addAll(generateRemovedMethodMarker(javaProject, className, classMember.getName(), classMember.ifRemoved()));
                        }
                    }
                }
            }
        }
    }
    return markers;
}
Also used : MarkerData(org.bndtools.build.api.MarkerData) Delta(aQute.bnd.service.diff.Delta) Diff(aQute.bnd.service.diff.Diff) Tree(aQute.bnd.service.diff.Tree) LinkedList(java.util.LinkedList)

Aggregations

MarkerData (org.bndtools.build.api.MarkerData)9 LinkedList (java.util.LinkedList)6 HashMap (java.util.HashMap)5 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 IFile (org.eclipse.core.resources.IFile)2 ASTVisitor (org.eclipse.jdt.core.dom.ASTVisitor)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 Project (aQute.bnd.build.Project)1 ProjectBuilder (aQute.bnd.build.ProjectBuilder)1 Workspace (aQute.bnd.build.Workspace)1 DeclarativeServicesAnnotationError (aQute.bnd.component.error.DeclarativeServicesAnnotationError)1 BundleInfo (aQute.bnd.differ.Baseline.BundleInfo)1 Info (aQute.bnd.differ.Baseline.Info)1 Builder (aQute.bnd.osgi.Builder)1 BundleActivatorError (aQute.bnd.osgi.Verifier.BundleActivatorError)1 Delta (aQute.bnd.service.diff.Delta)1 Diff (aQute.bnd.service.diff.Diff)1