| 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 com.android.tools.chunkio.Chunked; |
| 20 |
|
| 21 |
import javax.lang.model.element.Element; |
| 22 |
import javax.lang.model.element.ElementKind; |
| 23 |
import javax.lang.model.element.ExecutableElement; |
| 24 |
import javax.lang.model.element.TypeElement; |
| 25 |
import javax.lang.model.type.TypeMirror; |
| 26 |
import javax.lang.model.util.ElementFilter; |
| 27 |
import javax.lang.model.util.Types; |
| 28 |
import java.util.List; |
| 29 |
|
| 30 |
public class Validator { |
| 31 |
private final Types typeUtils; |
| 32 |
private final ErrorHandler errorHandler; |
| 33 |
|
| 34 |
public Validator(Types typeUtils, Environment environment) { |
| 35 |
this.typeUtils = typeUtils; |
| 36 |
errorHandler = environment.errorHandler; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Validates to specified type element to ensure we can process the |
| 41 |
* class annotated with {@link Chunked}. |
| 42 |
* |
| 43 |
* A class is not valid if it: |
| 44 |
* - has a super class |
| 45 |
* - is a non-static inner class |
| 46 |
* - is declared in a method |
| 47 |
* - does not have an empty constructor |
| 48 |
* - is abstract |
| 49 |
*/ |
| 50 |
public boolean validate(Element element) { |
| 51 |
TypeElement typeElement = (TypeElement) element; |
| 52 |
|
| 53 |
if (hasSuperclass(typeElement)) return false; |
| 54 |
if (isAbstract(typeElement)) return false; |
| 55 |
if (isDependent(typeElement)) return false; |
| 56 |
|
| 57 |
return hasEmptyConstructor(typeElement); |
| 58 |
} |
| 59 |
|
| 60 |
private boolean hasSuperclass(TypeElement typeElement) { |
| 61 |
TypeMirror superType = typeElement.getSuperclass(); |
| 62 |
TypeElement superClass = (TypeElement) typeUtils.asElement(superType); |
| 63 |
String superName = superClass.getQualifiedName().toString(); |
| 64 |
|
| 65 |
// TODO: support super classes |
| 66 |
if (!superName.equals(Object.class.getCanonicalName())) { |
| 67 |
errorHandler.error(typeElement, |
| 68 |
"Class %s annotated with @%s cannot extend another class", |
| 69 |
typeElement.getQualifiedName()); |
| 70 |
return true; |
| 71 |
} |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
private boolean isAbstract(TypeElement typeElement) { |
| 76 |
if (Utils.isAbstract(typeElement)) { |
| 77 |
errorHandler.error(typeElement, "Class %s annotated with @%s cannot be abstract", |
| 78 |
typeElement.getQualifiedName()); |
| 79 |
return true; |
| 80 |
} |
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
private boolean isDependent(TypeElement typeElement) { |
| 85 |
ElementKind enclosingKind = typeElement.getEnclosingElement().getKind(); |
| 86 |
switch (enclosingKind) { |
| 87 |
case CLASS: |
| 88 |
if (!Utils.isTopLevel(typeElement) && !Utils.isStatic(typeElement)) { |
| 89 |
errorHandler.error(typeElement, |
| 90 |
"Class %s must be static", typeElement.getQualifiedName()); |
| 91 |
return true; |
| 92 |
} |
| 93 |
break; |
| 94 |
case METHOD: |
| 95 |
errorHandler.error(typeElement, "Class %s cannot be declared in a method", |
| 96 |
typeElement.getQualifiedName()); |
| 97 |
return true; |
| 98 |
default: |
| 99 |
break; |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
private boolean hasEmptyConstructor(TypeElement typeElement) { |
| 106 |
boolean hasEmptyConstructor = false; |
| 107 |
List<ExecutableElement> constructors = |
| 108 |
ElementFilter.constructorsIn(typeElement.getEnclosedElements()); |
| 109 |
|
| 110 |
for (ExecutableElement constructor : constructors) { |
| 111 |
if (constructor.getParameters().isEmpty()) { |
| 112 |
hasEmptyConstructor = true; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
if (!hasEmptyConstructor) { |
| 117 |
errorHandler.error(typeElement, "Class %s must have an empty constructor", |
| 118 |
typeElement.getQualifiedName()); |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
return true; |
| 123 |
} |
| 124 |
} |