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

ClassBuilderInfo sometimes generates invalid imports for classes in the same package #264

Closed
ritzow opened this issue Aug 20, 2024 · 3 comments

Comments

@ritzow
Copy link
Contributor

ritzow commented Aug 20, 2024

Discovered when generating code for systemd dbus interfaces

package org.freedesktop.systemd1;

import PresetUnitFilesWithModeChangesStruct; //ERROR IS HERE
import java.util.List;
import org.freedesktop.dbus.Tuple;
import org.freedesktop.dbus.annotations.Position;

/**
 * Auto-generated class.
 */
public class PresetUnitFilesWithModeTuple extends Tuple {
    @Position(0)
    private boolean carriesInstallInfo;
    @Position(1)
    private List<PresetUnitFilesWithModeChangesStruct> changes;

The other generated class, for reference

package org.freedesktop.systemd1;

import org.freedesktop.dbus.Struct;
import org.freedesktop.dbus.annotations.Position;

/**
 * Auto-generated class.
 */
public class PresetUnitFilesWithModeChangesStruct extends Struct {

In method org.freedesktop.dbus.utils.generator.ClassBuilderInfo#createClassFileContent(boolean, java.util.Set<java.lang.String>), the call to org.freedesktop.dbus.utils.generator.ClassBuilderInfo#getImports returns a set that contains PresetUnitFilesWithModeChangesStruct. Notice that it does not have it's package name. The import should be org.freedesktop.systemd1.PresetUnitFilesWithModeChangesStruct, which would cause the code at org/freedesktop/dbus/utils/generator/ClassBuilderInfo.java:298

.filter(l -> !l.replaceFirst("(.+)\\..+", "$1").equals(getPackageName()))

... to not filter out PresetUnitFilesWithModeChangesStruct despite it's actual generated class being in the same package.

So it seems like the root issue is that getImports().add(...) (or addAll()) is called at some point with a class name that has it's package name removed.

@ritzow ritzow changed the title ClassBuilderInfo sometimes generates imports for classes in the same package ClassBuilderInfo sometimes generates invalid imports for classes in the same package Aug 20, 2024
@hypfvieh
Copy link
Owner

Can you please add for which systemd path you try to create the classes for? I would like to investigate and fix this, but with about 200 possible pathes it is pointless to start.

@hypfvieh
Copy link
Owner

This should be fixed now.
The issue was, that the package name was removed by

.filter(l -> !l.replaceFirst("(.+)\\..+", "$1").equals(getPackageName()))

So just the classname was remaining. Because this class and the class using it are in the same package, the whole import can be skipped. Therefore removing imports without any packages should fix it.

@ritzow
Copy link
Contributor Author

ritzow commented Aug 25, 2024

Thank you!

@ritzow ritzow closed this as completed Aug 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants