Skip to content

Commit

Permalink
[MSHARED-785] Make ConstantPoolParser ignore classes in unnamed package
Browse files Browse the repository at this point in the history
Closes #21
  • Loading branch information
jhaber authored and slachiewicz committed Apr 8, 2021
1 parent 36465f6 commit 6186296
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,13 @@ static Set<String> parseConstantPoolClassReferences( ByteBuffer buf )
Set<String> result = new HashSet<>();
for ( Integer aClass : classes )
{
result.add( stringConstants.get( aClass ) );
String className = stringConstants.get( aClass );

// filter out things from unnamed package, probably a false-positive
if ( isImportableClass( className ) )
{
result.add( className );
}
}
return result;
}
Expand Down Expand Up @@ -207,4 +213,10 @@ private static String decodeString( ByteBuffer buf )
( (Buffer) buf ).limit( oldLimit );
return sb.toString();
}

private static boolean isImportableClass( String className )
{
// without a slash, class must be in unnamed package, which can't be imported
return className.indexOf( '/' ) != -1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.maven.shared.test.plugin.RepositoryTool;
import org.apache.maven.shared.test.plugin.TestToolsException;
import org.codehaus.plexus.PlexusTestCase;
import org.junit.Assume;

import java.io.File;
import java.util.Arrays;
Expand Down Expand Up @@ -421,6 +422,29 @@ public void testTypeUseAnnotationDependencyOnLocalVariable()
assertEquals( expectedAnalysis, actualAnalysis );
}

public void testUnnamedPackageClassReference()
throws TestToolsException, ProjectDependencyAnalyzerException
{
Assume.assumeTrue( SystemUtils.isJavaVersionAtLeast( JavaVersion.JAVA_1_8 ) );

// Only visible through constant pool analysis (supported for JDK8+)
compileProject( "unnamedPackageClassReference/pom.xml" );

MavenProject project = getProject( "unnamedPackageClassReference/pom.xml" );

ProjectDependencyAnalysis actualAnalysis = analyzer.analyze( project );

Artifact dnsjava = createArtifact( "dnsjava", "dnsjava", "jar", "2.1.8", "compile" );
// we don't use any dnsjava classes so this should show up as an unused dep
Set<Artifact> unusedDeclaredArtifacts = Collections.singleton( dnsjava );

ProjectDependencyAnalysis expectedAnalysis =
new ProjectDependencyAnalysis( new HashSet<Artifact>(), new HashSet<Artifact>(), unusedDeclaredArtifacts,
new HashSet<Artifact>() );

assertEquals( expectedAnalysis, actualAnalysis );
}

// private methods --------------------------------------------------------

private void compileProject( String pomPath )
Expand Down
41 changes: 41 additions & 0 deletions src/test/resources/unnamedPackageClassReference/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8"?>

<!--
~ 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.
-->

<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>

<groupId>org.apache.maven.shared.dependency-analyzer.tests</groupId>
<artifactId>unnamedPackageClassReference</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<dependencies>
<dependency>
<groupId>dnsjava</groupId>
<artifactId>dnsjava</artifactId>
<version>2.1.8</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package unnamedPackageClassReference;

/*
* 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 Project
{
// dnsjava 2.1.8 includes a class called "update"
public static final String UPDATE = "update";

public Project()
{
// no op
}
}

0 comments on commit 6186296

Please sign in to comment.