You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2018/07/28 19:34:16 UTC

[GitHub] judby closed pull request #5: Fix for JXR-68 and JXR-135

judby closed pull request #5: Fix for JXR-68 and JXR-135
URL: https://github.com/apache/maven-jxr/pull/5
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java b/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
index b15b058..3280235 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/DirectoryIndexer.java
@@ -331,7 +331,7 @@ private void doVelocity( String templateName, String outDir, VelocityContext con
      * allClasses collection of Maps with class info, format as above
      *
      */
-    private Map<String, Object> getPackageInfo()
+    Map<String, Object> getPackageInfo()
     {
         Map<String, Map<String, Object>> allPackages = new TreeMap<>();
         Map<String, Map<String, String>> allClasses = new TreeMap<>();
@@ -372,8 +372,8 @@ private void doVelocity( String templateName, String outDir, VelocityContext con
                 classInfo.put( "name", className );
                 classInfo.put( "dir", pkgDir );
 
-                pkgClasses.put( className, classInfo );
-                allClasses.put( className, classInfo );
+                pkgClasses.put( pkgName + "." + className, classInfo );
+                allClasses.put( pkgName + "." + className, classInfo );
             }
 
             Map<String, Object> pkgInfo = new HashMap<>();
diff --git a/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java b/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
index e1594e7..baef9ae 100644
--- a/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
+++ b/maven-jxr/src/main/java/org/apache/maven/jxr/pacman/JavaFileImpl.java
@@ -41,7 +41,7 @@
     /**
      * Create a new JavaFileImpl that points to a given file...
      *
-     * @param filename
+     * @param path
      * @throws IOException
      */
     public JavaFileImpl( Path path, String encoding )
@@ -63,65 +63,86 @@ public JavaFileImpl( Path path, String encoding )
      * statements.
      */
     private void parse()
-        throws IOException
+            throws IOException
     {
         StreamTokenizer stok = null;
         try ( Reader reader = getReader() )
         {
             stok = this.getTokenizer( reader );
 
-            while ( stok.nextToken() != StreamTokenizer.TT_EOF )
-            {
+            parseRecursive( "", stok );
+        }
+        finally
+        {
+            stok = null;
+        }
+    }
 
-                if ( stok.sval == null )
-                {
-                    continue;
-                }
+    private void parseRecursive( String nestedPrefix, StreamTokenizer stok )
+            throws IOException
+    {
+        int openBracesCount = 0;
+
+        while ( stok.nextToken() != StreamTokenizer.TT_EOF )
+        {
 
-                //set the package
-                if ( "package".equals( stok.sval ) && stok.ttype != '\"' )
+            if ( stok.sval == null )
+            {
+                if ( stok.ttype == '{' )
                 {
-                    stok.nextToken();
-                    this.setPackageType( new PackageType( stok.sval ) );
+                    openBracesCount++;
                 }
-
-                //set the imports
-                if ( "import".equals( stok.sval )  && stok.ttype != '\"' )
+                else if ( stok.ttype == '}' )
                 {
-                    stok.nextToken();
-
-                    String name = stok.sval;
-
-                    /*
-                    WARNING: this is a bug/non-feature in the current
-                    StreamTokenizer.  We needed to set the comment char as "*"
-                    and packages that are imported with this (ex "test.*") will be
-                    stripped( and become "test." ).  Here we need to test for this
-                    and if necessary re-add the char.
-                    */
-                    if ( name.charAt( name.length() - 1 ) == '.' )
+                    if ( --openBracesCount == 0 )
                     {
-                        name = name + '*';
+                        // break out of recursive
+                        return;
                     }
-
-                    this.addImportType( new ImportType( name ) );
                 }
+                continue;
+            }
 
-                // Add the class or classes. There can be several classes in one file so
-                // continue with the while loop to get them all.
-                if ( ( "class".equals( stok.sval ) || "interface".equals( stok.sval ) || "enum".equals( stok.sval ) )
-                    &&  stok.ttype != '\"' )
+            //set the package
+            if ( "package".equals( stok.sval ) && stok.ttype != '\"' )
+            {
+                stok.nextToken();
+                this.setPackageType( new PackageType( stok.sval ) );
+            }
+
+            //set the imports
+            if ( "import".equals( stok.sval )  && stok.ttype != '\"' )
+            {
+                stok.nextToken();
+
+                String name = stok.sval;
+
+                /*
+                WARNING: this is a bug/non-feature in the current
+                StreamTokenizer.  We needed to set the comment char as "*"
+                and packages that are imported with this (ex "test.*") will be
+                stripped( and become "test." ).  Here we need to test for this
+                and if necessary re-add the char.
+                */
+                if ( name.charAt( name.length() - 1 ) == '.' )
                 {
-                    stok.nextToken();
-                    this.addClassType( new ClassType( stok.sval,
-                                                      getFilenameWithoutPathOrExtension( this.getPath() ) ) );
+                    name = name + '*';
                 }
 
+                this.addImportType( new ImportType( name ) );
             }
-        }
-        finally
-        {
-            stok = null;
+
+            // Add the class or classes. There can be several classes in one file so
+            // continue with the while loop to get them all.
+            if ( ( "class".equals( stok.sval ) || "interface".equals( stok.sval ) || "enum".equals( stok.sval ) )
+                    &&  stok.ttype != '\"' )
+            {
+                stok.nextToken();
+                this.addClassType( new ClassType( nestedPrefix + stok.sval,
+                        getFilenameWithoutPathOrExtension( this.getPath() ) ) );
+                parseRecursive( nestedPrefix + stok.sval + ".", stok );
+            }
+
         }
     }
 
diff --git a/maven-jxr/src/test/java/org/apache/maven/jxr/ClassWithNested.java b/maven-jxr/src/test/java/org/apache/maven/jxr/ClassWithNested.java
new file mode 100644
index 0000000..cca3e5c
--- /dev/null
+++ b/maven-jxr/src/test/java/org/apache/maven/jxr/ClassWithNested.java
@@ -0,0 +1,40 @@
+package org.apache.maven.jxr;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * Class part of the test for JXR-135 illustrating the use of nested classes, interfaces and enums
+ */
+public class ClassWithNested
+{
+    public interface NestedInterface {}
+    public static class NestedClassWithEnum {
+        public enum NestedEnum { }
+        public class NestedClass2 {
+        }
+    }
+
+    public static class NestedClassWithEnum2 {
+        public enum NestedEnum { }
+        public class NestedClass2 {
+        }
+    }
+}
+class NotNested {}
diff --git a/maven-jxr/src/test/java/org/apache/maven/jxr/DirectoryIndexerTest.java b/maven-jxr/src/test/java/org/apache/maven/jxr/DirectoryIndexerTest.java
new file mode 100644
index 0000000..b836be3
--- /dev/null
+++ b/maven-jxr/src/test/java/org/apache/maven/jxr/DirectoryIndexerTest.java
@@ -0,0 +1,53 @@
+package org.apache.maven.jxr;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.jxr.pacman.FileManager;
+import org.apache.maven.jxr.pacman.PackageManager;
+import org.junit.Test;
+
+import java.nio.file.Paths;
+import java.util.Map;
+
+import static org.junit.Assert.assertTrue;
+
+public class DirectoryIndexerTest {
+    /**
+     * Parse the files in test/resources/java packages, ensure all are present in the allClasses Map
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testJXR_68() throws Exception {
+        FileManager fileManager = FileManager.getInstance();
+        PackageManager packageManager = new PackageManager(new DummyLog(), fileManager);
+        packageManager.process(Paths.get("src/test/resources/java"));
+        DirectoryIndexer directoryIndexer = new DirectoryIndexer(packageManager, "");
+
+        final Map<String, Object> packageInfo = directoryIndexer.getPackageInfo();
+        final Map<String, String> allPackages = (Map<String, String>) packageInfo.get("allPackages");
+        assertTrue(allPackages.containsKey("pkga"));
+        assertTrue(allPackages.containsKey("pkgb"));
+        final Map<String, String> allClasses = (Map<String, String>) packageInfo.get("allClasses");
+        assertTrue(allClasses.containsKey("pkga.SomeClass"));
+        assertTrue(allClasses.containsKey("pkgb.SomeClass"));
+    }
+
+}
\ No newline at end of file
diff --git a/maven-jxr/src/test/java/org/apache/maven/jxr/pacman/JavaFileImplTest.java b/maven-jxr/src/test/java/org/apache/maven/jxr/pacman/JavaFileImplTest.java
new file mode 100644
index 0000000..4bb5169
--- /dev/null
+++ b/maven-jxr/src/test/java/org/apache/maven/jxr/pacman/JavaFileImplTest.java
@@ -0,0 +1,45 @@
+package org.apache.maven.jxr.pacman;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.junit.Test;
+
+import java.nio.file.Paths;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+public class JavaFileImplTest {
+    @Test
+    public void testJXR_135() throws Exception {
+        JavaFileImpl javaFile = new JavaFileImpl( Paths.get( "src/test/java/org/apache/maven/jxr/ClassWithNested.java" ), "UTF-8" );
+        final List<ClassType> classTypes = javaFile.getClassTypes();
+        assertEquals( "ClassWithNested", classTypes.get(0).getName() );
+        assertEquals( "ClassWithNested.NestedInterface", classTypes.get(1).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum", classTypes.get(2).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum.NestedEnum", classTypes.get(3).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum.NestedClass2", classTypes.get(4).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum2", classTypes.get(5).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum2.NestedEnum", classTypes.get(6).getName() );
+        assertEquals( "ClassWithNested.NestedClassWithEnum2.NestedClass2", classTypes.get(7).getName() );
+        assertEquals( "NotNested", classTypes.get(8).getName() );
+    }
+
+}
\ No newline at end of file
diff --git a/maven-jxr/src/test/resources/java/pkga/SomeClass.java b/maven-jxr/src/test/resources/java/pkga/SomeClass.java
new file mode 100644
index 0000000..daeaeef
--- /dev/null
+++ b/maven-jxr/src/test/resources/java/pkga/SomeClass.java
@@ -0,0 +1,22 @@
+package pkga;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class SomeClass {}
\ No newline at end of file
diff --git a/maven-jxr/src/test/resources/java/pkgb/SomeClass.java b/maven-jxr/src/test/resources/java/pkgb/SomeClass.java
new file mode 100644
index 0000000..5bb53c7
--- /dev/null
+++ b/maven-jxr/src/test/resources/java/pkgb/SomeClass.java
@@ -0,0 +1,22 @@
+package pkgb;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+public class SomeClass {}
\ No newline at end of file


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services