use of javax.lang.model.type.ExecutableType in project mapstruct by mapstruct.
the class MethodRetrievalProcessor method getReferencedMethod.
private SourceMethod getReferencedMethod(TypeElement usedMapper, ExecutableType methodType, ExecutableElement method, TypeElement mapperToImplement, List<Parameter> parameters) {
Type returnType = typeFactory.getReturnType(methodType);
List<Type> exceptionTypes = typeFactory.getThrownTypes(methodType);
Type usedMapperAsType = typeFactory.getType(usedMapper);
Type mapperToImplementAsType = typeFactory.getType(mapperToImplement);
if (!mapperToImplementAsType.canAccess(usedMapperAsType, method)) {
return null;
}
Type definingType = typeFactory.getType(method.getEnclosingElement().asType());
return new SourceMethod.Builder().setDeclaringMapper(usedMapper.equals(mapperToImplement) ? null : usedMapperAsType).setDefininingType(definingType).setExecutable(method).setParameters(parameters).setReturnType(returnType).setExceptionTypes(exceptionTypes).setTypeUtils(typeUtils).setTypeFactory(typeFactory).build();
}
use of javax.lang.model.type.ExecutableType in project j2objc by google.
the class MethodTranslator method visitSuperReferenceExpression.
@Override
public TreeNode visitSuperReferenceExpression(SuperReferenceExpression node, Void data) {
TypeMirror objType = typeUtil.getJavaObject().asType();
TypeMirror nodeType = typeDecl.getTypeElement().asType();
assert !parserEnv.typeUtilities().isSameType(objType, nodeType);
TypeElement superClass = TranslationUtil.getSuperType(typeDecl);
SuperConstructorInvocation superCall = null;
for (ExecutableElement exec : ElementUtil.getConstructors(superClass)) {
if (exec.getParameters().size() == 0) {
ExecutableType execType = typeUtil.asMemberOf((DeclaredType) superClass.asType(), exec);
superCall = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(exec, execType));
break;
}
}
return superCall;
}
use of javax.lang.model.type.ExecutableType in project j2objc by google.
the class TreeConverter method convertFunctionalExpression.
private TreeNode convertFunctionalExpression(JCFunctionalExpression node, TreePath parent, FunctionalExpression newNode) {
List<? extends TypeMirror> targets = getTargets(node, parent);
for (TypeMirror type : targets) {
newNode.addTargetType(type);
}
Types types = Types.instance(((com.sun.tools.javac.api.BasicJavacTask) env.task()).getContext());
return newNode.setTypeMirror(targets.iterator().next()).setDescriptor(new ExecutablePair((ExecutableElement) types.findDescriptorSymbol(((com.sun.tools.javac.code.Type) targets.get(0)).tsym), (ExecutableType) node.getDescriptorType(types)));
}
use of javax.lang.model.type.ExecutableType in project j2objc by google.
the class AbstractMethodRewriter method addReturnTypeNarrowingDeclarations.
// Adds declarations for any methods where the known return type is more
// specific than what is already declared in inherited types.
private void addReturnTypeNarrowingDeclarations(AbstractTypeDeclaration node) {
TypeElement type = node.getTypeElement();
// No need to run this if the entire class is dead.
if (deadCodeMap != null && deadCodeMap.containsClass(type, elementUtil)) {
return;
}
Map<String, ExecutablePair> newDeclarations = new HashMap<>();
Map<String, TypeMirror> resolvedReturnTypes = new HashMap<>();
for (DeclaredType inheritedType : typeUtil.getObjcOrderedInheritedTypes(type.asType())) {
TypeElement inheritedElem = (TypeElement) inheritedType.asElement();
for (ExecutableElement methodElem : ElementUtil.getMethods(inheritedElem)) {
if (ElementUtil.isPrivate(methodElem)) {
continue;
}
TypeMirror declaredReturnType = typeUtil.erasure(methodElem.getReturnType());
if (!TypeUtil.isReferenceType(declaredReturnType)) {
// Short circuit
continue;
}
String selector = nameTable.getMethodSelector(methodElem);
ExecutableType methodType = typeUtil.asMemberOf(inheritedType, methodElem);
TypeMirror returnType = typeUtil.erasure(methodType.getReturnType());
TypeMirror resolvedReturnType = resolvedReturnTypes.get(selector);
if (resolvedReturnType == null) {
resolvedReturnType = declaredReturnType;
resolvedReturnTypes.put(selector, resolvedReturnType);
} else if (!typeUtil.isSubtype(returnType, resolvedReturnType)) {
continue;
}
if (resolvedReturnType != returnType && !nameTable.getObjCType(resolvedReturnType).equals(nameTable.getObjCType(returnType))) {
newDeclarations.put(selector, new ExecutablePair(methodElem, methodType));
resolvedReturnTypes.put(selector, returnType);
}
}
}
for (Map.Entry<String, ExecutablePair> newDecl : newDeclarations.entrySet()) {
if (deadCodeMap != null && deadCodeMap.containsMethod(newDecl.getValue().element(), typeUtil)) {
continue;
}
node.addBodyDeclaration(newReturnTypeNarrowingDeclaration(newDecl.getKey(), newDecl.getValue(), type));
}
}
use of javax.lang.model.type.ExecutableType in project tiger by google.
the class HubInjectorGenerator method generateProvisionAndInjectionMethods.
private void generateProvisionAndInjectionMethods() {
// Injection methods and non-injection methods.
Set<String> miscMethodNames = new HashSet<>();
DeclaredType eitherComponentType = (DeclaredType) eitherComponent.asType();
final int[] count = { 0 };
utils.generateDebugInfoMethod(injectorBuilder, "generateProvisionAndInjectionMethods");
utils.traverseAndDo(types, eitherComponentType, eitherComponent, p -> {
Element element = p.second;
// logger.n("" + element);
if (!element.getKind().equals(ElementKind.METHOD)) {
return null;
}
ExecutableElement method = (ExecutableElement) element;
ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().asMemberOf(eitherComponentType, method);
// Injection methods.
if (utils.isInjectionMethod(element)) {
// TODO: add duplicate check for provision method also.
if (injectionMethodsDone.add(Pair.of(method.getSimpleName().toString(), TypeName.get(Iterables.getOnlyElement(methodType.getParameterTypes())))) == false) {
// logger.w("injection method: " + method);
return null;
}
TypeMirror typeMirror = Iterables.getOnlyElement(methodType.getParameterTypes());
TypeElement cls = (TypeElement) ((DeclaredType) typeMirror).asElement();
// logger.n(methodType.toString());
injectorBuilder.addMethod(MethodSpec.methodBuilder(method.getSimpleName().toString()).addModifiers(Modifier.PUBLIC).addParameter(ClassName.get(cls), "arg").addStatement("inject(arg)").build());
count[0]++;
} else if (utils.isComponentProvisionMethod(element)) {
logger.l(Kind.ERROR, "Injecting components is not supported: " + element);
} else if (utils.isSubcomponentProvisionMethod(element)) {
/**
* TODO: handle this in {@link #generateProvisionMethodIfNeeded(BindingKey)}
*/
generateGetSubcomponentMethod((ExecutableElement) element, injectorBuilder);
count[0]++;
} else if (utils.isProvisionMethodInInjector(element)) {
if (!miscMethodNames.add(method.getSimpleName().toString())) {
return null;
}
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(method.getSimpleName().toString()).addModifiers(Modifier.PUBLIC).returns(TypeName.get(method.getReturnType()));
BindingKey providedKey = utils.getKeyProvidedByMethod(method);
// ClassName packagedInjectorClassName = null;
// for (ClassName className : keyToPackagedInjectorMap.get(providedKey)) {
// if (isInjectorOfScope(className, coreInjectorInfo.getScope())) {
// packagedInjectorClassName = className;
// break;
// }
// }
// if (packagedInjectorClassName == null) {
// logger.l(Kind.WARNING,
// String.format(
// "PackagedInjector or multiBindingInjector not found for key: %s "
// + "from provisionMethod: %s. Probably it is not used.",
// providedKey, method));
// // Create a dumb method
// String statement = "return ";
// TypeKind typeKind = method.getReturnType().getKind();
// if (typeKind.equals(TypeKind.BOOLEAN)) {
// statement += "false";
// } else if (typeKind.equals(TypeKind.CHAR)) {
// statement += "\'0\'";
// } else if (typeKind.isPrimitive()) {
// statement += "0";
// } else {
// statement += "null";
// }
// methodBuilder.addStatement(statement);
// } else {
String statement = "return $L()";
methodBuilder.addStatement(statement, Utils.getProvisionMethodName(dependencies, providedKey));
// }
// logger.n("method added: " + methodBuilder.build());
injectorBuilder.addMethod(methodBuilder.build());
count[0]++;
// } else if (utils.isEitherComponentProvisionMethod(element)) {
// // TODO: support get component method.
// if(utils.isComponentProvisionMethod(element)) {
// throw new RuntimeException("component provision method is not suported yet.");
// }
// generateGetSubcomponentMethod((ExecutableElement) element, injectorBuilder);
// } else if (utils.isEitherComponentBuilderProvisionMethod(element)) {
// /**
// * TODO: handle it in the way consistent with other {@link DependencySourceType} in
// * {@link #generateProvisionMethodIfNeeded(BindingKey, TypeElement)}
// */
// generateExplicitProvisionMethodForEitherComponentBuilder(
// (ExecutableElement) element, injectorBuilder);
} else if (utils.isIrrelevantMethodInInjector(element)) {
// do nothing
} else {
logger.l(Kind.WARNING, "Element %s ignored from injector %s.", element, eitherComponentType);
}
return null;
});
logger.w("count: %d", count[0]);
}
Aggregations