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

JAXB xjc CClassInfo constructor with fullname constructs wrong package #1114

Closed
Tomas-Kraus opened this issue Dec 21, 2016 · 4 comments · Fixed by #1452
Closed

JAXB xjc CClassInfo constructor with fullname constructs wrong package #1114

Tomas-Kraus opened this issue Dec 21, 2016 · 4 comments · Fixed by #1452

Comments

@Tomas-Kraus
Copy link
Member

CClassInfo has the below constructor which takes a JCodeModel and a fullName. The fullName parameter should be the FQCN of the class. However, if the fullName starts with a package having more than one name segment (like "com.example" which has two segments), the following code fails, because it puts the resulting CClassInfo into the package that is the first segment.
Assuming a fullName "com.example.MyClass", the code below will find that the indexOf '.' is 3, and from there everything goes wrong.
The constructor sets this.parent to the package "com" and it will use fullName.substring(idx+1) to determine the shortName, i.e. he shortName ends up being "example.MyClass".

{code title=com.sun.tools.xjc.model.CClassInfo|borderStyle=solid}

public CClassInfo(Model model,JCodeModel cm, String fullName, Locator location, QName typeName, QName elementName, XSComponent source, CCustomizations customizations) {
super(model,source,location,customizations);
this.model = model;
int idx = fullName.indexOf('.'); <-- must be lastIndexOf('.')
if(idx<0)

{ this.parent = model.getPackage(cm.rootPackage()); this.shortName = model.allocator.assignClassName(parent,fullName); }

else

{ this.parent = model.getPackage(cm._package(fullName.substring(0,idx))); this.shortName = model.allocator.assignClassName(parent,fullName.substring(idx+1)); }

this.typeName = typeName;
this.elementName = elementName;

model.add(this);
}

The line
int idx = fullName.indexOf('.');
should be
int idx = fullName.lastIndexOf('.');

Affected Versions

[2.2.11]

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
Reported by dschulten

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
dschulten said:
Corrected code block:

**com.sun.tools.xjc.model.CClassInfo**
public CClassInfo(Model model,JCodeModel cm, String fullName, Locator location, QName typeName, QName elementName, XSComponent source, CCustomizations customizations) {
        super(model,source,location,customizations);
        this.model = model;
        int idx = fullName.indexOf('.');  <-- must be lastIndexOf('.')
        if(idx<0) {
            this.parent = model.getPackage(cm.rootPackage());
            this.shortName = model.allocator.assignClassName(parent,fullName);
        } else {
            this.parent = model.getPackage(cm._package(fullName.substring(0,idx)));
            this.shortName = model.allocator.assignClassName(parent,fullName.substring(idx+1));
        }
        this.typeName = typeName;
        this.elementName = elementName;

        model.add(this);
    }

@Tomas-Kraus
Copy link
Member Author

@glassfishrobot Commented
This issue was imported from java.net JIRA JAXB-1114

@Tomas-Kraus
Copy link
Member Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants