Search in sources :

Example 46 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project azure-tools-for-java by Microsoft.

the class SDKJarsFilter method getClasspathEntriesOfAzureLibabries.

public IClasspathEntry[] getClasspathEntriesOfAzureLibabries(IPath containerPath) {
    String sdkID = "com.microsoft.azuretools.sdk";
    Bundle bundle = Platform.getBundle(sdkID);
    // Search the available SDKs
    Bundle[] bundles = Platform.getBundles(sdkID, null);
    List<IClasspathEntry> listEntries = new ArrayList<IClasspathEntry>();
    if (bundles != null) {
        for (Bundle bundle2 : bundles) {
            if (bundle2.getVersion().toString().startsWith(containerPath.segment(1))) {
                bundle = bundle2;
                break;
            }
        }
        // Get the SDK jar.
        URL sdkJar = FileLocator.find(bundle, new Path("azure-1.0.0.jar"), null);
        URL resSdkJar = null;
        IClasspathAttribute[] attr = null;
        try {
            if (sdkJar != null) {
                resSdkJar = FileLocator.resolve(sdkJar);
            // create classpath attribute for java doc, if present
            }
            if (resSdkJar == null) {
                /*
					 * if sdk jar is not present then create an place holder for
					 * sdk jar so that it would be shown as missing file
					 */
                URL bundleLoc = new URL(bundle.getLocation());
                StringBuffer strBfr = new StringBuffer(bundleLoc.getPath());
                strBfr.append(File.separator).append("azure-1.0.0.jar");
                URL jarLoc = new URL(strBfr.toString());
                IPath jarPath = new Path(FileLocator.resolve(jarLoc).getPath());
                File jarFile = jarPath.toFile();
                listEntries.add(JavaCore.newLibraryEntry(new Path(jarFile.getAbsolutePath()), null, null, null, attr, true));
            } else {
                File directory = new File(resSdkJar.getPath());
                // create the library entry for sdk jar
                listEntries.add(JavaCore.newLibraryEntry(new Path(directory.getAbsolutePath()), null, null, null, attr, true));
                FilenameFilter sdkJarsFilter = new SDKJarsFilter();
                File[] jars = new File(String.format("%s%s%s", directory.getParent(), File.separator, Messages.depLocation)).listFiles(sdkJarsFilter);
                for (int i = 0; i < jars.length; i++) {
                    listEntries.add(JavaCore.newLibraryEntry(new Path(jars[i].getAbsolutePath()), null, null, null, attr, true));
                }
            }
        } catch (Exception e) {
            listEntries = new ArrayList<IClasspathEntry>();
            Activator.getDefault().log(Messages.excp, e);
        }
    }
    IClasspathEntry[] entries = new IClasspathEntry[listEntries.size()];
    // Return the classpath entries.
    return listEntries.toArray(entries);
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) URL(java.net.URL) CoreException(org.eclipse.core.runtime.CoreException) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) FilenameFilter(java.io.FilenameFilter) File(java.io.File)

Example 47 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project azure-tools-for-java by Microsoft.

the class ClasspathContainerPage method configureClasspathEntries.

/**
     * Method adds entries into .classpath file.
     */
private void configureClasspathEntries() {
    IJavaProject proj1 = JavaCore.create(getSelectedProject());
    IClasspathEntry[] entries;
    try {
        entries = proj1.getRawClasspath();
        IClasspathEntry[] newentries = new IClasspathEntry[entries.length];
        for (int i = 0; i < entries.length; i++) {
            if (entries[i].toString().contains(Messages.containerID)) {
                if (depCheck.getSelection()) {
                    IClasspathAttribute[] attr = new IClasspathAttribute[1];
                    attr[0] = JavaCore.newClasspathAttribute(Messages.jstDep, "/WEB-INF/lib");
                    newentries[i] = JavaCore.newContainerEntry(entry, null, attr, true);
                } else {
                    newentries[i] = JavaCore.newContainerEntry(entry);
                }
            } else {
                newentries[i] = entries[i];
            }
        }
        proj1.setRawClasspath(newentries, null);
    } catch (Exception e) {
        Activator.getDefault().log(e.getMessage(), e);
    }
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry)

Example 48 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project lwjgl by LWJGL.

the class BuildPathSupport method getLWJGLLibraryEntries.

