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

using empty namespace for types with no package #398

Closed
wants to merge 2 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 @@ -55,7 +55,10 @@ public static FastenURI toCanonicalSchemelessURI(final String product,

final var javaURIRaw = FastenJavaURI.create(null, product, null,
packageName, className, method, params, returnType);
final var javaURI = javaURIRaw.canonicalize();
FastenJavaURI javaURI = javaURIRaw;
if (javaURIRaw.getNamespace() != null) {
javaURI = javaURIRaw.canonicalize();
}

return FastenURI.createSchemeless(javaURI.getRawForge(), javaURI.getRawProduct(),
javaURI.getRawVersion(),
Expand Down Expand Up @@ -104,7 +107,7 @@ public static String getPackageName(final Type parameter) {
return slashToDot(Objects
.requireNonNull(getPackageName(parameter.asArrayType().componentType())));
} else if (parameter.asObjectType().packageName().equals("")) {
return null;
return "";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we then end up with a double slash in the URLs? So before it was "/null/ClassName", now it is "//ClassName"... or is this being handled through canonicalize?

Copy link
Contributor Author

@ashkboos ashkboos Feb 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, now it is "//ClassName". Canonicalize does not handle it. In fact, if there is no namespace canonicalize complains about it. That is why we now check before canonicalizing to see if there is no namespace please don't try.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I do not like the // construct... I would argue that the correct version would be /ClassName. Is there an easy way to achieve this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not allowd by the FastenUri class if you write this:

var v = FastenURI.create("/ClassName");

you would get java.lang.IllegalArgumentException: Missing entity

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also just realized that even "//ClassName" cannot be correctly parsed by FastenUri. It does not cause any exception but It assumes that ClassName is the product name while it is the name of the class. What should we do about this? should we use some meaningful placeholder for the package name such as /no.package/ClassName? or should we create another URI specific for the classes?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using /ClassName and fixing the code that extracts the package/product/class information?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think we should revise the FastenURI at some point. We should improve our validation and I also think that it does not make sense to parse the whole thing on initialization. I would prefer storing the string representation and only parse part of it on demand, we can still cache information, once parsed. ... but this is a discussion for another issue.

} else {
return slashToDot(parameter.asObjectType().packageName());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import eu.fasten.core.data.JavaScope;
import eu.fasten.core.data.PartialJavaCallGraph;
import java.io.File;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -102,6 +103,12 @@ public void staticInitializer() throws OPALException, URISyntaxException {
"\n", toString(mergedRcg));
}

@Test
public void noPackage() throws URISyntaxException {
final var np = getRCG("merge/staticInitializer/NoPackage.class", "NoPackage", "0.0.0");
Assertions.assertEquals(np.getClassHierarchy().get(JavaScope.internalTypes).keySet(),
Set.of("//NoPackage"));
}
private String toString(List<Pair<String, String>> mergedRcg) {
return CallGraphUtils.toStringEdges(mergedRcg);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ void getPackageNameReferenceTypeEmpty() {
Mockito.when(type.isArrayType()).thenReturn(false);
Mockito.when(type.asObjectType()).thenReturn(objectType);

assertNull(OPALMethod.getPackageName(type));
assertEquals("", OPALMethod.getPackageName(type));
}

@Test
Expand Down
Binary file not shown.
10 changes: 9 additions & 1 deletion core/src/main/java/eu/fasten/core/data/JavaNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public class JavaNode extends Node {
*/
public JavaNode(final FastenURI uri, final Map<String, Object> metadata) {
super(uri, metadata);
this.signature = StringUtils.substringAfter(FastenJavaURI.create(uri.toString()).decanonicalize().getEntity(), ".");
final var javaURI = FastenJavaURI.create(uri.toString());
String entity;
if (javaURI.getNamespace().equals("null")) {
entity = javaURI.getEntity();
} else {
entity = javaURI.decanonicalize().getEntity();
}
this.signature = StringUtils.substringAfter(entity, ".");

}

/**
Expand Down