Debian Codegraph @

chunkio-processor/src/com/android/tools/chunkio/processor/ClassName.java

raw
1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package com.android.tools.chunkio.processor;
18
19 import javax.lang.model.element.ElementKind;
20 import javax.lang.model.element.PackageElement;
21 import javax.lang.model.element.TypeElement;
22 import javax.lang.model.util.Elements;
23 import java.io.PrintWriter;
24
25 /**
26 * Holds a generated class name.
27 */
28 final class ClassName {
29 private static final String GENERATED_CLASS_SUFFIX = "$$ChunkIO";
30
31 /** Package where to generate the class. */
32 final String packageName;
33 /** Name of the generated class. */
34 final String className;
35 /** Fully qualified name of the generated class. */
36 final String qualifiedName;
37 /** Name of the class this generated name was derived from. */
38 final String sourceName;
39
40 private ClassName(String packageName, String className, String sourceName) {
41 this.packageName = packageName;
42 this.className = className;
43 this.qualifiedName = packageName.isEmpty() ? className : packageName + '.' + className;
44 this.sourceName = sourceName;
45 }
46
47 /**
48 * Generates a class name for generation from the specified type element.
49 * The generated class name will have the same package as the type element.
50 * The generated class name will be the type element's class name with
51 * all '.' instances replaced with '$'.
52 * The generated class name will be suffixed with "$$ChunkIO".
53 *
54 * For instance, if the source type element is a class called
55 * com.myapp.MyClass, the generated class name will be:
56 * com.myapp.MyClass$$ChunkIO.
57 *
58 * If the source type element is an inner class called
59 * com.myapp.Outer.Inner, the generated class name will be:
60 * com.myapp.Outer$Inner$$ChunkIO.
61 */
62 static ClassName from(TypeElement typeElement, Elements elementUtils) {
63 PackageElement packageElement = elementUtils.getPackageOf(typeElement);
64 String typeName = typeElement.getQualifiedName().toString();
65
66 String sourceName;
67 String className;
68 String packageName = packageElement.getQualifiedName().toString();
69
70 int length = packageName.length();
71 if (length == 0) {
72 sourceName = typeName;
73 className = typeName + GENERATED_CLASS_SUFFIX;
74 } else {
75 sourceName = typeName.substring(length + 1);
76 className = sourceName.replace('.', '$') + GENERATED_CLASS_SUFFIX;
77 }
78
79 return new ClassName(packageName, className, sourceName);
80 }
81 }