有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    使用getPackageFragmentRoots()获取类路径中的等价项。对于每个根,您可以调用getNonJavaResources()来获取该根下的非java内容,并且可以递归调用getChildren()来获取子级(以java层次结构的方式)。最终,这些间接遍历的子对象将是java源文件,您可以通过向它们发送getUnderlyingResource()方法来确认这些源文件

    下面是一些代码:

    private Collection<String> keys( IJavaProject project, String[] bundleNames ) throws CoreException, IOException {
    
        Set<String> keys = Sets.newLinkedHashSet();
    
        for( String bundleName : bundleNames ) {
    
            IPath path = new Path( toResourceName( bundleName ) );
    
            boolean found = false;
    
            IPackageFragmentRoot[] packageFragmentRoots = project.getPackageFragmentRoots();
            for( IPackageFragmentRoot root : packageFragmentRoots ) {
                found |= collectKeys( root, path, keys );
            }
    
            if( ! found ) {
                throw new BundleNotFoundException( bundleName );
            }
        }
    
        return keys;
    }
    
    private boolean collectKeys( IPackageFragmentRoot root, IPath path, Set<String> keys ) throws CoreException, IOException {
        IPath fullPath = root.getPath().append( path );
        System.out.println( "fullPath=" + fullPath );
    
        IFile file = root.getJavaProject().getProject().getFile( fullPath.removeFirstSegments( 1 ) );
        System.out.println( "file=" + fullPath );
    
        if( ! file.exists() ) {
            return false;
        }
    
        log.debug( "Loading " + file );
    
        InputStream stream = file.getContents( true );
        try {
            Properties p = load( file.getFullPath().toString(), stream );
    
            keys.addAll( keySet( p ) );
        } finally {
            stream.close();
        }
    
        return true;
    }
    
    protected String toResourceName( String bundleKey ) {
    
        String path = bundleKey.replace( '.', '/' );
        return path + ".properties";
    }