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

ClassName is not a valid Java identifier #70

Open
jiangdequan opened this issue Nov 27, 2017 · 2 comments
Open

ClassName is not a valid Java identifier #70

jiangdequan opened this issue Nov 27, 2017 · 2 comments

Comments

@jiangdequan
Copy link

jiangdequan commented Nov 27, 2017

There is a problem about the classname!
In the model castor-codegen of org.exolab.castor.builder.conflict.strategy.BaseClassNameConflictResolver.calculateXPathPrefix,
in some cases, when the input is like '/Voucher{http://www.your.org/Test/Space}/Name/Surname', but the output is 'WwwYourOrgTestSpace}NameSurname'.This is not a valid java identifier.

the source code below split the input string words, but not remove the invalid identifier in model castor-xml(org.castor.xml.JavaNamingImpl)

protected String calculateXPathPrefix(final String xpath) {
    String prefix = "";
    StringTokenizer stringTokenizer = new StringTokenizer(xpath, "/.");
    while (stringTokenizer.hasMoreTokens()) {
      String token = stringTokenizer.nextToken();
      // break on last token
      if (!stringTokenizer.hasMoreTokens()) {
        break;
      }
      if (token.startsWith(ExtendedBinding.COMPLEXTYPE_ID)
          || token.startsWith(ExtendedBinding.SIMPLETYPE_ID)
          || token.startsWith(ExtendedBinding.ENUMTYPE_ID)
          || token.startsWith(ExtendedBinding.GROUP_ID)) {
        token = token.substring(token.indexOf(":") + 1);
      }
      prefix += _sourceGenerator.getJavaNaming().toJavaClassName(token);
    }
    return prefix;
  }

fix:
add some code to remove "}" in model castor-xml(org.castor.xml.JavaNamingImpl)

public final String toJavaClassName(final String name) {

        if ((name == null) || (name.length() <= 0)) {
            // handle error
            return name; // -- for now just return name
        }
        // Remove namespace prefix (Andrew Fawcett, temporary until namespace
        // changes go in)
        int colon = name.indexOf(':');
        if (colon != -1) {
            return toJavaName(name.substring(colon + 1), true);
        }
        return toJavaName(name, true);

    } // -- toJavaClassName
@wguttmn
Copy link
Contributor

wguttmn commented Nov 27, 2017

Can you please provide a pull request ? And possibly include a unit test as well that clearly shows the abnormal behavior ?

@jiangdequan
Copy link
Author

It occured when I add property 'org.exolab.castor.builder.automaticConflictResolution=true' to resolve class name conflict.The following is the error log:

Caused by: java.lang.IllegalArgumentException: 'WwwYourOrgTestSpace}NameSurname' is not a valid Java identifier.
	at org.exolab.javasource.JStructure.<init>(JStructure.java:109)
	at org.exolab.javasource.AbstractJClass.<init>(AbstractJClass.java:78)
	at org.exolab.javasource.AbstractJClass.<init>(AbstractJClass.java:69)
	at org.exolab.javasource.JClass.<init>(JClass.java:59)
	at org.exolab.castor.builder.descriptors.DescriptorJClass.<init>(DescriptorJClass.java:92)
	at org.exolab.castor.builder.descriptors.DescriptorSourceFactory.createSource(DescriptorSourceFactory.java:112)
	at org.exolab.castor.builder.SingleClassGenerator.processClassDescriptor(SingleClassGenerator.java:390)
	at org.exolab.castor.builder.SingleClassGenerator.process(SingleClassGenerator.java:370)
	at org.exolab.castor.builder.SingleClassGenerator.process(SingleClassGenerator.java:310)
	at org.exolab.castor.builder.SourceGenerator.createClasses(SourceGenerator.java:933)
	at org.exolab.castor.builder.SourceGenerator.processContentModel(SourceGenerator.java:1078)
	at org.exolab.castor.builder.SourceGenerator.processContentModel(SourceGenerator.java:1081)
	at org.exolab.castor.builder.SourceGenerator.processComplexType(SourceGenerator.java:1019)
	at org.exolab.castor.builder.SourceGenerator.createClasses(SourceGenerator.java:939)
	at org.exolab.castor.builder.SourceGenerator.generateAllClassFiles(SourceGenerator.java:786)
	at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:744)
	at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:690)
	at org.exolab.castor.builder.SourceGenerator.generateSource(SourceGenerator.java:587)

Due to the development environment, I commited several times, but the last one 'a57c621' is useful.

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