Author: scabanovich
Date: 2007-08-02 06:54:34 -0400 (Thu, 02 Aug 2007)
New Revision: 2839
Modified:
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/MethodDescriptor.java
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/StrutsGenerator.java
Log:
Type structure processed using IType instead of java.lang.Class
Modified:
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/MethodDescriptor.java
===================================================================
---
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/MethodDescriptor.java 2007-08-02
10:26:07 UTC (rev 2838)
+++
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/MethodDescriptor.java 2007-08-02
10:54:34 UTC (rev 2839)
@@ -10,71 +10,92 @@
******************************************************************************/
package org.jboss.tools.struts.model.helpers;
-import java.lang.reflect.Constructor;
-import java.lang.reflect.Modifier;
+import org.eclipse.jdt.core.Flags;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.JavaModelException;
+import org.jboss.tools.common.model.util.EclipseJavaUtil;
-
-public class MethodDescriptor
-{
- private Constructor constructor;
+public class MethodDescriptor {
+ private IMethod constructor;
- public MethodDescriptor(Constructor constructor)
- {
+ public MethodDescriptor(IMethod constructor) {
this.constructor = constructor;
}
- public String getName()
- {
- return constructor.getName();
+ public String getName() {
+ return constructor.getElementName();
}
- public String getParameters()
- {
+ public String getParameters() {
StringBuffer result = new StringBuffer();
- Class parameters[] = constructor.getParameterTypes();
- for (int i = 0; i < parameters.length; i++)
- result.append('p').append(i).append((i == parameters.length - 1) ?
"" : ", ");
+ String[] ps = constructor.getParameterTypes();
+
+ for (int i = 0; i < ps.length; i++) {
+ String type = EclipseJavaUtil.resolveType(constructor.getDeclaringType(), ps[i]);
+ if(type == null) type = ps[i];
+ result.append('p').append(i).append((i == ps.length - 1) ? "" :
", ");
+ }
return result.toString();
}
- public String getParametersWithType()
- {
+ public String getParametersWithType() {
StringBuffer result = new StringBuffer();
- Class parameters[] = constructor.getParameterTypes();
- for (int i = 0; i < parameters.length; i++)
- {
- if (parameters[i].isArray())
- result.append(parameters[i].getComponentType().getName()).append("[]");
- else
- result.append(parameters[i].getName());
+ String[] ps = constructor.getParameterTypes();
+
+ for (int i = 0; i < ps.length; i++) {
+ String type = EclipseJavaUtil.resolveType(constructor.getDeclaringType(), ps[i]);
+ if(type == null) type = ps[i];
+ type = convertType(type);
+ result.append(type);
result.append(' ').append('p').append(i);
- if (i < parameters.length - 1) result.append(", ");
+ if (i < ps.length - 1) result.append(", ");
}
return result.toString();
}
+ private String convertType(String type) {
+ String postfix = "";
+ while(type.startsWith("[")) {
+ postfix += "[]";
+ type = type.substring(1);
+ }
+ if(type.startsWith("L") && type.endsWith(";")) {
+ return type.substring(1, type.length() - 1) + postfix;
+ }
+ if(type.equals("I")) {
+ type = "int";
+ }
+ return type + postfix;
+ }
+
public String getModifiers()
{
StringBuffer result = new StringBuffer();
- int modifiers = constructor.getModifiers();
+ int modifiers = 0;
+ try {
+ modifiers = constructor.getFlags();
+ } catch (JavaModelException e) {
+ //ignore
+ }
- if ((Modifier.PUBLIC & modifiers) != 0)
+ if (Flags.isPublic(modifiers))
result.append("public");
- else if ((Modifier.PRIVATE & modifiers) != 0)
+ else if (Flags.isPrivate(modifiers))
result.append("private");
- else if ((Modifier.PROTECTED & modifiers) != 0)
+ else if (Flags.isProtected(modifiers))
result.append("protected");
- if ((Modifier.STATIC & modifiers) != 0)
+ if (Flags.isStatic(modifiers))
result.append(result.length() > 0 ? " " :
"").append("static");
- if ((Modifier.FINAL & modifiers) != 0)
+ if (Flags.isFinal(modifiers))
result.append(result.length() > 0 ? " " :
"").append("final");
return result.toString();
}
+
}
Modified:
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/StrutsGenerator.java
===================================================================
---
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/StrutsGenerator.java 2007-08-02
10:26:07 UTC (rev 2838)
+++
trunk/struts/plugins/org.jboss.tools.struts/src/org/jboss/tools/struts/model/helpers/StrutsGenerator.java 2007-08-02
10:54:34 UTC (rev 2839)
@@ -10,7 +10,6 @@
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
-import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -26,12 +25,19 @@
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaProject;
+import org.eclipse.jdt.core.IMethod;
+import org.eclipse.jdt.core.IType;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.osgi.util.NLS;
import org.jboss.tools.common.model.ServiceDialog;
import org.jboss.tools.common.model.XModel;
import org.jboss.tools.common.model.XModelObject;
import org.jboss.tools.common.model.options.PreferenceModelUtilities;
+import org.jboss.tools.common.model.util.EclipseJavaUtil;
+import org.jboss.tools.common.model.util.EclipseResourceUtil;
import org.jboss.tools.struts.StrutsConstants;
import org.jboss.tools.struts.StrutsModelPlugin;
import org.jboss.tools.struts.messages.StrutsUIMessages;
@@ -97,7 +103,7 @@
if (!checkOverwrite(source, forward.getModel())) return null;
targetPath = source.getAbsolutePath();
- ArrayList properties = getProperties(forward, genProps);
+ ArrayList<PropertyDescriptor> properties = getProperties(forward, genProps);
Map<String,Object> parameters = getParameters(forward, className, baseClass,
properties);
executeTemplate(parameters, templatePath, targetPath);
@@ -120,7 +126,7 @@
if (!checkOverwrite(source, exception.getModel())) return null;
targetPath = source.getAbsolutePath();
- ArrayList properties = getProperties(exception, genProps);
+ ArrayList<PropertyDescriptor> properties = getProperties(exception, genProps);
Map<String,Object> parameters = getParameters(exception, className, baseClass,
properties);
parameters.put("constructors", getConstructors(exception, baseClass));
//$NON-NLS-1$
@@ -145,7 +151,7 @@
if (!checkOverwrite(source, formBean.getModel())) return null;
targetPath = source.getAbsolutePath();
- ArrayList properties = getProperties(formBean, genProps);
+ ArrayList<PropertyDescriptor> properties = getProperties(formBean, genProps);
Map<String,Object> parameters = getParameters(formBean, className, baseClass,
properties);
executeTemplate(parameters, templatePath, targetPath);
@@ -173,7 +179,7 @@
return velocityEngine;
}
- private void executeTemplate(Map parameters, String templatePath, String targetPath) {
+ private void executeTemplate(Map<String,Object> parameters, String templatePath,
String targetPath) {
ServiceDialog d = PreferenceModelUtilities.getPreferenceModel().getService();
try {
VelocityContext context = new VelocityContext(parameters);
@@ -234,7 +240,7 @@
if (!checkOverwrite(source, action.getModel())) return null;
targetPath = source.getAbsolutePath();
- ArrayList<Object> properties = getProperties(action, genProps);
+ ArrayList<PropertyDescriptor> properties = getProperties(action, genProps);
ArrayList<Object> globalForwards = null;
if (genGlobal) {
@@ -269,16 +275,22 @@
return className;
}
- private List getConstructors(XModelObject object, String baseClass)
- {
- List<Object> constructors = new ArrayList<Object>();
+ private List<MethodDescriptor> getConstructors(XModelObject object, String
baseClass) {
+ List<MethodDescriptor> constructors = new
ArrayList<MethodDescriptor>();
- ClassLoader classLoader = object.getModel().getModelClassLoader();
try {
- Class classObject = classLoader.loadClass(baseClass);
- Constructor constr[] = classObject.getConstructors();
- for (int i = 0; i < constr.length; i++) constructors.add(new
MethodDescriptor(constr[i]));
- } catch (ClassNotFoundException ex) {
+ IJavaProject javaProject =
EclipseResourceUtil.getJavaProject(EclipseResourceUtil.getProject(object));
+ IType type = EclipseJavaUtil.findType(javaProject, baseClass);
+ if(type == null) return constructors;
+ IJavaElement[] ch = type.getChildren();
+ for (int i = 0; i < ch.length; i++) {
+ if(!(ch[i] instanceof IMethod)) continue;
+ IMethod method = (IMethod)ch[i];
+ if(!method.isConstructor()) continue;
+ constructors.add(new MethodDescriptor(method));
+
+ }
+ } catch (JavaModelException ex) {
StrutsModelPlugin.getPluginLog().logError(ex);
}
@@ -332,7 +344,7 @@
return (ind > 0) ? fullName.substring(0, ind) : ""; //$NON-NLS-1$
}
- private Map<String,Object> getParameters(XModelObject o, String className, String
baseClass, ArrayList properties) {
+ private Map<String,Object> getParameters(XModelObject o, String className, String
baseClass, ArrayList<PropertyDescriptor> properties) {
Map<String,Object> parameters = new HashMap<String,Object>();
parameters.put("package", getPackageName(className)); //$NON-NLS-1$
parameters.put("className", getClassName(className)); //$NON-NLS-1$
@@ -341,10 +353,10 @@
return parameters;
}
- private ArrayList<Object> getProperties(XModelObject o, boolean genProps) {
- ArrayList<Object> p = null;
+ private ArrayList<PropertyDescriptor> getProperties(XModelObject o, boolean
genProps) {
+ ArrayList<PropertyDescriptor> p = null;
if (genProps) {
- p = new ArrayList<Object>();
+ p = new ArrayList<PropertyDescriptor>();
XModelObject[] props = o.getChildren(ENT_SETPROPERTY); /// one entity for both
versions
for (int i = 0; i < props.length; i++) {
String name = props[i].getAttributeValue(ATT_PROPERTY);