<commit_before>/*
 * Copyright 2009 the original author or authors.
 *
 * 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.spockframework.compiler;

import java.util.List;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.stmt.Statement;

import static org.spockframework.util.Identifiers.*;
import org.spockframework.compiler.model.*;

import spock.lang.Shared;

/**
 * Given the abstract syntax tree of a Groovy class representing a Spock
 * specification, builds an object model of the specification.
 *
 * @author <NAME>
 */
public class SpecParser implements GroovyClassVisitor {
  private final ErrorReporter errorReporter;

  private Spec spec;
  private int fieldCount = 0;
  private int featureMethodCount = 0;

  public SpecParser(ErrorReporter errorReporter) {
    this.errorReporter = errorReporter;
  }

  public Spec build(ClassNode clazz) {
    spec = new Spec(clazz);
    clazz.visitContents(this);
    return spec;
  }

  public void visitClass(ClassNode clazz) {
   throw new UnsupportedOperationException("visitClass");
  }

  public void visitField(FieldNode gField) {
    PropertyNode owner = spec.getAst().getProperty(gField.getName());
    // precaution against internal fields that aren't marked as synthetic
    if (gField.getName().startsWith("$")) return;
    if (gField.isStatic() || (gField.isSynthetic() && owner == null)) return;

    Field field = new Field(spec, gField, fieldCount++);
    field.setShared(AstUtil.hasAnnotation(gField, Shared.class));
    field.setOwner(owner);
    spec.getFields().add(field);
  }

  public void visitProperty(PropertyNode node) {}

  public void visitConstructor(ConstructorNode constructor) {
    if (AstUtil.isSynthetic(constructor)) return;

    errorReporter.error(constructor,
"Constructors are not allowed; instead, define a 'setup()' or 'setupSpec()' method");
   }

  public void visitMethod(MethodNode method) {
    if (isIgnoredMethod(method)) return;
    
    if (isFixtureMethod(method))<commit_msg>added support for user-defined fields that start with $<commit_after>/*
 * Copyright 2009 the original author or authors.
 *
 * 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.spockframework.compiler;

import java.util.List;

import org.codehaus.groovy.ast.*;
import org.codehaus.groovy.ast.expr.ConstantExpression;
import org.codehaus.groovy.ast.stmt.Statement;
import org.objectweb.asm.Opcodes;

import static org.spockframework.util.Identifiers.*;
import org.spockframework.compiler.model.*;

import spock.lang.Shared;

/**
 * Given the abstract syntax tree of a Groovy class representing a Spock
 * specification, builds an object model of the specification.
 *
 * @author <NAME>
 */
public class SpecParser implements GroovyClassVisitor {
  private final ErrorReporter errorReporter;

  private Spec spec;
  private int fieldCount = 0;
  private int featureMethodCount = 0;

  public SpecParser(ErrorReporter errorReporter) {
    this.errorReporter = errorReporter;
  }

  public Spec build(ClassNode clazz) {
    spec = new Spec(clazz);
    clazz.visitContents(this);
    return spec;
  }

  public void visitClass(ClassNode clazz) {
   throw new UnsupportedOperationException("visitClass");
  }

  // might only want to include fields relating to a user-provided
  // definition, but it's hard to tell them apart; for example,
  // field.isSynthetic() is true for a property's backing field
  // although it IS related to a user-provided definition
  public void visitField(FieldNode gField) {
    PropertyNode owner = spec.getAst().getProperty(gField.getName());
    if (gField.isStatic() || (gField.getModifiers() & Opcodes.ACC_STATIC) != 0) return;

    Field field = new Field(spec, gField, fieldCount++);
    field.setShared(AstUtil.hasAnnotation(gField, Shared.class));
    field.setOwner(owner);
    spec.getFields().add(field);
  }

  public void visitProperty(PropertyNode node) {}

  public void visitConstructor(ConstructorNode constructor) {
    if (AstUtil.isSynthetic(constructor)) return;

    errorReporter.error(constructor,
"Constructors are not allowed; instead, define a 'setup()' or 'setupSpec()' method");
  }

  public void visitMethod(MethodNode method) {
    if (isIgnoredMethod(method)) return;
    
    if (isFixtureMethod(method))
{'fraction_non_alphanumeric': 0.09574468085106383, 'fraction_numerical': 0.003919372900335946, 'mean_word_length': 4.259077526987243, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1565779', 'n_tokens_mistral': 1535, 'n_tokens_neox': 1467, 'n_words': 501}