eclipse-ee4j / jaxb-ri

Jaxb RI
https://eclipse-ee4j.github.io/jaxb-ri/
BSD 3-Clause "New" or "Revised" License
204 stars 111 forks source link

JAXB xjc CClassInfo constructor with fullname constructs wrong package #1648

Closed Tomas-Kraus closed 1 year ago

Tomas-Kraus commented 2 years ago

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".

**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('.');
        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('.');

Source: https://github.com/javaee/metro-jax-ws/issues/1187 Author: glassfishrobot

antoniosanct commented 1 year ago

Fixed in 748571a. Maybe could you close it, @lukasj ?

Regards, Antonio.

lukasj commented 1 year ago

fixed by #1452, thanks for heads up