public static IClasspathEntry[] getLWJGLLibraryEntries() {
    IPath bundleBase = getBundleLocation(LWJGL_PLUGIN);
    if (bundleBase != null) {
        IClasspathEntry[] entries = new IClasspathEntry[JAR_FILES.length];
        for (int i = 0; i < JAR_FILES.length; i++) {
            //$NON-NLS-1$
            IPath jarLocation = bundleBase.append(JAR_FILES[i]);
            IPath srcLocation = getSourceLocation(SRC_FILES[i]);
            String nativeLocation = getNativeLocation();
            String javadocLocation = getJavadocLocation(DOC_FILES[i]);
            IAccessRule[] accessRules = {};
            IClasspathAttribute[] attributes = { // 
            JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation), JavaCore.newClasspathAttribute(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY, nativeLocation) };
            // return JavaCore.newClasspathAttribute(JavaRuntime.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY, dialog.getNativeLibraryPath());
            entries[i] = JavaCore.newLibraryEntry(jarLocation, srcLocation, null, accessRules, attributes, false);
        }
        return entries;
    }
    return null;
}
Also used : IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IAccessRule(org.eclipse.jdt.core.IAccessRule)

Example 49 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project bndtools by bndtools.

the class PackageDecorator method updateDecoration.

public static void updateDecoration(IProject project, Project model) throws Exception {
    if (!project.isOpen()) {
        return;
    }
    IJavaProject javaProject = JavaCore.create(project);
    if (javaProject == null) {
        // project is not a java project
        return;
    }
    boolean changed = false;
    for (IClasspathEntry cpe : javaProject.getRawClasspath()) {
        if (cpe.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            continue;
        }
        for (IPackageFragmentRoot pkgRoot : javaProject.findPackageFragmentRoots(cpe)) {
            assert pkgRoot.getKind() == IPackageFragmentRoot.K_SOURCE;
            IResource pkgRootResource = pkgRoot.getCorrespondingResource();
            if (pkgRootResource == null) {
                continue;
            }
            File pkgRootFile = pkgRootResource.getLocation().toFile();
            boolean pkgInSourcePath = model.getSourcePath().contains(pkgRootFile);
            for (IJavaElement child : pkgRoot.getChildren()) {
                IPackageFragment pkg = (IPackageFragment) child;
                assert pkg.getKind() == IPackageFragmentRoot.K_SOURCE;
                IResource pkgResource = pkg.getCorrespondingResource();
                if (pkgResource == null) {
                    continue;
                }
                String text = pkgResource.getPersistentProperty(packageDecoratorKey);
                if (pkgInSourcePath) {
                    String pkgName = pkg.getElementName();
                    // Decorate if exported package
                    Attrs pkgAttrs = model.getExports().getByFQN(pkgName);
                    if (pkgAttrs != null) {
                        StringBuilder sb = new StringBuilder(" ").append(Version.parseVersion(pkgAttrs.getVersion()));
                        pkgAttrs = model.getImports().getByFQN(pkgName);
                        if (pkgAttrs != null) {
                            String versionRange = pkgAttrs.getVersion();
                            if (versionRange != null) {
                                sb.append('↔').append(versionRange);
                            }
                        }
                        String version = sb.toString();
                        if (!version.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, version);
                            changed = true;
                        }
                        continue;
                    }
                    // Decorate if non-empty, non-contained package
                    if (pkg.containsJavaResources() && !model.getContained().containsFQN(pkgName)) {
                        if (!excluded.equals(text)) {
                            pkgResource.setPersistentProperty(packageDecoratorKey, excluded);
                            changed = true;
                        }
                        continue;
                    }
                }
                // Clear decoration
                if (text != null) {
                    pkgResource.setPersistentProperty(packageDecoratorKey, null);
                    changed = true;
                }
            }
        }
    }
    // If decoration change, update display
    if (changed) {
        Display display = PlatformUI.getWorkbench().getDisplay();
        SWTConcurrencyUtil.execForDisplay(display, true, new Runnable() {

            @Override
            public void run() {
                PlatformUI.getWorkbench().getDecoratorManager().update(packageDecoratorId);
            }
        });
    }
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Attrs(aQute.bnd.header.Attrs) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) File(java.io.File) IResource(org.eclipse.core.resources.IResource) Display(org.eclipse.swt.widgets.Display)

Example 50 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry 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)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)55 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ArrayList (java.util.ArrayList)35 Path (org.eclipse.core.runtime.Path)27 JavaModelException (org.eclipse.jdt.core.JavaModelException)23 IProject (org.eclipse.core.resources.IProject)20 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 CoreException (org.eclipse.core.runtime.CoreException)12 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)10 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)7