Search in sources :

Example 36 with TypeReference

use of org.eclipse.jdt.internal.compiler.ast.TypeReference in project lombok by rzwitserloot.

the class HandleLog method createTypeReference.

public static TypeReference createTypeReference(String typeName, Annotation source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;
    TypeReference typeReference;
    if (typeName.contains(".")) {
        char[][] typeNameTokens = fromQualifiedName(typeName);
        long[] pos = new long[typeNameTokens.length];
        Arrays.fill(pos, p);
        typeReference = new QualifiedTypeReference(typeNameTokens, pos);
    } else {
        typeReference = null;
    }
    setGeneratedBy(typeReference, source);
    return typeReference;
}
Also used : QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) SingleTypeReference(org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)

Example 37 with TypeReference

use of org.eclipse.jdt.internal.compiler.ast.TypeReference in project lombok by rzwitserloot.

the class HandleVal method visitLocal.

@Override
public void visitLocal(EclipseNode localNode, LocalDeclaration local) {
    TypeReference type = local.type;
    boolean isVal = typeMatches(val.class, localNode, type);
    boolean isVar = typeMatches(var.class, localNode, type);
    if (!(isVal || isVar))
        return;
    if (isVal)
        handleFlagUsage(localNode, ConfigurationKeys.VAL_FLAG_USAGE, "val");
    if (isVar)
        handleFlagUsage(localNode, ConfigurationKeys.VAR_FLAG_USAGE, "var");
    boolean variableOfForEach = false;
    if (localNode.directUp().get() instanceof ForeachStatement) {
        ForeachStatement fs = (ForeachStatement) localNode.directUp().get();
        variableOfForEach = fs.elementVariable == local;
    }
    String annotation = isVal ? "val" : "var";
    if (local.initialization == null && !variableOfForEach) {
        localNode.addError("'" + annotation + "' on a local variable requires an initializer expression");
        return;
    }
    if (local.initialization instanceof ArrayInitializer) {
        localNode.addError("'" + annotation + "' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })");
        return;
    }
    if (isVal && localNode.directUp().get() instanceof ForStatement) {
        localNode.addError("'val' is not allowed in old-style for loops");
        return;
    }
    if (local.initialization != null && local.initialization.getClass().getName().equals("org.eclipse.jdt.internal.compiler.ast.LambdaExpression")) {
        localNode.addError("'" + annotation + "' is not allowed with lambda expressions.");
        return;
    }
    if (isVar && local.initialization instanceof NullLiteral) {
        localNode.addError("variable initializer is 'null'");
        return;
    }
}
Also used : TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) ForStatement(org.eclipse.jdt.internal.compiler.ast.ForStatement) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) ForeachStatement(org.eclipse.jdt.internal.compiler.ast.ForeachStatement) ArrayInitializer(org.eclipse.jdt.internal.compiler.ast.ArrayInitializer)

Example 38 with TypeReference

use of org.eclipse.jdt.internal.compiler.ast.TypeReference in project lombok by rzwitserloot.

the class EclipseGuavaSingularizer method generateSingularMethod.

