/*
* Copyright 2002-2008 the original author or authors.
*
* 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.
*/
package org.gwtoolbox.ioc.core.rebind.processor.javaconfig.inspect;
import java.lang.annotation.Annotation;
import java.util.HashMap;
import java.util.Map;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JField;
import com.google.gwt.core.ext.typeinfo.JMethod;
import org.gwtoolbox.commons.generator.rebind.Criteria;
import org.gwtoolbox.commons.generator.rebind.GeneratorUtils;
import org.gwtoolbox.ioc.core.client.annotation.*;
import org.gwtoolbox.commons.generator.rebind.StringUtils;
/**
* @author Uri Boness
*/
public class DefaultJavaConfigInspector implements JavaConfigInspector {
public boolean isComponent(JMethod method) {
return method.isAnnotationPresent(Component.class);
}
public boolean isLazy(JMethod method) {
Component component = method.getAnnotation(Component.class);
return component.lazyInit();
}
public boolean isSingleton(JMethod method) {
Component component = method.getAnnotation(Component.class);
return component.scope() == Scope.SINGLETON;
}
public String extractComponentInitMethodName(JMethod method) {
JClassType returnType = method.getReturnType().isClassOrInterface();
if (returnType == null) {
return null;
}
Component component = method.getAnnotation(Component.class);
String methodName = component.initMethodName();
if (StringUtils.hasLength(methodName)) {
return methodName;
}
JMethod initMethod = findAnnotatedMethod(returnType, Initializer.class);
if (initMethod == null) {
return null;
}
return initMethod.getName();
}
public String extractComponentDisposeMethodName(JMethod method) {
JClassType returnType = method.getReturnType().isClassOrInterface();
if (returnType == null) {
return null;
}
Component component = method.getAnnotation(Component.class);
String methodName = component.disposeMethodName();
if (StringUtils.hasLength(methodName)) {
return methodName;
}
JMethod disposeMethod = findAnnotatedMethod(returnType, Disposer.class);
if (disposeMethod == null) {
return null;
}
return disposeMethod.getName();
}
public Map<String, String> extractComponentMetaData(JMethod method) {
Map<String, String> metaData = new HashMap<String, String>();
if (!method.isAnnotationPresent(MetaData.class)) {
return metaData;
}
Property[] properties = method.getAnnotation(MetaData.class).value();
for (Property property : properties) {
metaData.put(property.name(), property.value());
}
return metaData;
}
public boolean isConfiguredProperty(JMethod method) {
return method.isAnnotationPresent(ConfiguredProperty.class);
}
public boolean isRuntimeProperty(JMethod method) {
return method.isAnnotationPresent(MetaProperty.class);
}
public boolean isConstant(JField field) {
return field.isAnnotationPresent(Constant.class);
}
//============================================== Helper Methods ====================================================
protected JMethod findAnnotatedMethod(JClassType type, final Class<? extends Annotation> annotationClass) {
return GeneratorUtils.findMethod(type, new Criteria<JMethod>() {
public boolean check(JMethod method) {
return method.isAnnotationPresent(annotationClass);
}
});
}
}