use of javax.lang.model.type.ExecutableType in project react4j by react4j.
the class ReactProcessor method determineRenderMethod.
private void determineRenderMethod(@Nonnull final TypeElement typeElement, @Nonnull final ComponentDescriptor descriptor) {
/*
* Get the render method that has been overridden by the typeElement, a parent class, or by a default
* method method implemented by the typeElement or a parent class.
*/
final ExecutableElement renderMethod = getComponentRenderMethod();
final Elements elementUtils = processingEnv.getElementUtils();
final Types typeUtils = processingEnv.getTypeUtils();
final TypeElement componentType = elementUtils.getTypeElement(Constants.COMPONENT_CLASSNAME);
final MethodDescriptor overriddenRenderMethod = // Get all methods on type parent classes, and default methods from interfaces
ProcessorUtil.getMethods(typeElement, processingEnv.getTypeUtils()).stream().filter(m -> elementUtils.overrides(m, renderMethod, typeElement)).filter(m -> m.getEnclosingElement() != componentType).map(m -> new MethodDescriptor(m, (ExecutableType) typeUtils.asMemberOf(descriptor.getDeclaredType(), m))).findAny().orElse(null);
if (null == overriddenRenderMethod) {
throw new ReactProcessorException("The react component does not override any render methods.", typeElement);
}
}
use of javax.lang.model.type.ExecutableType in project react4j by react4j.
the class Generator method buildBuilderStepImpl.
@Nonnull
private static MethodSpec buildBuilderStepImpl(@Nonnull final Step step, @Nonnull final StepMethod stepMethod) {
final MethodSpec.Builder method = MethodSpec.methodBuilder(stepMethod.getName());
method.addModifiers(Modifier.PUBLIC, Modifier.FINAL);
method.addAnnotation(Override.class);
method.addAnnotation(NONNULL_CLASSNAME);
final ExecutableType propMethodType = stepMethod.getPropMethodType();
if (null != propMethodType) {
ProcessorUtil.copyTypeParameters(propMethodType, method);
}
final ParameterSpec.Builder parameter = ParameterSpec.builder(stepMethod.getType(), stepMethod.getName(), Modifier.FINAL);
final ExecutableElement propMethod = stepMethod.getPropMethod();
if (null != propMethod) {
ProcessorUtil.copyDocumentedAnnotations(propMethod, parameter);
} else if (stepMethod.isKeyIntrinsic() || stepMethod.isChildrenStreamIntrinsic()) {
parameter.addAnnotation(NONNULL_CLASSNAME);
} else if (stepMethod.isChildOfChildrenIntrinsic()) {
parameter.addAnnotation(NULLABLE_CLASSNAME);
}
method.addParameter(parameter.build());
if (stepMethod.isChildrenIntrinsic()) {
method.varargs();
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("for ( final $T child : $N )", REACT_NODE_CLASSNAME, stepMethod.getName());
block.addStatement("child( child )");
block.endControlFlow();
method.addCode(block.build());
} else if (stepMethod.isChildOfChildrenIntrinsic()) {
final CodeBlock.Builder block = CodeBlock.builder();
block.beginControlFlow("if ( null != $N )", stepMethod.getName());
block.addStatement("_children.push( $N )", stepMethod.getName());
block.endControlFlow();
method.addCode(block.build());
} else if (stepMethod.isChildrenStreamIntrinsic()) {
method.addStatement("children( $N.toArray( $T[]::new ) )", stepMethod.getName(), REACT_NODE_CLASSNAME);
} else if (stepMethod.isChildIntrinsic()) {
assert null != propMethod;
if (null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME)) {
method.addStatement("_child = $T.requireNonNull( $N )", Objects.class, stepMethod.getName());
} else {
method.addStatement("_child = $N", stepMethod.getName());
}
} else if (stepMethod.isKeyIntrinsic() || (null != propMethod && null != ProcessorUtil.findAnnotationByType(propMethod, Constants.NONNULL_ANNOTATION_CLASSNAME))) {
method.addStatement("_props.set( $S, $T.requireNonNull( $N ) )", stepMethod.getName(), Objects.class, stepMethod.getName());
} else {
method.addStatement("_props.set( $S, $N )", stepMethod.getName(), stepMethod.getName());
}
if (StepMethodType.TERMINATE == stepMethod.getStepMethodType()) {
method.addStatement("return build()");
} else {
method.addStatement("return this");
}
configureStepMethodReturns(method, step, stepMethod.getStepMethodType());
return method.build();
}
use of javax.lang.model.type.ExecutableType in project hibernate-orm by hibernate.
the class AnnotationMetaEntity method isGetterOrSetter.
/**
* Check if method respects Java Bean conventions for getter and setters.
*
* @param methodOfClass method element
*
* @return whether method respects Java Bean conventions.
*/
private boolean isGetterOrSetter(Element methodOfClass) {
ExecutableType methodType = (ExecutableType) methodOfClass.asType();
String methodSimpleName = methodOfClass.getSimpleName().toString();
List<? extends TypeMirror> methodParameterTypes = methodType.getParameterTypes();
TypeMirror returnType = methodType.getReturnType();
if (methodSimpleName.startsWith("set") && methodParameterTypes.size() == 1 && "void".equalsIgnoreCase(returnType.toString())) {
return true;
} else if ((methodSimpleName.startsWith("get") || methodSimpleName.startsWith("is")) && methodParameterTypes.isEmpty() && !"void".equalsIgnoreCase(returnType.toString())) {
return true;
} else {
return false;
}
}
use of javax.lang.model.type.ExecutableType in project tutorials by eugenp.
the class BuilderProcessor method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement annotation : annotations) {
Set<? extends Element> annotatedElements = roundEnv.getElementsAnnotatedWith(annotation);
Map<Boolean, List<Element>> annotatedMethods = annotatedElements.stream().collect(Collectors.partitioningBy(element -> ((ExecutableType) element.asType()).getParameterTypes().size() == 1 && element.getSimpleName().toString().startsWith("set")));
List<Element> setters = annotatedMethods.get(true);
List<Element> otherMethods = annotatedMethods.get(false);
otherMethods.forEach(element -> processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@BuilderProperty must be applied to a setXxx method with a single argument", element));
if (setters.isEmpty()) {
continue;
}
String className = ((TypeElement) setters.get(0).getEnclosingElement()).getQualifiedName().toString();
Map<String, String> setterMap = setters.stream().collect(Collectors.toMap(setter -> setter.getSimpleName().toString(), setter -> ((ExecutableType) setter.asType()).getParameterTypes().get(0).toString()));
try {
writeBuilderFile(className, setterMap);
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
use of javax.lang.model.type.ExecutableType in project arez by arez.
the class ComponentDescriptor method analyzeCandidateMethods.
void analyzeCandidateMethods(@Nonnull final List<ExecutableElement> methods, @Nonnull final Types typeUtils) throws ArezProcessorException {
final Map<String, CandidateMethod> getters = new HashMap<>();
final Map<String, CandidateMethod> setters = new HashMap<>();
final Map<String, CandidateMethod> trackeds = new HashMap<>();
final Map<String, CandidateMethod> onDepsChangeds = new HashMap<>();
for (final ExecutableElement method : methods) {
final ExecutableType methodType = (ExecutableType) typeUtils.asMemberOf((DeclaredType) _element.asType(), method);
if (!analyzeMethod(method, methodType)) {
/*
* If we get here the method was not annotated so we can try to detect if it is a
* candidate arez method in case some arez annotations are implied via naming conventions.
*/
if (method.getModifiers().contains(Modifier.STATIC)) {
continue;
}
final CandidateMethod candidateMethod = new CandidateMethod(method, methodType);
final boolean voidReturn = method.getReturnType().getKind() == TypeKind.VOID;
final int parameterCount = method.getParameters().size();
String name;
if (!method.getModifiers().contains(Modifier.FINAL)) {
name = ProcessorUtil.deriveName(method, SETTER_PATTERN, ProcessorUtil.SENTINEL_NAME);
if (voidReturn && 1 == parameterCount && null != name) {
setters.put(name, candidateMethod);
continue;
}
name = ProcessorUtil.deriveName(method, ISSER_PATTERN, ProcessorUtil.SENTINEL_NAME);
if (!voidReturn && 0 == parameterCount && null != name) {
getters.put(name, candidateMethod);
continue;
}
name = ProcessorUtil.deriveName(method, GETTER_PATTERN, ProcessorUtil.SENTINEL_NAME);
if (!voidReturn && 0 == parameterCount && null != name) {
getters.put(name, candidateMethod);
continue;
}
}
name = ProcessorUtil.deriveName(method, TrackedDescriptor.ON_DEPS_CHANGED_PATTERN, ProcessorUtil.SENTINEL_NAME);
if (voidReturn && 0 == parameterCount && null != name) {
onDepsChangeds.put(name, candidateMethod);
continue;
}
final String methodName = method.getSimpleName().toString();
if (!OBJECT_METHODS.contains(methodName)) {
trackeds.put(methodName, candidateMethod);
}
}
}
linkUnAnnotatedObservables(getters, setters);
linkUnAnnotatedTracked(trackeds, onDepsChangeds);
linkObserverRefs();
linkDependencies(getters.values());
/*
* ALl of the maps will have called remove() for all matching candidates.
* Thus any left are the non-arez methods.
*/
ensureNoAbstractMethods(getters.values());
ensureNoAbstractMethods(setters.values());
ensureNoAbstractMethods(trackeds.values());
ensureNoAbstractMethods(onDepsChangeds.values());
}
Aggregations