Search in sources :

Example 6 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class PackageFragmentRootWalker method traverse.

public T traverse(IPackageFragmentRoot root, boolean stopOnFirstResult) throws JavaModelException {
    T result = null;
    if (root.exists() && existsPhysically(root)) {
        Object[] resources = root.getNonJavaResources();
        TraversalState state = new TraversalState(root);
        for (Object object : resources) {
            if (object instanceof IJarEntryResource) {
                result = traverse((IJarEntryResource) object, stopOnFirstResult, state);
                if (stopOnFirstResult && result != null)
                    return result;
            }
        }
        IJavaElement[] children = root.getChildren();
        for (IJavaElement javaElement : children) {
            if (javaElement instanceof IPackageFragment) {
                result = traverse((IPackageFragment) javaElement, stopOnFirstResult, state);
                if (stopOnFirstResult && result != null)
                    return result;
            }
        }
    }
    return result;
}
Also used : IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Example 7 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class Storage2UriMapperJavaImpl method initializeData.

/**
 * @since 2.4
 */
protected PackageFragmentRootData initializeData(final IPackageFragmentRoot root) {
    final PackageFragmentRootData data = new PackageFragmentRootData(computeModificationStamp(root));
    data.addRoot(root);
    if (shouldHandle(root)) {
        try {
            final SourceAttachmentPackageFragmentRootWalker<Void> walker = new SourceAttachmentPackageFragmentRootWalker<Void>() {

                @Override
                protected URI getURI(IFile file, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                    if (!uriValidator.isPossiblyManaged(file))
                        return null;
                    return super.getURI(file, state);
                }

                @Override
                protected URI getURI(IJarEntryResource jarEntry, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                    if (!uriValidator.isPossiblyManaged(jarEntry))
                        return null;
                    final URI uri = locator.getURI(root, jarEntry, state);
                    if (!uriValidator.isValid(uri, jarEntry))
                        return null;
                    return uri;
                }

                @Override
                protected Void handle(URI uri, IStorage storage, org.eclipse.xtext.ui.resource.PackageFragmentRootWalker.TraversalState state) {
                    data.uri2Storage.put(uri, storage);
                    return null;
                }
            };
            walker.traverse(root, false);
            if (walker.getBundleSymbolicName() != null)
                data.uriPrefix = URI.createPlatformResourceURI(walker.getBundleSymbolicName() + "/", true);
        } catch (RuntimeException e) {
            log.error(e.getMessage(), e);
        } catch (JavaModelException e) {
            log.debug(e.getMessage(), e);
        }
    }
    return data;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IFile(org.eclipse.core.resources.IFile) IStorage(org.eclipse.core.resources.IStorage) URI(org.eclipse.emf.common.util.URI) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource)

Example 8 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project jbosstools-base by jbosstools.

the class ModelObjectStorageEditorInput method findJarEntryFile.

IJarEntryResource findJarEntryFile() {
    XModelObject o = object;
    JarEntryFile f = null;
    JarEntryResource current = null;
    String packageName = "";
    List<String> parts = new ArrayList<String>();
    List<XModelObject> os = new ArrayList<XModelObject>();
    while (o != null && o.getFileType() != XModelObject.SYSTEM) {
        String part = o.getFileType() == XModelObject.FILE ? FileAnyImpl.toFileName(o) : o.getFileType() == XModelObject.FOLDER ? o.getAttributeValue(XModelObjectConstants.ATTR_NAME) : null;
        if (part != null) {
            parts.add(0, part);
            os.add(0, o);
            if (f == null) {
                f = new JarEntryFile(part) {

                    public InputStream getContents() throws CoreException {
                        return storage.getContents();
                    }
                };
                current = f;
            } else {
                if (packageName.length() > 0) {
                    packageName = part + "." + packageName;
                } else {
                    packageName = part;
                }
                JarEntryDirectory d = new JarEntryDirectory(part);
                current.setParent(d);
                current = d;
            }
        }
        o = o.getParent();
    }
    // if(!(o instanceof JarSystemImpl)) return null;
    String file = Paths.expand(o.get(XModelObjectConstants.ATTR_NAME_LOCATION), o.getModel().getProperties());
    IProject p = EclipseResourceUtil.getProject(o);
    IJavaProject jp = EclipseResourceUtil.getJavaProject(p);
    if (jp == null)
        return null;
    IPackageFragmentRoot root = null;
    try {
        IPackageFragmentRoot[] rs = jp.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot r : rs) {
            if (r.getResource() != null && r.getResource().exists() && r.getResource().getLocation().toString().equals(file)) {
                root = r;
            } else if (r.getPath() != null && r.getPath().toString().equals(file)) {
                root = r;
            }
        }
    } catch (CoreException e) {
        ModelUIPlugin.getDefault().logError(e);
    }
    if (root == null) {
        root = jp.getPackageFragmentRoot(file);
    }
    if (root == null) {
        return null;
    }
    if (current != null && !"META-INF".equalsIgnoreCase(current.getName()) && packageName.length() > 0) {
        IPackageFragment pf = root.getPackageFragment(packageName);
        f.setParent(pf);
    } else {
        current.setParent(root);
        if (!(o instanceof JarSystemImpl)) {
            Object q = root;
            NonJavaResource nj = null;
            for (int i = 0; i < parts.size(); i++) {
                IResource ri = (IResource) os.get(i).getAdapter(IResource.class);
                if (ri == null) {
                    return f;
                }
                nj = new NonJavaResource(q, ri);
                q = nj;
            }
            if (nj != null) {
                return nj;
            }
        }
    }
    return f;
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) ArrayList(java.util.ArrayList) JarEntryResource(org.eclipse.jdt.internal.core.JarEntryResource) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) JarEntryDirectory(org.eclipse.jdt.internal.core.JarEntryDirectory) IJavaProject(org.eclipse.jdt.core.IJavaProject) NonJavaResource(org.eclipse.jdt.internal.core.NonJavaResource) JarEntryFile(org.eclipse.jdt.internal.core.JarEntryFile)

