use of javax.lang.model.type.ExecutableType in project tiger by google.
the class ProxyGenerator method addMethodCallingMethodOrCtor.
/**
* Handles ctor, class methods including module methods.
*/
private void addMethodCallingMethodOrCtor(Builder typeBuilder, TypeMirror returnType, TypeElement container, ExecutableElement executableElement, String generatedMethodName) {
boolean isCtor = executableElement.getKind().equals(ElementKind.CONSTRUCTOR);
boolean isStatic = executableElement.getModifiers().contains(Modifier.STATIC);
MethodSpec.Builder methodSpecBuilder = MethodSpec.methodBuilder(generatedMethodName).addModifiers(Modifier.PUBLIC);
boolean hasReturnValue = !returnType.getKind().equals(TypeKind.VOID);
if (hasReturnValue) {
methodSpecBuilder.returns(ClassName.get(returnType));
}
if (isCtor) {
addTypeVariablesToMethod(container, methodSpecBuilder);
}
TypeName moduleTypeName = isCtor ? null : ClassName.get(container.asType());
List<TypeName> types = new ArrayList<>();
TypeName returnTypeName = ClassName.get(returnType);
types.add(returnTypeName);
if (isCtor) {
types.add(returnTypeName);
} else if (isStatic) {
types.add(moduleTypeName);
}
StringBuilder builder = new StringBuilder();
if (hasReturnValue) {
builder.append("$T result = ");
}
if (isCtor) {
builder.append("new $T(");
} else if (isStatic) {
builder.append("$T.").append(executableElement.getSimpleName()).append("(");
} else {
methodSpecBuilder.addParameter(moduleTypeName, "module");
builder.append("module.").append(executableElement.getSimpleName()).append("(");
}
String varNameBase = "var";
int varOrdinal = 1;
List<? extends TypeMirror> arguments = ((ExecutableType) executableElement.asType()).getParameterTypes();
for (TypeMirror parameter : arguments) {
String varName = varNameBase + varOrdinal;
TypeName typeName = TypeName.get(parameter);
methodSpecBuilder.addParameter(toAccessibleType(typeName), varName);
if (!utils.isPublicallyAccessible(typeName)) {
types.add(typeName);
builder.append("($T) ");
}
builder.append(varName).append(", ");
varOrdinal++;
}
if (arguments.size() > 0) {
builder.delete(builder.length() - 2, builder.length());
}
builder.append(")");
methodSpecBuilder.addStatement(builder.toString(), types.toArray());
if (hasReturnValue) {
methodSpecBuilder.addStatement("return result");
}
typeBuilder.addMethod(methodSpecBuilder.build());
}
use of javax.lang.model.type.ExecutableType in project ceylon-compiler by ceylon.
the class P method process.
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
if (!ran) {
ran = true;
ExecutableElement m = getFirstMethodIn("C");
System.err.println("method: " + m);
TypeMirror type = (DeclaredType) m.getParameters().get(0).asType();
System.err.println("parameters[0]: " + type);
if (!isParameterized(type))
throw new AssertionError(type);
type = ((ExecutableType) m.asType()).getParameterTypes().get(0);
System.err.println("parameterTypes[0]: " + type);
if (!isParameterized(type))
throw new AssertionError(type);
System.err.println();
}
return true;
}
use of javax.lang.model.type.ExecutableType in project vertx-docgen by vert-x3.
the class JavaDocGenerator method toExecutableLink.
private String toExecutableLink(ExecutableElement elt, String name) {
TypeElement typeElt = (TypeElement) elt.getEnclosingElement();
String link = resolveTypeLink(typeElt);
StringBuilder anchor = new StringBuilder("#");
anchor.append(name).append('-');
TypeMirror type = elt.asType();
ExecutableType methodType = (ExecutableType) processingEnv.getTypeUtils().erasure(type);
List<? extends TypeMirror> parameterTypes = methodType.getParameterTypes();
for (int i = 0; i < parameterTypes.size(); i++) {
if (i > 0) {
anchor.append('-');
}
// We need to check whether or not the parameter is annotated. In this case, we must use the unannotated type.
TypeMirror typeOfParameter = parameterTypes.get(i);
String s = typeOfParameter.toString();
Matcher matcher = A.matcher(s);
if (matcher.matches()) {
String t = matcher.group(1);
anchor.append(t);
} else {
anchor.append(s);
}
}
anchor.append('-');
return link + anchor;
}
use of javax.lang.model.type.ExecutableType in project j2objc by google.
the class TreeConverter method convertMethodInvocation.
private TreeNode convertMethodInvocation(JCTree.JCMethodInvocation node) {
JCTree.JCExpression method = node.getMethodSelect();
String methodName = getMemberName(method);
ExecutableType type = (ExecutableType) method.type;
Symbol.MethodSymbol sym = (Symbol.MethodSymbol) getMemberSymbol(method);
JCTree.JCExpression target = method.getKind() == Kind.MEMBER_SELECT ? ((JCTree.JCFieldAccess) method).selected : null;
if ("this".equals(methodName)) {
ConstructorInvocation newNode = new ConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
if ("super".equals(methodName)) {
SuperConstructorInvocation newNode = new SuperConstructorInvocation().setExecutablePair(new ExecutablePair(sym)).setVarargsType(node.varargsElement);
if (target != null) {
newNode.setExpression((Expression) convert(target));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
if (target != null && "super".equals(getMemberName(target))) {
SuperMethodInvocation newNode = new SuperMethodInvocation().setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement).setName(convertSimpleName(sym, type, getPosition(node)));
if (target.getKind() == Kind.MEMBER_SELECT) {
// foo.bar.MyClass.super.print(...):
// target: foo.bar.MyClass.super
// target.selected: foo.bar.MyClass
newNode.setQualifier((Name) convert(((JCTree.JCFieldAccess) target).selected));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode;
}
MethodInvocation newNode = new MethodInvocation();
newNode.setName(convertSimpleName(sym, type, getPosition(method)));
if (target != null) {
newNode.setExpression((Expression) convert(target));
}
for (JCTree.JCExpression arg : node.getArguments()) {
newNode.addArgument((Expression) convert(arg));
}
return newNode.setTypeMirror(node.type).setExecutablePair(new ExecutablePair(sym, type)).setVarargsType(node.varargsElement);
}
use of javax.lang.model.type.ExecutableType in project epoxy by airbnb.
the class ClassToGenerateInfo method collectMethodsReturningClassType.
/**
* Get information about methods returning class type of the original class so we can duplicate
* them in the generated class for chaining purposes
*/
private void collectMethodsReturningClassType(TypeElement originalClass) {
TypeElement clazz = originalClass;
while (clazz.getSuperclass().getKind() != TypeKind.NONE) {
for (Element subElement : clazz.getEnclosedElements()) {
Set<Modifier> modifiers = subElement.getModifiers();
if (subElement.getKind() == ElementKind.METHOD && !modifiers.contains(Modifier.PRIVATE) && !modifiers.contains(Modifier.FINAL) && !modifiers.contains(Modifier.STATIC)) {
TypeMirror methodReturnType = ((ExecutableType) subElement.asType()).getReturnType();
if (methodReturnType.equals(clazz.asType()) || typeUtils.isSubtype(clazz.asType(), methodReturnType)) {
ExecutableElement castedSubElement = ((ExecutableElement) subElement);
List<? extends VariableElement> params = castedSubElement.getParameters();
String methodName = subElement.getSimpleName().toString();
if (methodName.equals(RESET_METHOD) && params.isEmpty()) {
continue;
}
methodsReturningClassType.add(new MethodInfo(methodName, modifiers, buildParamList(params), castedSubElement.isVarArgs()));
}
}
}
clazz = (TypeElement) typeUtils.asElement(clazz.getSuperclass());
}
}
Aggregations