use of javax.lang.model.type.DeclaredType in project j2objc by google.
the class CastResolver method endVisit.
/**
* Adds a cast check to compareTo methods. This helps Comparable types behave
* well in sorted collections which rely on Java's runtime type checking.
*/
@Override
public void endVisit(MethodDeclaration node) {
ExecutableElement element = node.getExecutableElement();
if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
return;
}
DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
if (comparableType == null) {
return;
}
List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
List<? extends VariableElement> parameters = element.getParameters();
if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
return;
}
VariableElement param = node.getParameter(0).getVariableElement();
FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
if (castCheck != null) {
node.getBody().addStatement(0, new ExpressionStatement(castCheck));
}
}
use of javax.lang.model.type.DeclaredType in project querydsl by querydsl.
the class AbstractQuerydslProcessor method handleEmbeddedType.
private void handleEmbeddedType(Element element, Set<TypeElement> elements) {
TypeMirror type = element.asType();
if (element.getKind() == ElementKind.METHOD) {
type = ((ExecutableElement) element).getReturnType();
}
String typeName = type.toString();
if (typeName.startsWith(Collection.class.getName()) || typeName.startsWith(List.class.getName()) || typeName.startsWith(Set.class.getName())) {
type = ((DeclaredType) type).getTypeArguments().get(0);
} else if (typeName.startsWith(Map.class.getName())) {
type = ((DeclaredType) type).getTypeArguments().get(1);
}
TypeElement typeElement = typeExtractor.visit(type);
if (typeElement != null && !TypeUtils.hasAnnotationOfType(typeElement, conf.getEntityAnnotations())) {
if (!typeElement.getQualifiedName().toString().startsWith("java.")) {
elements.add(typeElement);
}
}
}
use of javax.lang.model.type.DeclaredType in project querydsl by querydsl.
the class TypeUtils method getAnnotationValuesAsElements.
@SuppressWarnings("unchecked")
public static Set<TypeElement> getAnnotationValuesAsElements(AnnotationMirror mirror, String method) {
Set<TypeElement> elements = new HashSet<TypeElement>();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : mirror.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals(method)) {
List<AnnotationValue> values = ((List) entry.getValue().getValue());
for (AnnotationValue value : values) {
DeclaredType type = (DeclaredType) value.getValue();
elements.add((TypeElement) type.asElement());
}
}
}
return elements;
}
use of javax.lang.model.type.DeclaredType in project querydsl by querydsl.
the class JPAConfiguration method getRealElementType.
private TypeMirror getRealElementType(Element element) {
AnnotationMirror mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToOne.class);
if (mirror == null) {
mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToOne.class);
}
if (mirror != null) {
return TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
}
mirror = TypeUtils.getAnnotationMirrorOfType(element, OneToMany.class);
if (mirror == null) {
mirror = TypeUtils.getAnnotationMirrorOfType(element, ManyToMany.class);
}
if (mirror != null) {
TypeMirror typeArg = TypeUtils.getAnnotationValueAsTypeMirror(mirror, "targetEntity");
TypeMirror erasure = types.erasure(element.asType());
TypeElement typeElement = (TypeElement) types.asElement(erasure);
if (typeElement != null && typeArg != null) {
if (typeElement.getTypeParameters().size() == 1) {
return types.getDeclaredType(typeElement, typeArg);
} else if (typeElement.getTypeParameters().size() == 2) {
if (element.asType() instanceof DeclaredType) {
TypeMirror first = ((DeclaredType) element.asType()).getTypeArguments().get(0);
return types.getDeclaredType(typeElement, first, typeArg);
}
}
}
}
return null;
}
use of javax.lang.model.type.DeclaredType in project roboguice by roboguice.
the class GuiceAnnotationProcessor method addMethodOrConstructorToAnnotationDatabase.
private void addMethodOrConstructorToAnnotationDatabase(String annotationClassName, Element injectionPoint) {
String injectionPointName = injectionPoint.getSimpleName().toString();
for (VariableElement variable : ((ExecutableElement) injectionPoint).getParameters()) {
String parameterTypeName = getTypeName((TypeElement) ((DeclaredType) variable.asType()).asElement());
bindableClasses.add(parameterTypeName);
injectionPointName += ":" + parameterTypeName;
}
TypeElement typeElementRequiringScanning = (TypeElement) injectionPoint.getEnclosingElement();
String typeElementName = getTypeName(typeElementRequiringScanning);
//System.out.printf("Type: %s, injection: %s \n",typeElementName, injectionPointName);
if (injectionPointName.startsWith("<init>")) {
addToInjectedConstructors(annotationClassName, typeElementName, injectionPointName);
} else {
addToInjectedMethods(annotationClassName, typeElementName, injectionPointName);
}
}
Aggregations