Example 9 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project quarkus-ls by redhat-developer.

the class QuarkusConfigRootProvider method getJavadoc.

/**
 * Returns the Javadoc from the given field. There are 3 strategies to extract
 * the Javadoc:
 *
 * <ul>
 * <li>try to extract Javadoc from the source (from '.java' source file or from
 * JAR which is linked to source).</li>
 * <li>try to get Javadoc from the attached Javadoc.</li>
 * <li>get Javadoc from the Quarkus properties file stored in JAR META-INF/
 * </ul>
 *
 * @param field        the field to process
 * @param javadocCache the Javadoc cache
 * @param monitor      the progress monitor
 * @return the doc entry for the field
 * @throws JavaModelException
 */
private static String getJavadoc(IField field, Map<IPackageFragmentRoot, Properties> javadocCache, IProgressMonitor monitor) throws JavaModelException {
    // TODO: get Javadoc from source anad attached doc by processing Javadoc tag as
    // markdown
    // Try to get javadoc from sources
    /*
		 * String javadoc = findJavadocFromSource(field); if (javadoc != null) { return
		 * javadoc; } // Try to get attached javadoc javadoc =
		 * field.getAttachedJavadoc(monitor); if (javadoc != null) { return javadoc; }
		 */
    // Try to get the javadoc inside the META-INF/quarkus-javadoc.properties of the
    // JAR
    IPackageFragmentRoot packageRoot = (IPackageFragmentRoot) field.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    Properties properties = javadocCache.get(packageRoot);
    if (properties == null) {
        properties = new Properties();
        javadocCache.put(packageRoot, properties);
        IJarEntryResource quarkusJavadocResource = findJavadocFromQuakusJavadocProperties(packageRoot);
        if (quarkusJavadocResource != null) {
            try {
                properties.load(quarkusJavadocResource.getContents());
            } catch (Exception e) {
                // TODO : log it
                e.printStackTrace();
            }
        }
    }
    if (properties.isEmpty()) {
        return null;
    }
    // The META-INF/quarkus-javadoc.properties stores Javadoc without $ . Ex:
    // io.quarkus.deployment.SslProcessor.SslConfig.native_=Enable native SSL
    // support.
    String fieldKey = field.getDeclaringType().getFullyQualifiedName() + "." + field.getElementName();
    // Here field key contains '$'
    // Ex : io.quarkus.deployment.SslProcessor$SslConfig.native_
    // replace '$' with '.'
    fieldKey = fieldKey.replace('$', '.');
    return properties.getProperty(fieldKey);
}
Also used : IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) Properties(java.util.Properties) JavaModelException(org.eclipse.jdt.core.JavaModelException) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot)

Example 10 with IJarEntryResource

use of org.eclipse.jdt.core.IJarEntryResource in project xtext-eclipse by eclipse.

the class PackageFragmentRootWalkerTest method testTraversePackageFragmentRoot.

@Test
public void testTraversePackageFragmentRoot() throws Exception {
    IJavaProject project = createJavaProject("foo");
    String jarName = "JarWalkerTest.jar";
    IFile file = project.getProject().getFile(jarName);
    file.create(getClass().getResourceAsStream(jarName), true, new NullProgressMonitor());
    addJarToClasspath(project, file);
    final Set<IPath> pathes = new HashSet<IPath>();
    PackageFragmentRootWalker<Void> walker = new PackageFragmentRootWalker<Void>() {

        @Override
        protected Void handle(IJarEntryResource jarEntry, TraversalState state) {
            pathes.add(jarEntry.getFullPath());
            return null;
        }
    };
    for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
        if (root.getElementName().equals(jarName))
            walker.traverse(root, false);
    }
    assertEquals(3, pathes.size());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) IFile(org.eclipse.core.resources.IFile) IPath(org.eclipse.core.runtime.IPath) PackageFragmentRootWalker(org.eclipse.xtext.ui.resource.PackageFragmentRootWalker) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) IJavaProject(org.eclipse.jdt.core.IJavaProject) IJarEntryResource(org.eclipse.jdt.core.IJarEntryResource) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

IJarEntryResource (org.eclipse.jdt.core.IJarEntryResource)20 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)11 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)10 IFile (org.eclipse.core.resources.IFile)6 IJavaElement (org.eclipse.jdt.core.IJavaElement)6 IStorage (org.eclipse.core.resources.IStorage)5 URI (org.eclipse.emf.common.util.URI)5 IResource (org.eclipse.core.resources.IResource)4 IPath (org.eclipse.core.runtime.IPath)4 IJavaProject (org.eclipse.jdt.core.IJavaProject)4 JavaModelException (org.eclipse.jdt.core.JavaModelException)3 Properties (java.util.Properties)2 IProject (org.eclipse.core.resources.IProject)2 CoreException (org.eclipse.core.runtime.CoreException)2 Path (org.eclipse.core.runtime.Path)2 ITypeRoot (org.eclipse.jdt.core.ITypeRoot)2 Javadoc (org.eclipse.jdt.core.dom.Javadoc)2 IStorageEditorInput (org.eclipse.ui.IStorageEditorInput)2 ForwardingMap (com.google.common.collect.ForwardingMap)1 UserTagInfo (com.redhat.qute.commons.usertags.UserTagInfo)1