Uploaded image for project: 'Spring Roo'
  1. Spring Roo
  2. ROO-1375

JavaParserMethodMetadata.addMethod allows to add duplicated methods to a class

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 1.1.0.M1
    • Fix Version/s: 1.1.0.RC1
    • Component/s: @ CORE
    • Labels:
      None
    • Environment:
      SUN JDK 1.5.0_07
      Ubuntu 10.04
      Eclipse 3.5.1

      Description

      Hi!
      I found a bug related to JavaParserMethodMetadata addMethod.

      When I use JavaParserMethodMetadata.addMethod(..) to create a method with the same number, type and name of parameters and return type the method is created. This is not the correct behaviour, the method has to warn me that the method is duplicated.

      When I debugged the code I found taht the problem is: after creating the annotation for input parameters in the method inside the 'for' in *line 225*:

      JavaParserMethodMetadata.java
      for (AnnotatedJavaType methodParameter : method.getParameterTypes()) {
      	index++;
      ...
      }
      

      The problem is that the variable index never is reset and only compares the method type of the first parameter (of hte method we are creating) with the method type of the last parameter of the method in the class and never matches, never find a duplicated method with parameters because of this.

      When the method tries to locate where to add the method and also verify if this method already exists, always adds the method to the class because the order of the parameters to compare never is correct (if (!existingParameter.getJavaType().equals(method.getParameterTypes().get(index)))):

      JavaParserMethodMetadata.java
      // Locate where to add this method; also verify if this method already exists
      for (BodyDeclaration bd : members) {
      	if (bd instanceof MethodDeclaration) {
      		// Next method should appear after this current method
      		MethodDeclaration md = (MethodDeclaration) bd;
      		if (md.getName().equals(d.getName()) && md.getParameters().size() == d.getParameters().size()) {
      			// Possible match, we need to consider parameter types as well now
      			JavaParserMethodMetadata jpmm = new JavaParserMethodMetadata(method.getDeclaredByMetadataId(), md, compilationUnitServices, typeParameters);
      			boolean matchesFully = true;
      			for (AnnotatedJavaType existingParameter : jpmm.getParameterTypes()) {
      				if (!existingParameter.getJavaType().equals(method.getParameterTypes().get(index))) {
      					matchesFully = false;
      					break;
      				}
      			}
      			if (matchesFully) {
      				throw new IllegalStateException("Method '" + method.getMethodName().getSymbolName() + "' already exists with identical parameters");
      			}
      		}
      	}
      }
      

      Thanks!

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              stewarta Alan Stewart
              Reporter:
              ricardogarfe Ricardo García
              Votes:
              3 Vote for this issue
              Watchers:
              1 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: