All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.errorprone.refaster.UMethodDecl Maven / Gradle / Ivy

There is a newer version: 2.27.1
Show newest version
/*
 * Copyright 2013 The Error Prone 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 com.google.errorprone.refaster;

import static com.google.errorprone.refaster.Unifier.unifications;

import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.TreeVisitor;
import com.sun.source.tree.TypeParameterTree;
import com.sun.source.tree.VariableTree;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCTypeParameter;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.List;
import javax.annotation.Nullable;

/**
 * {@code UTree} representation of a {@code MethodTree}.
 *
 * @author [email protected] (Louis Wasserman)
 */
@AutoValue
abstract class UMethodDecl extends UTree implements MethodTree {
  public static UMethodDecl create(
      UModifiers modifiers,
      CharSequence name,
      UExpression returnType,
      Iterable parameters,
      Iterable thrown,
      UBlock body) {
    return new AutoValue_UMethodDecl(
        modifiers,
        StringName.of(name),
        returnType,
        ImmutableList.copyOf(parameters),
        ImmutableList.copyOf(thrown),
        body);
  }

  @Override
  public  R accept(TreeVisitor visitor, D data) {
    return visitor.visitMethod(this, data);
  }

  @Override
  public Kind getKind() {
    return Kind.METHOD;
  }

  @Override
  @Nullable
  public Choice visitMethod(MethodTree decl, @Nullable Unifier unifier) {
    return getName()
        .unify(decl.getName(), unifier)
        .thenChoose(unifications(getReturnType(), decl.getReturnType()))
        .thenChoose(unifications(getParameters(), decl.getParameters()))
        .thenChoose(unifications(getThrows(), decl.getThrows()))
        .thenChoose(unifications(getBody(), decl.getBody()));
  }

  @Override
  public JCMethodDecl inline(Inliner inliner) throws CouldNotResolveImportException {
    return inliner
        .maker()
        .MethodDef(
            getModifiers().inline(inliner),
            getName().inline(inliner),
            getReturnType().inline(inliner),
            List.nil(),
            List.convert(JCVariableDecl.class, inliner.inlineList(getParameters())),
            inliner.inlineList(getThrows()),
            getBody().inline(inliner),
            null);
  }

  @Override
  public abstract UModifiers getModifiers();

  @Override
  public abstract StringName getName();

  @Override
  public abstract UExpression getReturnType();

  @Override
  public abstract ImmutableList getParameters();

  @Override
  public abstract ImmutableList getThrows();

  @Override
  public abstract UBlock getBody();

  @Override
  public UTree getDefaultValue() {
    return null;
  }

  @Override
  public ImmutableList getTypeParameters() {
    return ImmutableList.of();
  }

  @Override
  public VariableTree getReceiverParameter() {
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy