/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.groovy.lang.resolve.ast.builder.strategy;

import com.intellij.codeInsight.AnnotationUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiModifier;
import com.intellij.psi.impl.light.LightMethodBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable;
import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition;
import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil;
import org.jetbrains.plugins.groovy.lang.resolve.ast.builder.GrBuilderStrategySupport;

public class SimpleBuilderStrategySupport extends GrBuilderStrategySupport {

  public static final String SIMPLE_STRATEGY_FQN = "groovy.transform.builder.SimpleStrategy";

  @NotNull
  @Override
  public Members process(GrTypeDefinition typeDefinition) {
    if (!SIMPLE_STRATEGY_FQN.equals(getStrategy(typeDefinition))) return Members.EMPTY;
    final PsiAnnotation annotation = PsiImplUtil.getAnnotation(typeDefinition, BUILDER_FQN);
    assert annotation != null;
    final Members result = new Members();
    for (GrField field : typeDefinition.getCodeFields()) {
      result.methods.add(createFieldSetter(typeDefinition, field, annotation));
    }
    return result;
  }

  @NotNull
  public static LightMethodBuilder createFieldSetter(@NotNull PsiClass builderClass, @NotNull GrVariable field, @NotNull PsiAnnotation annotation) {
    final String name = field.getName();
    final LightMethodBuilder fieldSetter = new LightMethodBuilder(builderClass.getManager(), getFieldMethodName(annotation, name));
    fieldSetter.addModifier(PsiModifier.PUBLIC);
    fieldSetter.addParameter(name, field.getType(), false);
    fieldSetter.setContainingClass(builderClass);
    fieldSetter.setMethodReturnType(JavaPsiFacade.getElementFactory(builderClass.getProject()).createType(builderClass));
    fieldSetter.setNavigationElement(field);
    fieldSetter.setOriginInfo(ORIGIN_INFO);
    return fieldSetter;
  }

  @NotNull
  public static String getFieldMethodName(@NotNull PsiAnnotation annotation, @NotNull String fieldName) {
    final String prefix = AnnotationUtil.getDeclaredStringAttributeValue(annotation, "prefix");
    return String.format("%s%s", prefix == null ? "set" : prefix, StringUtil.capitalize(fieldName));
  }
}

@@ -2600,67 +2600,8 @@
 ype(
-JavaPsiFacade.getElementFactory(builderClass.getProject()).
 crea
@@ -2969,30 +2969,8 @@
 urn 
-String.format(%22%25s%25s%22, 
 pref
@@ -2987,16 +2987,153 @@
  ? %22set%22
+ + StringUtil.capitalize(fieldName)%0A                          : prefix.isEmpty() ? fieldName%0A                                            
  : prefi
@@ -3133,17 +3133,18 @@
 : prefix
-,
+ +
  StringU
@@ -3164,21 +3164,20 @@
 e(fieldName)
-)
 ;%0A  %7D%0A%7D%0A

fix processing of @Builder attributes for SimpleStrategy