Hi Darran,
We have a reserved keyword routine in JavaUtils.isReservedKeyword(). I'm
fine if you want to use your implementation instead, although the code
should be consolidated to only use one, and the other should be removed.
Thanks,
-Jason
On Mon, 2007-03-05 at 07:04 -0500, jbossws-commits(a)lists.jboss.org
wrote:
Author: darran.lofthouse(a)jboss.com
Date: 2007-03-05 07:04:01 -0500 (Mon, 05 Mar 2007)
New Revision: 2521
Added:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaKeywords.java
Modified:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaWriter.java
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/NamespacePackageMapping.java
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/WSDLToJava.java
branches/dlofthouse/JBWS-1534/jbossws-tests/src/main/java/org/jboss/test/ws/tools/jbws1534/JBWS1534TestCase.java
Log:
JBWS-1543 - Prefix Java keywords with an underscore.
Added:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaKeywords.java
===================================================================
---
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaKeywords.java
(rev 0)
+++
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaKeywords.java 2007-03-05
12:04:01 UTC (rev 2521)
@@ -0,0 +1,90 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2007, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.ws.tools;
+
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * Singleton to check if a specified work is a restricted keyword.
+ *
+ * This list also includes the boolean literals and the null literal.
+ *
+ * @author darran.lofthouse(a)jboss.com
+ * @since 5 Mar 2007
+ */
+public class JavaKeywords
+{
+
+ private static final JavaKeywords keywords = new JavaKeywords();
+
+ private final String[] keywordsArray = { "abstract", "assert",
"boolean", "break", "byte", "case",
"catch", "char", "class", "const",
"continue", "default", "do",
+ "double", "else", "enum", "extends",
"final", "finally", "float", "for",
"if", "goto", "implements", "import",
"instanceof", "int", "interface", "long",
+ "native", "new", "package", "private",
"protected", "public", "return", "short",
"static", "strictfp", "super", "switch",
"synchronized", "this", "throw",
+ "throws", "transient", "try", "void",
"volatile", "while" };
+
+ private final String[] restrictedLiteralsArray = { "true",
"false", "null" };
+
+ private final Set<String> restrictedKeywords;
+
+ private JavaKeywords()
+ {
+ int keywordsSize = keywordsArray.length + restrictedLiteralsArray.length;
+ keywordsSize = (int)((double)keywordsSize / 0.75) + 1;
+
+ restrictedKeywords = new HashSet<String>(keywordsSize);
+ addAll(restrictedKeywords, keywordsArray);
+ addAll(restrictedKeywords, restrictedLiteralsArray);
+ }
+
+ private void addAll(final Set<String> set, final String[] data)
+ {
+ for (String current : data)
+ {
+ set.add(current);
+ }
+ }
+
+ /**
+ * The internal method to check if the keyword is a restricted
+ * keyword.
+ *
+ * @param name
+ * @return
+ */
+ private boolean internalIsRestrictedKeyword(final String name)
+ {
+ return restrictedKeywords.contains(name);
+ }
+
+ /**
+ * Check is the passed in name is a reserved Java keyword.
+ *
+ * @param name
+ * @return
+ */
+ public static boolean isJavaKeyword(final String name)
+ {
+ return keywords.internalIsRestrictedKeyword(name);
+ }
+
+}
Property changes on:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaKeywords.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaWriter.java
===================================================================
---
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaWriter.java 2007-03-05
11:22:47 UTC (rev 2520)
+++
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/JavaWriter.java 2007-03-05
12:04:01 UTC (rev 2521)
@@ -1,24 +1,24 @@
/*
-* JBoss, Home of Professional Open Source
-* Copyright 2005, JBoss Inc., and individual contributors as indicated
-* by the @authors tag. See the copyright.txt in the distribution for a
-* full listing of individual contributors.
-*
-* This is free software; you can redistribute it and/or modify it
-* under the terms of the GNU Lesser General Public License as
-* published by the Free Software Foundation; either version 2.1 of
-* the License, or (at your option) any later version.
-*
-* This software is distributed in the hope that it will be useful,
-* but WITHOUT ANY WARRANTY; without even the implied warranty of
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-* Lesser General Public License for more details.
-*
-* You should have received a copy of the GNU Lesser General Public
-* License along with this software; if not, write to the Free
-* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-* 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
-*/
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
package org.jboss.ws.tools;
import java.io.File;
@@ -50,7 +50,6 @@
{
}
-
/**
* Creates a Java Class that represents a Simple Type restricted by enumeration
* @param fname
@@ -60,16 +59,13 @@
* @throws IOException
*/
- public void createJavaFileForEnumeratedValues(String fname, StringList lst,
- File loc, String pkgname,
- XSSimpleTypeDefinition type)
- throws IOException
+ public void createJavaFileForEnumeratedValues(String fname, StringList lst, File loc,
String pkgname, XSSimpleTypeDefinition type) throws IOException
{
List importList = new ArrayList();
importList.add("java.util.Map");
importList.add("java.util.HashMap");
File sei = utils.createPhysicalFile(loc, fname);
- StringBuilder buf = utils.createClassBasicStructure(pkgname, fname, type,
importList,null);
+ StringBuilder buf = utils.createClassBasicStructure(pkgname, fname, type,
importList, null);
buf.append("private java.lang.String value;" + newline);
buf.append("private static Map valueMap = new HashMap();" + newline);
@@ -116,7 +112,8 @@
for (int i = 0; i < lenOfArr; i++)
{
String str = lst.item(i);
- if (i > 0) buf.append("else ");
+ if (i > 0)
+ buf.append("else ");
buf.append("if (" + str + ".value.equals(value)) {" +
newline);
@@ -141,7 +138,8 @@
for (int i = 0; i < lenOfArr; i++)
{
String str = lst.item(i);
- if (i > 0) buf.append("else ");
+ if (i > 0)
+ buf.append("else ");
buf.append("if (value.equals(_" + str + "String)) {" +
newline);
buf.append("return " + str + ";" + newline +
"}");
}
@@ -157,24 +155,20 @@
//create -readResolve method
- buf.append(newline + "private Object readResolve()" + newline +
- " throws java.io.ObjectStreamException {" + newline +
- " return fromValue(getValue());" + newline +
- " } " + newline);
+ buf.append(newline + "private Object readResolve()" + newline + "
throws java.io.ObjectStreamException {" + newline
+ + " return fromValue(getValue());" + newline + " }
" + newline);
//End - readResolve method
//create - equals method
- buf.append(newline + "private boolean equals(Object obj){" + newline +
- " if (!(obj instanceof " + fname + ")) {" + newline
+
- " return false;" + newline + " } " + newline);
+ buf.append(newline + "private boolean equals(Object obj){" + newline +
" if (!(obj instanceof " + fname + ")) {" + newline + "
return false;"
+ + newline + " } " + newline);
buf.append("return ((" + fname + ")obj).value.equals(value);"
+ newline + "}" + newline);
//End - equals method
//create - hashCode method
- buf.append(newline + " public int hashCode() { " + newline +
- " return value.hashCode(); " + newline + " }" +
newline);
+ buf.append(newline + " public int hashCode() { " + newline + "
return value.hashCode(); " + newline + " }" + newline);
//End - hashCode method
buf.append("}" + newline); //end of class
@@ -197,17 +191,14 @@
* @param typeNameToBaseVARList Needed if we are dealing with an exception type
* @throws IOException
*/
- public void createJavaFile(File location, String filename, String packageName,
- List<VAR> vars, List<String> importList, String baseTypeName,
- boolean isExceptionType, Map<String,List> typeNameToBaseVARList)
- throws IOException
+ public void createJavaFile(File location, String filename, String packageName,
List<VAR> vars, List<String> importList, String baseTypeName,
+ boolean isExceptionType, Map<String, List> typeNameToBaseVARList) throws
IOException
{
File newLoc = null;
- if(needToCreatePackageStructure(location, packageName))
- newLoc = utils.createPackage(location.getPath(),packageName);
- else
- newLoc = location;
- String classname = utils.chop(filename,".java");
+ if (needToCreatePackageStructure(location, packageName))
+ newLoc = utils.createPackage(location.getPath(), packageName);
+ else newLoc = location;
+ String classname = utils.chop(filename, ".java");
File sei = utils.createPhysicalFile(newLoc, classname);
StringBuilder buffer = new StringBuilder();
utils.writeJbossHeader(buffer);
@@ -215,16 +206,16 @@
//Create the package Name
buffer.append(newline).append("package
").append(packageName).append(";");
- if(importList != null)
+ if (importList != null)
{
- for(String imp:importList)
+ for (String imp : importList)
{
buffer.append(newline).append("import
").append(imp).append(";");
}
}
buffer.append(newline).append(newline);
buffer.append(newline).append("public class
").append(classname).append(newline);
- if(baseTypeName != null && baseTypeName.length() > 0)
+ if (baseTypeName != null && baseTypeName.length() > 0)
buffer.append(" extends ").append(baseTypeName);
buffer.append("{").append(newline);
createVariables(buffer, vars, isExceptionType);
@@ -239,14 +230,12 @@
writer.close();
}
-
//PRIVATE METHODS
- private void createCTR(StringBuilder buf, String cname, List vars,
- boolean isExceptionType,Map<String,List> typeNameToBaseVARList)
+ private void createCTR(StringBuilder buf, String cname, List vars, boolean
isExceptionType, Map<String, List> typeNameToBaseVARList)
{
- if(vars.size() > 0 && isExceptionType == false)
+ if (vars.size() > 0 && isExceptionType == false)
{
- buf.append("public " + cname + "(){}"); //Empty CTR
+ buf.append("public " + cname + "(){}"); //Empty CTR
buf.append(newline);
buf.append(newline);
}
@@ -255,17 +244,17 @@
StringBuilder ctrintbuf = new StringBuilder();
boolean calledSuper = false;
- if(isExceptionType)
+ if (isExceptionType)
{
List<VAR> baseList = typeNameToBaseVARList.get(cname);
- int baseLen = baseList != null ? baseList.size() :0;
+ int baseLen = baseList != null ? baseList.size() : 0;
String arrStr = "[]";
if (baseLen > 0)
{
calledSuper = true;
ctrintbuf.append("super(");
- for(int i = 0 ; i < baseLen; i++)
+ for (int i = 0; i < baseLen; i++)
{
if (i > 0)
@@ -275,7 +264,7 @@
}
VAR v = baseList.get(i);
ctrvarbuf.append(v.getVartype());
- if(v.isArrayType)
+ if (v.isArrayType)
ctrvarbuf.append(arrStr);
ctrvarbuf.append(" ").append(v.getVarname());
@@ -294,10 +283,16 @@
}
VAR v = (VAR)iter.next();
String name = v.getVarname();
+ if (JavaKeywords.isJavaKeyword(name))
+ {
+ name = "_" + name;
+ }
+
String type = v.getVartype();
boolean isArr = v.isArrayType();
ctrvarbuf.append(type);
- if (isArr) ctrvarbuf.append("[]");
+ if (isArr)
+ ctrvarbuf.append("[]");
ctrvarbuf.append(" " + name);
if (isExceptionType && calledSuper == false && index == 1
&& v.getVartype().equals("java.lang.String"))
{
@@ -321,19 +316,27 @@
{
VAR v = (VAR)iter.next();
String name = v.getVarname();
+ String internalName = name;
+ if (JavaKeywords.isJavaKeyword(internalName))
+ {
+ internalName = "_" + internalName;
+ }
+
String type = v.getVartype();
boolean isArr = v.isArrayType();
//Add getter/setter also
buf.append("public " + type);
- if (isArr) buf.append("[] ");
+ if (isArr)
+ buf.append("[] ");
String str = " get";
//boolean case
- if (type.equals("boolean")) str = " is";
- buf.append(str + utils.getMixedCase(name) + "() { return " + name +
" ;}");
+ if (type.equals("boolean"))
+ str = " is";
+ buf.append(str + utils.getMixedCase(name) + "() { return " +
internalName + " ;}");
buf.append(newline);
buf.append(newline);
- if(isExceptionType == false)
- writeSetter( buf, name, type, isArr);
+ if (isExceptionType == false)
+ writeSetter(buf, name, internalName, type, isArr);
buf.append(newline);
buf.append(newline);
}
@@ -341,23 +344,29 @@
private List createVariables(StringBuilder buf, List vars, boolean isExceptionType)
{
- if (vars == null) return vars;
+ if (vars == null)
+ return vars;
Iterator iter = vars.iterator();
while (iter.hasNext())
{
VAR v = (VAR)iter.next();
String name = v.getVarname();
// JBWS-1170 Convert characters which are illegal in Java identifiers
- name=ToolsUtils.convertInvalidCharacters(name);
+ name = ToolsUtils.convertInvalidCharacters(name);
+ if (JavaKeywords.isJavaKeyword(name))
+ {
+ name = "_" + name;
+ }
+
String type = v.getVartype();
boolean isArr = v.isArrayType();
buf.append(newline);
- if(isExceptionType)
+ if (isExceptionType)
buf.append("private " + type);
- else
- buf.append("protected " + type);
- if (isArr) buf.append("[] ");
+ else buf.append("protected " + type);
+ if (isArr)
+ buf.append("[] ");
buf.append(" " + name).append(";").append(newline);
}
return vars;
@@ -365,7 +374,7 @@
private boolean needToCreatePackageStructure(File location, String packageName)
{
- packageName = packageName.replace( '.','/');
+ packageName = packageName.replace('.', '/');
try
{
String externalForm = location.toURL().toExternalForm();
@@ -378,13 +387,13 @@
}
}
- private void writeSetter(StringBuilder buf, String name, String type, boolean isArr)
+ private void writeSetter(StringBuilder buf, String name, String internalName, String
type, boolean isArr)
{
buf.append("public void ");
buf.append("set" + utils.getMixedCase(name) + "(" + type);
if (isArr)
buf.append("[]");
- buf.append(" " + name + "){ this." + name + "=" +
name+"; }");
+ buf.append(" " + internalName + "){ this." + internalName +
"=" + internalName + "; }");
}
}
\ No newline at end of file
Modified:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/NamespacePackageMapping.java
===================================================================
---
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/NamespacePackageMapping.java 2007-03-05
11:22:47 UTC (rev 2520)
+++
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/NamespacePackageMapping.java 2007-03-05
12:04:01 UTC (rev 2521)
@@ -38,13 +38,6 @@
private static final NamespacePackageMapping npm = new NamespacePackageMapping();
- private final String[] keywordsArray = { "abstract", "assert",
"boolean", "break", "byte", "case",
"catch", "char", "class", "const",
"continue", "default", "do",
- "double", "else", "enum", "extends",
"final", "finally", "float", "for",
"if", "goto", "implements", "import",
"instanceof", "int", "interface", "long",
- "native", "new", "package", "private",
"protected", "public", "return", "short",
"static", "strictfp", "super", "switch",
"synchronized", "this", "throw",
- "throws", "transient", "try", "void",
"volatile", "while" };
-
- private final String[] restrictedLiteralsArray = { "true",
"false", "null" };
-
/*
* The JAXB specification says to use the list of country codes as defined in
ISO-3166 1981 to identify the top
* level domains.
@@ -73,8 +66,6 @@
private final String[] topLevelDomainsArray = { "com", "gov",
"net", "org", "edu" };
- private final Set<String> restrictedKeywords;
-
private final Set<String> topLevelDomains;
/**
@@ -91,16 +82,9 @@
private NamespacePackageMapping()
{
- int keywordsSize = keywordsArray.length + restrictedLiteralsArray.length;
- keywordsSize = (int)((double)keywordsSize / 0.75) + 1;
-
int topLevelSize = countryCodesArray.length + topLevelDomainsArray.length;
topLevelSize = (int)((double)topLevelSize / 0.75) + 1;
- restrictedKeywords = new HashSet<String>(keywordsSize);
- addAll(restrictedKeywords, keywordsArray);
- addAll(restrictedKeywords, restrictedLiteralsArray);
-
topLevelDomains = new HashSet<String>(topLevelSize);
addAll(topLevelDomains, countryCodesArray);
addAll(topLevelDomains, topLevelDomainsArray);
@@ -185,7 +169,7 @@
private String convertReserved(final String component)
{
String result = component;
- if (restrictedKeywords.contains(result))
+ if (JavaKeywords.isJavaKeyword(result))
{
result = result + "_";
}
Modified:
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/WSDLToJava.java
===================================================================
---
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/WSDLToJava.java 2007-03-05
11:22:47 UTC (rev 2520)
+++
branches/dlofthouse/JBWS-1534/jbossws-core/src/main/java/org/jboss/ws/tools/WSDLToJava.java 2007-03-05
12:04:01 UTC (rev 2521)
@@ -737,9 +737,22 @@
return cls;
}
- private String getMethodParam(String containingElement)
+ /**
+ * Make sure the first character is lower case and if the
+ * parameter is a reserved word prefix it with '_'.
+ *
+ * @param containingElement
+ * @return
+ */
+ private String getMethodParam(String name)
{
- return ToolsUtils.firstLetterLowerCase(containingElement);
+ String paramName = ToolsUtils.firstLetterLowerCase(name);
+ if (JavaKeywords.isJavaKeyword(paramName))
+ {
+ paramName = "_" + paramName;
+ }
+
+ return paramName;
}
private File getLocationForJavaGeneration()
Modified:
branches/dlofthouse/JBWS-1534/jbossws-tests/src/main/java/org/jboss/test/ws/tools/jbws1534/JBWS1534TestCase.java
===================================================================
---
branches/dlofthouse/JBWS-1534/jbossws-tests/src/main/java/org/jboss/test/ws/tools/jbws1534/JBWS1534TestCase.java 2007-03-05
11:22:47 UTC (rev 2520)
+++
branches/dlofthouse/JBWS-1534/jbossws-tests/src/main/java/org/jboss/test/ws/tools/jbws1534/JBWS1534TestCase.java 2007-03-05
12:04:01 UTC (rev 2521)
@@ -103,7 +103,7 @@
}
JaxrpcMappingValidator mappingValidator = new JaxrpcMappingValidator();
- mappingValidator.validate(resourceDir + "/wrapped-mapping.xml", toolsDir
+ "/wrapped-mapping.xml");
+ mappingValidator.validate(resourceDir + "/jaxrpc-mapping.xml", toolsDir
+ "/jaxrpc-mapping.xml");
}
private static void compareSource(final String expectedName, final String
generatedName) throws Exception
_______________________________________________
jbossws-commits mailing list
jbossws-commits(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/jbossws-commits