package org.apache.beehive.wsm.processor.apt;
/*
* Copyright 2004 The Apache Software Foundation
*
* 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.
*
* $Header:$Factory
*/
import java.io.File;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Collection;
import com.sun.mirror.declaration.AnnotationMirror;
import com.sun.mirror.declaration.ClassDeclaration;
import com.sun.mirror.declaration.ConstructorDeclaration;
import com.sun.mirror.declaration.InterfaceDeclaration;
import com.sun.mirror.declaration.MethodDeclaration;
import com.sun.mirror.declaration.Modifier;
import com.sun.mirror.declaration.TypeDeclaration;
import com.sun.mirror.apt.AnnotationProcessorEnvironment;
import com.sun.mirror.type.AnnotationType;
import org.apache.beehive.wsm.model.java.JavaMethodInfo;
import org.apache.beehive.wsm.model.java.JavaTypeInfo;
public class MirrorTypeInfo implements JavaTypeInfo {
protected TypeDeclaration decl;
protected AnnotationProcessorEnvironment env;
protected Collection<JavaMethodInfo> methods = new ArrayList<JavaMethodInfo>();
public MirrorTypeInfo(TypeDeclaration decl, AnnotationProcessorEnvironment env) {
if (null == decl) {
throw new IllegalArgumentException("type declaration: <null>");
}
this.decl = decl;
this.env = env;
Collection<? extends MethodDeclaration> mdecls = decl.getMethods();
if (null != mdecls) {
for (MethodDeclaration mdecl : mdecls) {
methods.add(new MirrorMethodInfo(mdecl, env));
}
}
}
public void logError(String msg) {
env.getMessager().printError(decl.getPosition(), msg);
}
public String getName() {
return decl.getQualifiedName();
}
public File getLocation() {
return decl.getPosition().file().getParentFile();
}
public <A extends Annotation> A getAnnotation(Class<A> annotationType) {
return decl.getAnnotation(annotationType);
}
public Collection<Annotation> getAnnotations() {
Collection<Annotation> annotations = new ArrayList<Annotation>();
for (AnnotationMirror am : decl.getAnnotationMirrors()) {
AnnotationType at = am.getAnnotationType();
try {
Class clazz = Class.forName(at.getDeclaration().getQualifiedName());
Annotation a = decl.getAnnotation(clazz);
if (null != a) {
annotations.add(a);
}
} catch (ClassNotFoundException e) {
}
}
return annotations;
}
public Collection<JavaMethodInfo> getMethods() {
return methods;
}
public boolean hasDefaultConstructor() {
boolean result = false;
if (decl instanceof InterfaceDeclaration) {
result = false;
}
else {
ClassDeclaration cdecl = (ClassDeclaration) decl;
for (ConstructorDeclaration c : cdecl.getConstructors()) {
if (c.getParameters().isEmpty()) {
result = true;
break;
}
}
}
return result;
}
public boolean hasFinalize() {
boolean result = false;
for (MethodDeclaration mdecl : decl.getMethods()) {
if (mdecl.getSimpleName().equals("finalize") && (null == mdecl.getParameters() || mdecl.getParameters().isEmpty())) {
result = true;
}
}
return result;
}
public boolean isAbstract() {
return decl.getModifiers().contains(Modifier.ABSTRACT);
}
public boolean isFinal() {
return decl.getModifiers().contains(Modifier.FINAL);
}
public boolean isPublic() {
return decl.getModifiers().contains(Modifier.PUBLIC);
}
public boolean isInterface() {
return (decl instanceof InterfaceDeclaration);
}
}