Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 0c52993432da35d2489e26c7fadf8f1645cc6630 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20080505   182167 makandre@ca.ibm.com - Andrew Mak, Warning not issued when non-instantiable class is bypassed in sampe JSPs
 *******************************************************************************/

package org.eclipse.jst.ws.internal.consumption.codegen.javamofvisitors;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jem.java.Field;
import org.eclipse.jem.java.JavaClass;
import org.eclipse.jem.java.JavaHelpers;
import org.eclipse.jem.java.JavaParameter;
import org.eclipse.jem.java.Method;
import org.eclipse.jst.ws.internal.consumption.codegen.Visitor;
import org.eclipse.jst.ws.internal.consumption.codegen.VisitorAction;
import org.eclipse.jst.ws.internal.consumption.command.common.JavaMofReflectionCommand;
import org.eclipse.jst.ws.internal.consumption.datamodel.beanmodel.TypeFactory;
import org.eclipse.jst.ws.internal.consumption.sampleapp.common.SamplePropertyDescriptor;
import org.eclipse.wst.common.environment.IEnvironment;


/**
* Objects of this class represent a visitor.
* */
public class JavaMofAttributeVisitor implements Visitor 
{

  // Copyright
  public static final String copyright = "(c) Copyright IBM Corporation 2000, 2002.";


  private String clientProject;
  private boolean returnParameter;
  private IEnvironment env;
  
  
  public void setEnvironment(IEnvironment env)
  {
  	this.env = env;
  }
  
  /*
  * Use this to reflect 
  */
  public void setProject(String clientProject)
  {
    this.clientProject = clientProject;
  }

   /*
  * Use this to reflect 
  */
  public String getProject()
  {
    return clientProject;
  }

  public void setReturnParameter(boolean returnParameter)
  {
    this.returnParameter = returnParameter;
  }
 
  
  /*
  * Get the attribute belonging to this complex type 
  * @param JavaParameter javaParameter that owns the type
  * @param VisitorAction Action to be performed on each method
  **/
  public IStatus run ( Object javaclass, VisitorAction vAction)
  {
  	IStatus status = Status.OK_STATUS;
    JavaClass javaClass = (JavaClass)javaclass;     

    Hashtable spdMap = new Hashtable();  
    for (Iterator m=javaClass.getPublicMethods().iterator(); m.hasNext(); ) {
      Method method=(Method)m.next();
      if (((method.getMethodElementSignature().startsWith("get") 
      	|| method.getMethodElementSignature().startsWith("is")) 
        && method.listParametersWithoutReturn().length == 0 )
        || (method.getMethodElementSignature().startsWith("set") 
        && method.listParametersWithoutReturn().length > 0)) {
        int sub = 3;
        if(method.getMethodElementSignature().startsWith("is"))
          sub = 2;	
        String propertyName = method.getMethodElementSignature().substring(sub);    
        String lower = propertyName.substring(0,1).toLowerCase();
        String remainder = propertyName.substring(1); 
        propertyName = lower + remainder;
        int index = propertyName.lastIndexOf("(");
        String temp = propertyName;
        propertyName = temp.substring(0,index); 
        
        //find the propertydescriptor
        SamplePropertyDescriptor spd = (SamplePropertyDescriptor)spdMap.get(propertyName); 
        if(spd == null){
          spd = new SamplePropertyDescriptor(propertyName);
          spdMap.put(propertyName,spd); 
        }
        if(method.getMethodElementSignature().startsWith("set"))
          spd.setWriteMethod(method);
        else 
          spd.setReadMethod(method);

        Field field = javaClass.getFieldNamed(propertyName);
        JavaHelpers propertyType = null; 
        if(field != null){
          propertyType = (JavaHelpers)field.getEType();
          spd.setfStatic(field.isStatic());
        }
        else{
          if(method.getMethodElementSignature().startsWith("get") || method.getMethodElementSignature().startsWith("is"))
          	propertyType = method.getReturnType();
          else{
             JavaParameter params[] = method.listParametersWithoutReturn(); 
             propertyType = params[0].getJavaType();
          }  
        }
          	
        spd.setPropertyType(propertyType); 
        
      }
    }
    Enumeration spdEnum = spdMap.elements();
    while(spdEnum.hasMoreElements()){
      SamplePropertyDescriptor spd = (SamplePropertyDescriptor)spdEnum.nextElement();
      if(spdCheck(spd)) {
        status = vAction.visit(spd);   
        if (status.getSeverity() == IStatus.ERROR)
    	  break;
      }
    } 
   
    return status;

  }
  
  private boolean spdCheck(SamplePropertyDescriptor spd)
  {  
    //check for indexed properties
  	Method writeMethod = spd.getWriteMethod();
  	if(writeMethod != null)
  	  if(writeMethod.listParametersWithoutReturn().length > 1) return false;
  	
  	if(spd.getPropertyType() == null) return false;
    if(spd.getPropertyType().isPrimitive()) return true;
    if(returnParameter && TypeFactory.isRecognizedReturnType(spd.getPropertyType())) return true;
    if(!returnParameter && TypeFactory.isUnSupportedType(spd.getPropertyType())) return true;
    if(!checkPolarity(spd)) return false;   
       
    // so its a bean make sure it has a default constructor       
    JavaMofReflectionCommand javaMofRef = new JavaMofReflectionCommand();
    javaMofRef.setClientProject(getProject());
    javaMofRef.setProxyBean(((JavaHelpers)spd.getPropertyType()).getQualifiedName());
    javaMofRef.setEnvironment( env );
    javaMofRef.execute(null, null);
    if(javaMofRef.getJavaClass() instanceof JavaClass){
       if(TypeFactory.recognizedBean(javaMofRef.getJavaClass().getJavaName())) return true;
       return defaultCheck((JavaClass)javaMofRef.getJavaClass());
               
    }
    return true;
  
  }
  
  private boolean checkPolarity(SamplePropertyDescriptor spd){
    
  	Method readMethod = spd.getReadMethod();
  	Method writeMethod = spd.getWriteMethod();
  	if((readMethod == null && writeMethod != null) || (readMethod != null && writeMethod == null))
  	  return true;	
  	if(readMethod == null && writeMethod == null)
  	  return false;	
  	  	
  	JavaParameter javaParameter[] = writeMethod.listParametersWithoutReturn();
    for(int i = 0;i< javaParameter.length;i++){
      JavaParameter jp = javaParameter[i]; 	
      jp.getJavaType().getJavaName();
      readMethod.getReturnType().getJavaName();
      if(jp.getJavaType().getJavaName().equals(readMethod.getReturnType().getJavaName()))
        return true; 
    }
  	return false;
  }
  
  private boolean defaultCheck(JavaClass javaClass){
    
       Iterator m=javaClass.getMethods().iterator();
        //now check for a default constructor
        boolean defaultConst = true;
        while (m.hasNext()) {
          Method method=(Method)m.next();
          if (javaClass.getName().equals(method.getName())){
                //now the inputs
                JavaParameter javaParameter[] = method.listParametersWithoutReturn();
                if (javaParameter.length > 0){
                      //then we have no default constructor
                      defaultConst = false; 
                }
                else if(javaParameter.length == 0){
                     if (method.getJavaVisibility().getValue() == 0)
                       return true;
                }
          }
       }
    
       return defaultConst;
  }
          
}

Back to the top