aboutsummaryrefslogtreecommitdiff
path: root/java/dagger/internal/codegen/base/ElementFormatter.java
blob: 8e3edc9558f36ad88c4e999e9726b38134e09f49 (plain)
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
/*
 * Copyright (C) 2013 The Dagger 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 dagger.internal.codegen.base;

import static androidx.room.compiler.processing.XElementKt.isField;
import static androidx.room.compiler.processing.XElementKt.isMethodParameter;
import static androidx.room.compiler.processing.XElementKt.isTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.asExecutable;
import static dagger.internal.codegen.xprocessing.XElements.asMethodParameter;
import static dagger.internal.codegen.xprocessing.XElements.asTypeElement;
import static dagger.internal.codegen.xprocessing.XElements.getSimpleName;
import static dagger.internal.codegen.xprocessing.XElements.isExecutable;
import static java.util.stream.Collectors.joining;

import androidx.room.compiler.processing.XElement;
import androidx.room.compiler.processing.XExecutableElement;
import dagger.internal.codegen.xprocessing.XTypes;
import javax.inject.Inject;

/**
 * Formats elements into a useful string representation.
 *
 * <p>Elements directly enclosed by a type are preceded by the enclosing type's qualified name.
 *
 * <p>If the element is a parameter, the returned string will include the enclosing executable,
 * with other parameters elided.
 */
public final class ElementFormatter extends Formatter<XElement> {
  @Inject
  ElementFormatter() {}

  @Override
  public String format(XElement element) {
    return elementToString(element);
  }

  /**
   * Returns a useful string form for an element.
   *
   * <p>Elements directly enclosed by a type are preceded by the enclosing type's qualified name.
   *
   * <p>If the element is a parameter, the returned string will include the enclosing executable,
   * with other parameters elided.
   */
  public static String elementToString(XElement element) {
    return elementToString(element, /* elideMethodParameterTypes= */ false);
  }

  /**
   * Returns a useful string form for an element.
   *
   * <p>Elements directly enclosed by a type are preceded by the enclosing type's qualified name.
   *
   * <p>Parameters are given with their enclosing executable, with other parameters elided.
   */
  public static String elementToString(XElement element, boolean elideMethodParameterTypes) {
    if (isExecutable(element)) {
      return enclosingTypeAndMemberName(element)
          .append(
              elideMethodParameterTypes
                  ? (asExecutable(element).getParameters().isEmpty() ? "()" : "(…)")
                  : asExecutable(element).getParameters().stream()
                      .map(parameter -> XTypes.toStableString(parameter.getType()))
                      .collect(joining(", ", "(", ")")))
          .toString();
    } else if (isMethodParameter(element)) {
      XExecutableElement methodOrConstructor = asMethodParameter(element).getEnclosingElement();
      return enclosingTypeAndMemberName(methodOrConstructor)
          .append('(')
          .append(
              formatArgumentInList(
                  methodOrConstructor.getParameters().indexOf(element),
                  methodOrConstructor.getParameters().size(),
                  getSimpleName(element)))
          .append(')')
          .toString();
    } else if (isField(element)) {
      return enclosingTypeAndMemberName(element).toString();
    } else if (isTypeElement(element)) {
      return asTypeElement(element).getQualifiedName();
    }
    throw new UnsupportedOperationException("Can't determine string for element " + element);
  }

  private static StringBuilder enclosingTypeAndMemberName(XElement element) {
    StringBuilder name = new StringBuilder(elementToString(element.getEnclosingElement()));
    if (!getSimpleName(element).contentEquals("<init>")) {
      name.append('.').append(getSimpleName(element));
    }
    return name;
  }
}