[hg] main-silver: #201453: Only public packages from non-impleme...

  • From: Martin Kozeny < >
  • To:
  • Subject: [hg] main-silver: #201453: Only public packages from non-impleme...
  • Date: Thu, 21 Feb 2013 19:38:27 -0800

changeset 2f22116ce20e in main-silver ((none))
details: http://hg.netbeans.org/main-silver/rev/2f22116ce20e
description:
        #201453: Only public packages from non-implementation dependencies 
are accessible without warning in the NetBeans Module Project.

diffstat:

 
apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/ProjectWhiteListQueryImplementation.java
 |  89 ++++++---
 1 files changed, 55 insertions(+), 34 deletions(-)

diffs (201 lines):

diff --git 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/ProjectWhiteListQueryImplementation.java
 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/ProjectWhiteListQueryImplementation.java
--- 
a/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/ProjectWhiteListQueryImplementation.java
+++ 
b/apisupport.ant/src/org/netbeans/modules/apisupport/project/queries/ProjectWhiteListQueryImplementation.java
@@ -45,22 +45,28 @@
 import java.io.InputStream;
 import java.lang.ref.SoftReference;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 import java.util.StringTokenizer;
 import java.util.TreeSet;
 import java.util.jar.Attributes;
 import java.util.jar.Manifest;
+import javax.swing.event.ChangeEvent;
 import javax.swing.event.ChangeListener;
+import org.netbeans.api.annotations.common.NonNull;
 import org.netbeans.api.java.classpath.ClassPath;
 import org.netbeans.api.java.source.ElementHandle;
 import org.netbeans.api.whitelist.WhiteListQuery;
 import org.netbeans.modules.apisupport.project.ModuleDependency;
 import org.netbeans.modules.apisupport.project.NbModuleProject;
 import org.netbeans.modules.apisupport.project.ProjectXMLManager;
-import org.netbeans.spi.project.ProjectServiceProvider;
 import org.netbeans.spi.whitelist.WhiteListQueryImplementation;
 import org.openide.filesystems.FileObject;
