Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MSHARED-785] Make ConstantPoolParser ignore classes in unnamed package #21

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,13 @@ static Set<String> parseConstantPoolClassReferences( ByteBuffer buf )
Set<String> result = new HashSet<String>();
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 @@ -189,4 +195,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 @@ -43,6 +43,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;

/**
* Tests <code>DefaultProjectDependencyAnalyzer</code>.
Expand Down Expand Up @@ -427,6 +428,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
}
}