void generateSingularMethod(TypeReference returnType, Statement returnStatement, SingularData data, EclipseNode builderType, boolean fluent) {
    LombokImmutableList<String> suffixes = getArgumentSuffixes();
    char[][] names = new char[suffixes.size()][];
    for (int i = 0; i < suffixes.size(); i++) {
        String s = suffixes.get(i);
        char[] n = data.getSingularName();
        names[i] = s.isEmpty() ? n : s.toCharArray();
    }
    MethodDeclaration md = new MethodDeclaration(((CompilationUnitDeclaration) builderType.top().get()).compilationResult);
    md.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    md.modifiers = ClassFileConstants.AccPublic;
    List<Statement> statements = new ArrayList<Statement>();
    statements.add(createConstructBuilderVarIfNeeded(data, builderType));
    FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
    thisDotField.receiver = new ThisReference(0, 0);
    MessageSend thisDotFieldDotAdd = new MessageSend();
    thisDotFieldDotAdd.arguments = new Expression[suffixes.size()];
    for (int i = 0; i < suffixes.size(); i++) {
        thisDotFieldDotAdd.arguments[i] = new SingleNameReference(names[i], 0L);
    }
    thisDotFieldDotAdd.receiver = thisDotField;
    thisDotFieldDotAdd.selector = getAddMethodName().toCharArray();
    statements.add(thisDotFieldDotAdd);
    if (returnStatement != null)
        statements.add(returnStatement);
    md.statements = statements.toArray(new Statement[statements.size()]);
    md.arguments = new Argument[suffixes.size()];
    for (int i = 0; i < suffixes.size(); i++) {
        TypeReference tr = cloneParamType(i, data.getTypeArgs(), builderType);
        md.arguments[i] = new Argument(names[i], 0, tr, 0);
    }
    md.returnType = returnType;
    md.selector = fluent ? data.getSingularName() : HandlerUtil.buildAccessorName(getAddMethodName(), new String(data.getSingularName())).toCharArray();
    data.setGeneratedByRecursive(md);
    injectMethod(builderType, md);
}
Also used : FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) Argument(org.eclipse.jdt.internal.compiler.ast.Argument) MethodDeclaration(org.eclipse.jdt.internal.compiler.ast.MethodDeclaration) ReturnStatement(org.eclipse.jdt.internal.compiler.ast.ReturnStatement) Statement(org.eclipse.jdt.internal.compiler.ast.Statement) IfStatement(org.eclipse.jdt.internal.compiler.ast.IfStatement) ArrayList(java.util.ArrayList) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) SingleNameReference(org.eclipse.jdt.internal.compiler.ast.SingleNameReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)

Example 39 with TypeReference

use of org.eclipse.jdt.internal.compiler.ast.TypeReference in project lombok by rzwitserloot.

the class EclipseGuavaSingularizer method appendBuildCode.

@Override
public void appendBuildCode(SingularData data, EclipseNode builderType, List<Statement> statements, char[] targetVariableName) {
    TypeReference varType = new QualifiedTypeReference(fromQualifiedName(data.getTargetFqn()), NULL_POSS);
    String simpleTypeName = getSimpleTargetTypeName(data);
    int agrumentsCount = getTypeArgumentsCount();
    varType = addTypeArgs(agrumentsCount, false, builderType, varType, data.getTypeArgs());
    MessageSend emptyInvoke;
    {
        //ImmutableX.of()
        emptyInvoke = new MessageSend();
        emptyInvoke.selector = new char[] { 'o', 'f' };
        emptyInvoke.receiver = new QualifiedNameReference(makeGuavaTypeName(simpleTypeName, false), NULL_POSS, 0, 0);
        emptyInvoke.typeArguments = createTypeArgs(agrumentsCount, false, builderType, data.getTypeArgs());
    }
    MessageSend invokeBuild;
    {
        //this.pluralName.build();
        invokeBuild = new MessageSend();
        invokeBuild.selector = new char[] { 'b', 'u', 'i', 'l', 'd' };
        FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        invokeBuild.receiver = thisDotField;
    }
    Expression isNull;
    {
        //this.pluralName == null
        FieldReference thisDotField = new FieldReference(data.getPluralName(), 0L);
        thisDotField.receiver = new ThisReference(0, 0);
        isNull = new EqualExpression(thisDotField, new NullLiteral(0, 0), OperatorIds.EQUAL_EQUAL);
    }
    Expression init = new ConditionalExpression(isNull, emptyInvoke, invokeBuild);
    LocalDeclaration varDefStat = new LocalDeclaration(data.getPluralName(), 0, 0);
    varDefStat.type = varType;
    varDefStat.initialization = init;
    statements.add(varDefStat);
}
Also used : LocalDeclaration(org.eclipse.jdt.internal.compiler.ast.LocalDeclaration) FieldReference(org.eclipse.jdt.internal.compiler.ast.FieldReference) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) ThisReference(org.eclipse.jdt.internal.compiler.ast.ThisReference) MessageSend(org.eclipse.jdt.internal.compiler.ast.MessageSend) ConditionalExpression(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression) Expression(org.eclipse.jdt.internal.compiler.ast.Expression) EqualExpression(org.eclipse.jdt.internal.compiler.ast.EqualExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) NullLiteral(org.eclipse.jdt.internal.compiler.ast.NullLiteral) QualifiedNameReference(org.eclipse.jdt.internal.compiler.ast.QualifiedNameReference)

Example 40 with TypeReference

use of org.eclipse.jdt.internal.compiler.ast.TypeReference in project lombok by rzwitserloot.

the class HandlerLibrary method handleAnnotation.

/**
	 * Handles the provided annotation node by first finding a qualifying instance of
	 * {@link EclipseAnnotationHandler} and if one exists, calling it with a freshly cooked up
	 * instance of {@link AnnotationValues}.
	 * 
	 * Note that depending on the printASTOnly flag, the {@link lombok.core.PrintAST} annotation
	 * will either be silently skipped, or everything that isn't {@code PrintAST} will be skipped.
	 * 
	 * The HandlerLibrary will attempt to guess if the given annotation node represents a lombok annotation.
	 * For example, if {@code lombok.*} is in the import list, then this method will guess that
	 * {@code Getter} refers to {@code lombok.Getter}, presuming that {@link lombok.eclipse.handlers.HandleGetter}
	 * has been loaded.
	 * 
	 * @param ast The Compilation Unit that contains the Annotation AST Node.
	 * @param annotationNode The Lombok AST Node representing the Annotation AST Node.
	 * @param annotation 'node.get()' - convenience parameter.
	 */
public void handleAnnotation(CompilationUnitDeclaration ast, EclipseNode annotationNode, org.eclipse.jdt.internal.compiler.ast.Annotation annotation, long priority) {
    TypeResolver resolver = new TypeResolver(annotationNode.getImportList());
    TypeReference rawType = annotation.type;
    if (rawType == null)
        return;
    String fqn = resolver.typeRefToFullyQualifiedName(annotationNode, typeLibrary, toQualifiedName(annotation.type.getTypeName()));
    if (fqn == null)
        return;
    AnnotationHandlerContainer<?> container = annotationHandlers.get(fqn);
    if (container == null)
        return;
    if (priority != container.getPriority())
        return;
    if (!annotationNode.isCompleteParse() && container.deferUntilPostDiet()) {
        if (needsHandling(annotation))
            container.preHandle(annotation, annotationNode);
        return;
    }
    try {
        if (checkAndSetHandled(annotation))
            container.handle(annotation, annotationNode);
    } catch (AnnotationValueDecodeFail fail) {
        fail.owner.setError(fail.getMessage(), fail.idx);
    } catch (Throwable t) {
        error(ast, String.format("Lombok annotation handler %s failed", container.handler.getClass()), t);
    }
}
Also used : AnnotationValueDecodeFail(lombok.core.AnnotationValues.AnnotationValueDecodeFail) TypeResolver(lombok.core.TypeResolver) TypeReference(org.eclipse.jdt.internal.compiler.ast.TypeReference)

Aggregations

TypeReference (org.eclipse.jdt.internal.compiler.ast.TypeReference)52 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)44 SingleTypeReference (org.eclipse.jdt.internal.compiler.ast.SingleTypeReference)26 ParameterizedQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedQualifiedTypeReference)20 Statement (org.eclipse.jdt.internal.compiler.ast.Statement)20 MethodDeclaration (org.eclipse.jdt.internal.compiler.ast.MethodDeclaration)19 IfStatement (org.eclipse.jdt.internal.compiler.ast.IfStatement)17 ParameterizedSingleTypeReference (org.eclipse.jdt.internal.compiler.ast.ParameterizedSingleTypeReference)17 ArrayList (java.util.ArrayList)16 MessageSend (org.eclipse.jdt.internal.compiler.ast.MessageSend)16 ReturnStatement (org.eclipse.jdt.internal.compiler.ast.ReturnStatement)16 ThisReference (org.eclipse.jdt.internal.compiler.ast.ThisReference)16 SingleNameReference (org.eclipse.jdt.internal.compiler.ast.SingleNameReference)15 Expression (org.eclipse.jdt.internal.compiler.ast.Expression)13 Argument (org.eclipse.jdt.internal.compiler.ast.Argument)12 FieldReference (org.eclipse.jdt.internal.compiler.ast.FieldReference)12 EclipseNode (lombok.eclipse.EclipseNode)11 ArrayTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayTypeReference)11 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)11 ArrayQualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.ArrayQualifiedTypeReference)8