phax / jcodemodel

A heavily extended fork of the com.sun.codemodel (from 2013/09)
Other
92 stars 34 forks source link

JPackage.parent() cannot walk to root package #27

Closed davekipfer closed 9 years ago

davekipfer commented 9 years ago

When producing a code model using JCodeModel.ref(), the following operation doesn't work as expected:

// Create JCodeModel

JCodeModel wModel = new JCodeModel();

// Reflect into class

AbstractJClass wClass = wModel.ref(com.redfish.prod.Boomer.class);

// Walk up to the root package

JPackage wCurrentPackage = null;

for (wCurrentPackage = wClass._package(); wCurrentPackage.parent() != null; wCurrentPackage = wCurrentPackage.parent());

JPackage wRoot = wCurrentPackage;

The problem occurs in JPackage.parent() on the package just above the root package. The name is just "com", and so the existing code:

/**

  • Gets the parent package, or null if this class is the root package. */

    @Nullable

    public JPackage parent ()

    {

    if (m_sName.length () == 0)

    return null;

    final int idx = m_sName.lastIndexOf ('.');

    return m_aOwner._package (m_sName.substring (0, idx));

    }

The result in the lowest level non-root package is a StringIndexOutOfBoundsException as "idx" is set to -1.

This seems to be correct usage based on what I seei in the method's documentation, but clearly it can't handle it.

phax commented 9 years ago

Thanks for finding it. Fixed in the trunk. Is it urgent for you to get a new release, or can I await some more issues?

davekipfer commented 9 years ago

No urgent need, I think I have a workaround for now.

phax commented 9 years ago

Something like wModel._package ("com")?

davekipfer commented 9 years ago

No, what I do is instead of checking wCurrentPackage.parent() in the loop, I check to see if wCurrentPackage is a package just under the root package by checking for any '.' in the name.

phax commented 9 years ago

Alrighty. Is fixed in release 2.7.11. Stay tuned.

davekipfer commented 9 years ago

Much appreciated!