| 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.Element; |
| 20 |
import javax.lang.model.element.ElementKind; |
| 21 |
import javax.lang.model.element.Modifier; |
| 22 |
import javax.lang.model.element.TypeElement; |
| 23 |
import java.io.PrintWriter; |
| 24 |
import java.io.StringWriter; |
| 25 |
|
| 26 |
final class Utils { |
| 27 |
private Utils() { |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Returns true if the specified string contains multiple lines |
| 32 |
* delimited by '\n'. |
| 33 |
*/ |
| 34 |
static boolean isMultiline(String statement) { |
| 35 |
for (int i = 0; i < statement.length(); i++) { |
| 36 |
if (statement.charAt(i) == '\n') return true; |
| 37 |
} |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Creates a variable name from a simple class name (not fully qualified). |
| 43 |
* This simply ensures the first character is lower case. |
| 44 |
*/ |
| 45 |
static String variableName(String name) { |
| 46 |
return Character.toLowerCase(name.charAt(0)) + name.substring(1).replace('.', '_'); |
| 47 |
} |
| 48 |
|
| 49 |
static boolean isStatic(Element element) { |
| 50 |
return element.getModifiers().contains(Modifier.STATIC); |
| 51 |
} |
| 52 |
|
| 53 |
static boolean isAbstract(Element element) { |
| 54 |
return element.getModifiers().contains(Modifier.ABSTRACT); |
| 55 |
} |
| 56 |
|
| 57 |
static boolean isTopLevel(TypeElement typeElement) { |
| 58 |
return typeElement.getEnclosingElement().getKind() == ElementKind.PACKAGE; |
| 59 |
} |
| 60 |
|
| 61 |
static String stackTraceToString(Exception e) { |
| 62 |
StringWriter writer = new StringWriter(); |
| 63 |
e.printStackTrace(new PrintWriter(writer)); |
| 64 |
return writer.toString(); |
| 65 |
} |
| 66 |
} |