+import org.openide.util.RequestProcessor;
+import org.openide.util.WeakSet;
 
 /**
  *
@@ -73,6 +79,8 @@
     private SoftReference<TreeSet<String>> cachedPrivatePackages;
     private boolean isCached = false;
     private static final WhiteListQuery.Result OK = new 
WhiteListQuery.Result();
+    private final Set<ProjectWhiteListImplementation> results = 
Collections.synchronizedSet(new WeakSet<ProjectWhiteListImplementation>());
+    private static final RequestProcessor RP = new 
RequestProcessor(ProjectWhiteListQueryImplementation.class.getName(), 3);
 
     /**
      * Constructor
@@ -91,9 +99,8 @@
     public WhiteListImplementation getWhiteList(FileObject file) {
 
         ProjectXMLManager pxm = new ProjectXMLManager(this.project);
-        Attributes projAttrs = 
this.project.getManifest().getMainAttributes();
         
-        if (projAttrs == null || projAttrs.getValue("Enable-Whitelist") == 
null || !projAttrs.getValue("Enable-Whitelist").equals("true")) {
+        if (System.getProperty("Enable-Whitelist") == null || 
!System.getProperty("Enable-Whitelist").equals("true")) {
             return null;
         }
         
@@ -109,40 +116,34 @@
             isCached = true;
         }
         
+        fireChangeAllExistingResults(privatePackages);
         
-        return new ProjectWhiteListImplementation(privatePackages);
-        //final TreeSet<String> privatePackageList = privatePackages;
+        ProjectWhiteListImplementation pwi = new 
ProjectWhiteListImplementation(privatePackages);
+        results.add(pwi);
 
-        /*return new WhiteListImplementation() {
-            @Override
-            public WhiteListQuery.Result check(ElementHandle<?> element, 
WhiteListQuery.Operation operation) {
-                if (!operation.equals(WhiteListQuery.Operation.USAGE)) {
-                    return OK;
+        return pwi;
                 }
 
-                if (element != null && (element.getKind().isClass() || 
element.getKind().isInterface())) {
-                    String qualifiedName = element.getQualifiedName();
-                    if (qualifiedName!=null && 
qualifiedName.lastIndexOf(".") > 0) {
-                        qualifiedName = qualifiedName.substring(0, 
qualifiedName.lastIndexOf("."));
-                        if (privatePackageList.contains(qualifiedName)) {
-                            List<WhiteListQuery.RuleDescription> descs = new 
ArrayList<WhiteListQuery.RuleDescription>();
-                            descs.add(new 
WhiteListQuery.RuleDescription("Private package dependency access", "Element 
comes from private package of spec version dependency", null));
-                            return new WhiteListQuery.Result(descs);
-                        }
-                    }
-                }
-                return OK;
+    private void fireChangeAllExistingResults(final TreeSet<String> 
privatePackages) {
+            final Set<ProjectWhiteListImplementation> set;
+            synchronized (results) {
+                set = new HashSet(results);
             }
 
+            RP.post(new Runnable() {
+
             @Override
-            public void addChangeListener(ChangeListener listener) {
+                 public void run() {
+                    for (ProjectWhiteListImplementation res : set) {
+                        if (res != null) {
+                            res.changeData(privatePackages);
+                        }
+                    }
+                }
+            });
             }
 
-            @Override
-            public void removeChangeListener(ChangeListener listener) {
-            }
-        };*/
-    }
+
 
     private Manifest getManifest(FileObject root) {
         FileObject manifestFo = root.getFileObject("META-INF/MANIFEST.MF");
@@ -215,6 +216,9 @@
                 Attributes attrs = mf.getMainAttributes();
 
                 publicPackagesStr = 
attrs.getValue("OpenIDE-Module-Public-Packages");
+                if(publicPackagesStr != null && 
!"".equals(publicPackagesStr)) {
+                    publicPackagesStr = publicPackagesStr.replaceAll(" ", 
"");
+                }
 
                 if (publicPackagesStr != null && 
!"-".equals(publicPackagesStr)) {
                     StringTokenizer tokenizer = new 
StringTokenizer(publicPackagesStr, ", ");
@@ -239,11 +243,8 @@
 
         for (String allPkgIter : allPackages) {
             boolean contains = false;
-            System.out.println("All pkg iter: "+allPkgIter);
             for (String publicPkgIter : eqPublicPackages) {
-                System.out.println("Public eq pkg iter: "+allPkgIter);
                 if (allPkgIter.equals(publicPkgIter)) {
-                    System.out.println("Not added");
                     contains = true;
                     break;
                 }
@@ -251,9 +252,7 @@
 
             if (!contains) {
                 for (String publicPkgIter : subPublicPackages) {
-                    System.out.println("Public sub pkg iter: "+allPkgIter);
                     if (allPkgIter.startsWith(publicPkgIter)) {
-                        System.out.println("Not added");
                         contains = true;
                         break;
                     }
@@ -280,6 +279,10 @@
         
         private TreeSet<String> privatePackages;
         
+        private final List<ChangeListener> listeners = new 
ArrayList<ChangeListener>();
+        
+        private final Object IMPL_LOCK = new Object();
+        
         public ProjectWhiteListImplementation(TreeSet<String> 
privatePackages)
         {
             this.privatePackages = privatePackages;
@@ -295,6 +298,7 @@
                     String qualifiedName = element.getQualifiedName();
                     if (qualifiedName!=null && 
qualifiedName.lastIndexOf(".") > 0) {
                         qualifiedName = qualifiedName.substring(0, 
qualifiedName.lastIndexOf("."));
+                        synchronized (IMPL_LOCK) {
                         if (privatePackages.contains(qualifiedName)) {
                             List<WhiteListQuery.RuleDescription> descs = new 
ArrayList<WhiteListQuery.RuleDescription>();
                             descs.add(new 
WhiteListQuery.RuleDescription("Private package dependency access", "Element 
comes from private package of spec version dependency", null));
@@ -302,18 +306,35 @@
                         }
                     }
                 }
+                }
                 return OK;
         }
 
         @Override
         public void addChangeListener(ChangeListener listener) {
-            
+            this.listeners.add(listener);
         }
 
         @Override
         public void removeChangeListener(ChangeListener listener) {
+            this.listeners.remove(listener);
+        }
             
+        public void changeData(@NonNull TreeSet<String> privatePackages) {
+            synchronized (IMPL_LOCK) {
+                this.privatePackages = privatePackages;
+            }
+
+            ArrayList<ChangeListener> changes = new 
ArrayList<ChangeListener>();
+
+            synchronized (listeners) {
+                changes.addAll(listeners);
+            }
+
+            for (ChangeListener change : changes) {
+                change.stateChanged(new ChangeEvent(this));
         }
         
     }
 }
+}

[hg] main-silver: #201453: Only public packages from non-impleme...

Martin Kozeny 02/22/2013

Project Features

About this Project

APIsupport was started in November 2009, is owned by Antonin Nebuzelsky, and has 43 members.
By use of this website, you agree to the NetBeans Policies and Terms of Use (revision 20131025.e7cbc9d). © 2013, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo
 
 
Close
loading
Please Confirm
Close