Example usage for org.eclipse.jdt.internal.compiler.ast QualifiedNameReference QualifiedNameReference

List of usage examples for org.eclipse.jdt.internal.compiler.ast QualifiedNameReference QualifiedNameReference

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast QualifiedNameReference QualifiedNameReference.

Prototype

public QualifiedNameReference(char[][] tokens, long[] positions, int sourceStart, int sourceEnd) 

Source Link

Usage

From source file:lombok.eclipse.agent.PatchExtensionMethod.java

License:Open Source License

private static NameReference createNameRef(TypeBinding typeBinding, ASTNode source) {
    long p = ((long) source.sourceStart << 32) | source.sourceEnd;
    char[] pkg = typeBinding.qualifiedPackageName();
    char[] basename = typeBinding.qualifiedSourceName();

    StringBuilder sb = new StringBuilder();
    if (pkg != null)
        sb.append(pkg);//from  ww  w .  ja v a2s .  c  o m
    if (sb.length() > 0)
        sb.append(".");
    sb.append(basename);

    String tName = sb.toString();

    if (tName.indexOf('.') == -1) {
        return new SingleNameReference(basename, p);
    } else {
        char[][] sources;
        String[] in = tName.split("\\.");
        sources = new char[in.length][];
        for (int i = 0; i < in.length; i++)
            sources[i] = in[i].toCharArray();
        long[] poss = new long[in.length];
        Arrays.fill(poss, p);
        return new QualifiedNameReference(sources, poss, source.sourceStart, source.sourceEnd);
    }
}

From source file:lombok.eclipse.handlers.ast.EclipseASTMaker.java

License:Open Source License

@Override
public ASTNode visitNameRef(final lombok.ast.NameRef node, final Void p) {
    final NameReference nameReference;
    if (node.getName().contains(".")) {
        char[][] nameTokens = fromQualifiedName(node.getName());
        nameReference = new QualifiedNameReference(nameTokens, poss(posHintOf(node), nameTokens.length), 0, 0);
    } else {//from   w  w  w. j  a v a  2  s  .  c  o m
        nameReference = new SingleNameReference(node.getName().toCharArray(), 0);
    }
    setGeneratedByAndCopyPos(nameReference, source, posHintOf(node));
    return nameReference;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

static Expression createFieldAccessor(EclipseNode field, FieldAccess fieldAccess, ASTNode source,
        char[] receiver) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    boolean lookForGetter = lookForGetter(field, fieldAccess);

    GetterMethod getter = lookForGetter ? findGetter(field) : null;

    if (getter == null) {
        NameReference ref;//from w w w .j a  v a2  s  .  co m

        char[][] tokens = new char[2][];
        tokens[0] = receiver;
        tokens[1] = field.getName().toCharArray();
        long[] poss = { p, p };

        ref = new QualifiedNameReference(tokens, poss, pS, pE);
        setGeneratedBy(ref, source);
        return ref;
    }

    MessageSend call = new MessageSend();
    setGeneratedBy(call, source);
    call.sourceStart = pS;
    call.statementEnd = call.sourceEnd = pE;
    call.receiver = new SingleNameReference(receiver, p);
    setGeneratedBy(call.receiver, source);
    call.selector = getter.name;
    return call;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

/**
 * In eclipse 3.7+, the CastExpression constructor was changed from a really weird version to
 * a less weird one. Unfortunately that means we need to use reflection as we want to be compatible
 * with eclipse versions before 3.7 and 3.7+.
 * /*from  w  w w .jav a  2s . c om*/
 * @param ref The {@code foo} in {@code (String)foo}.
 * @param castTo The {@code String} in {@code (String)foo}.
 */
public static CastExpression makeCastExpression(Expression ref, TypeReference castTo, ASTNode source) {
    CastExpression result;
    try {
        if (castExpressionConstructorIsTypeRefBased) {
            result = castExpressionConstructor.newInstance(ref, castTo);
        } else {
            Expression castToConverted = castTo;

            if (castTo.getClass() == SingleTypeReference.class && !isPrimitive(castTo)) {
                SingleTypeReference str = (SingleTypeReference) castTo;
                //Why a SingleNameReference instead of a SingleTypeReference you ask? I don't know. It seems dumb. Ask the ecj guys.
                castToConverted = new SingleNameReference(str.token, 0);
                castToConverted.bits = (castToConverted.bits & ~Binding.VARIABLE) | Binding.TYPE;
                castToConverted.sourceStart = str.sourceStart;
                castToConverted.sourceEnd = str.sourceEnd;
                setGeneratedBy(castToConverted, source);
            } else if (castTo.getClass() == QualifiedTypeReference.class) {
                QualifiedTypeReference qtr = (QualifiedTypeReference) castTo;
                //Same here, but for the more complex types, they stay types.
                castToConverted = new QualifiedNameReference(qtr.tokens, copy(qtr.sourcePositions),
                        qtr.sourceStart, qtr.sourceEnd);
                castToConverted.bits = (castToConverted.bits & ~Binding.VARIABLE) | Binding.TYPE;
                setGeneratedBy(castToConverted, source);
            }

            result = castExpressionConstructor.newInstance(ref, castToConverted);
        }
    } catch (InvocationTargetException e) {
        throw Lombok.sneakyThrow(e.getCause());
    } catch (IllegalAccessException e) {
        throw Lombok.sneakyThrow(e);
    } catch (InstantiationException e) {
        throw Lombok.sneakyThrow(e);
    }

    result.sourceStart = source.sourceStart;
    result.sourceEnd = source.sourceEnd;
    result.statementEnd = source.sourceEnd;

    setGeneratedBy(result, source);
    return result;
}

From source file:lombok.eclipse.handlers.EclipseHandlerUtil.java

License:Open Source License

public static NameReference createNameReference(String name, Annotation source) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    char[][] nameTokens = fromQualifiedName(name);
    long[] pos = new long[nameTokens.length];
    Arrays.fill(pos, p);/*  ww w.  j av a2s .co  m*/

    QualifiedNameReference nameReference = new QualifiedNameReference(nameTokens, pos, pS, pE);
    nameReference.statementEnd = pE;

    setGeneratedBy(nameReference, source);
    return nameReference;
}

From source file:lombok.eclipse.handlers.HandleEqualsAndHashCode.java

License:Open Source License

public NameReference generateQualifiedNameRef(ASTNode source, char[]... varNames) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    NameReference ref;//from   w w  w .jav  a2 s. c  o m

    if (varNames.length > 1)
        ref = new QualifiedNameReference(varNames, new long[varNames.length], pS, pE);
    else
        ref = new SingleNameReference(varNames[0], p);
    setGeneratedBy(ref, source);
    return ref;
}

From source file:lombok.eclipse.handlers.HandleSetterVersionable.java

License:Open Source License

public static NameReference generateQualifiedNameRef(ASTNode source, char[]... varNames) {
    int pS = source.sourceStart, pE = source.sourceEnd;
    long p = (long) pS << 32 | pE;

    NameReference ref;/*from w ww .j a  v  a2s  . c  o  m*/

    if (varNames.length > 1)
        ref = new QualifiedNameReference(varNames, new long[varNames.length], pS, pE);
    else
        ref = new SingleNameReference(varNames[0], p);
    setGeneratedBy(ref, source);
    return ref;
}

From source file:lombok.eclipse.handlers.HandleSneakyThrows.java

License:Open Source License

public Statement buildTryCatchBlock(Statement[] contents, DeclaredException exception, ASTNode source,
        AbstractMethodDeclaration method) {
    int methodStart = method.bodyStart;
    int methodEnd = method.bodyEnd;
    long methodPosEnd = ((long) methodEnd) << 32 | (methodEnd & 0xFFFFFFFFL);

    TryStatement tryStatement = new TryStatement();
    setGeneratedBy(tryStatement, source);
    tryStatement.tryBlock = new Block(0);

    // Positions for in-method generated nodes are special
    tryStatement.tryBlock.sourceStart = methodStart;
    tryStatement.tryBlock.sourceEnd = methodEnd;

    setGeneratedBy(tryStatement.tryBlock, source);
    tryStatement.tryBlock.statements = contents;
    TypeReference typeReference;// w w w  . ja v  a  2 s. c  o  m
    if (exception.exceptionName.indexOf('.') == -1) {
        typeReference = new SingleTypeReference(exception.exceptionName.toCharArray(), methodPosEnd);
        typeReference.statementEnd = methodEnd;
    } else {
        String[] x = exception.exceptionName.split("\\.");
        char[][] elems = new char[x.length][];
        long[] poss = new long[x.length];
        Arrays.fill(poss, methodPosEnd);
        for (int i = 0; i < x.length; i++) {
            elems[i] = x[i].trim().toCharArray();
        }
        typeReference = new QualifiedTypeReference(elems, poss);
    }
    setGeneratedBy(typeReference, source);

    Argument catchArg = new Argument("$ex".toCharArray(), methodPosEnd, typeReference, Modifier.FINAL);
    setGeneratedBy(catchArg, source);
    catchArg.declarationSourceEnd = catchArg.declarationEnd = catchArg.sourceEnd = methodEnd;
    catchArg.declarationSourceStart = catchArg.modifiersSourceStart = catchArg.sourceStart = methodEnd;

    tryStatement.catchArguments = new Argument[] { catchArg };

    MessageSend sneakyThrowStatement = new MessageSend();
    setGeneratedBy(sneakyThrowStatement, source);
    sneakyThrowStatement.receiver = new QualifiedNameReference(
            new char[][] { "lombok".toCharArray(), "Lombok".toCharArray() }, new long[2], methodEnd, methodEnd);
    setGeneratedBy(sneakyThrowStatement.receiver, source);
    sneakyThrowStatement.receiver.statementEnd = methodEnd;
    sneakyThrowStatement.selector = "sneakyThrow".toCharArray();
    SingleNameReference exRef = new SingleNameReference("$ex".toCharArray(), methodPosEnd);
    setGeneratedBy(exRef, source);
    exRef.statementEnd = methodEnd;
    sneakyThrowStatement.arguments = new Expression[] { exRef };

    // This is the magic fix for rendering issues
    // In org.eclipse.jdt.core.dom.ASTConverter#convert(org.eclipse.jdt.internal.compiler.ast.MessageSend)
    // a new SimpleName is created and the setSourceRange should receive -1, 0. That's why we provide -2L :-)
    sneakyThrowStatement.nameSourcePosition = -2L;

    sneakyThrowStatement.sourceStart = methodEnd;
    sneakyThrowStatement.sourceEnd = sneakyThrowStatement.statementEnd = methodEnd;

    Statement rethrowStatement = new ThrowStatement(sneakyThrowStatement, methodEnd, methodEnd);
    setGeneratedBy(rethrowStatement, source);

    Block block = new Block(0);
    block.sourceStart = methodEnd;
    block.sourceEnd = methodEnd;
    setGeneratedBy(block, source);
    block.statements = new Statement[] { rethrowStatement };

    tryStatement.catchBlocks = new Block[] { block };

    // Positions for in-method generated nodes are special
    tryStatement.sourceStart = method.bodyStart;
    tryStatement.sourceEnd = method.bodyEnd;

    return tryStatement;
}

From source file:lombok.eclipse.handlers.HandleSuperBuilder.java

License:Open Source License

/**
 * Generates a constructor that has a builder as the only parameter.
 * The values from the builder are used to initialize the fields of new instances.
 *
 * @param typeNode//from www .  j  ava2  s  . c  om
 *            the type (with the {@code @Builder} annotation) for which a
 *            constructor should be generated.
 * @param typeParams
 * @param builderFields a list of fields in the builder which should be assigned to new instances.
 * @param source the annotation (used for setting source code locations for the generated code).
 * @param callBuilderBasedSuperConstructor
 *            If {@code true}, the constructor will explicitly call a super
 *            constructor with the builder as argument. Requires
 *            {@code builderClassAsParameter != null}.
 */
private void generateBuilderBasedConstructor(EclipseNode typeNode, TypeParameter[] typeParams,
        List<BuilderFieldData> builderFields, EclipseNode sourceNode, String builderClassName,
        boolean callBuilderBasedSuperConstructor) {

    ASTNode source = sourceNode.get();

    TypeDeclaration typeDeclaration = ((TypeDeclaration) typeNode.get());
    long p = (long) source.sourceStart << 32 | source.sourceEnd;

    ConstructorDeclaration constructor = new ConstructorDeclaration(
            ((CompilationUnitDeclaration) typeNode.top().get()).compilationResult);

    constructor.modifiers = toEclipseModifier(AccessLevel.PROTECTED);
    constructor.selector = typeDeclaration.name;
    if (callBuilderBasedSuperConstructor) {
        constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.Super);
        constructor.constructorCall.arguments = new Expression[] {
                new SingleNameReference(BUILDER_VARIABLE_NAME, p) };
    } else {
        constructor.constructorCall = new ExplicitConstructorCall(ExplicitConstructorCall.ImplicitSuper);
    }
    constructor.constructorCall.sourceStart = source.sourceStart;
    constructor.constructorCall.sourceEnd = source.sourceEnd;
    constructor.thrownExceptions = null;
    constructor.typeParameters = null;
    constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG;
    constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = source.sourceStart;
    constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = source.sourceEnd;

    TypeReference[] wildcards = new TypeReference[] { new Wildcard(Wildcard.UNBOUND),
            new Wildcard(Wildcard.UNBOUND) };
    TypeReference builderType = new ParameterizedSingleTypeReference(builderClassName.toCharArray(),
            mergeToTypeReferences(typeParams, wildcards), 0, p);
    constructor.arguments = new Argument[] {
            new Argument(BUILDER_VARIABLE_NAME, p, builderType, Modifier.FINAL) };

    List<Statement> statements = new ArrayList<Statement>();

    for (BuilderFieldData fieldNode : builderFields) {
        char[] fieldName = removePrefixFromField(fieldNode.originalFieldNode);
        FieldReference fieldInThis = new FieldReference(fieldNode.rawName, p);
        int s = (int) (p >> 32);
        int e = (int) p;
        fieldInThis.receiver = new ThisReference(s, e);

        Expression assignmentExpr;
        if (fieldNode.singularData != null && fieldNode.singularData.getSingularizer() != null) {
            fieldNode.singularData.getSingularizer().appendBuildCode(fieldNode.singularData, typeNode,
                    statements, fieldNode.name, BUILDER_VARIABLE_NAME_STRING);
            assignmentExpr = new SingleNameReference(fieldNode.name, p);
        } else {
            char[][] variableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldName };
            long[] positions = new long[] { p, p };
            assignmentExpr = new QualifiedNameReference(variableInBuilder, positions, s, e);
        }
        Statement assignment = new Assignment(fieldInThis, assignmentExpr, (int) p);

        // In case of @Builder.Default, set the value to the default if it was NOT set in the builder.
        if (fieldNode.nameOfSetFlag != null) {
            char[][] setVariableInBuilder = new char[][] { BUILDER_VARIABLE_NAME, fieldNode.nameOfSetFlag };
            long[] positions = new long[] { p, p };
            QualifiedNameReference setVariableInBuilderRef = new QualifiedNameReference(setVariableInBuilder,
                    positions, s, e);

            MessageSend defaultMethodCall = new MessageSend();
            defaultMethodCall.sourceStart = source.sourceStart;
            defaultMethodCall.sourceEnd = source.sourceEnd;
            defaultMethodCall.receiver = new SingleNameReference(((TypeDeclaration) typeNode.get()).name, 0L);
            defaultMethodCall.selector = fieldNode.nameOfDefaultProvider;
            defaultMethodCall.typeArguments = typeParameterNames(
                    ((TypeDeclaration) typeNode.get()).typeParameters);

            Statement defaultAssignment = new Assignment(fieldInThis, defaultMethodCall, (int) p);
            IfStatement ifBlockForDefault = new IfStatement(setVariableInBuilderRef, assignment,
                    defaultAssignment, s, e);
            statements.add(ifBlockForDefault);
        } else {
            statements.add(assignment);
        }

        if (hasNonNullAnnotations(fieldNode.originalFieldNode)) {
            Statement nullCheck = generateNullCheck((FieldDeclaration) fieldNode.originalFieldNode.get(),
                    sourceNode);
            if (nullCheck != null)
                statements.add(nullCheck);
        }
    }

    constructor.statements = statements.isEmpty() ? null : statements.toArray(new Statement[0]);

    constructor.traverse(new SetGeneratedByVisitor(source), typeDeclaration.scope);

    injectMethod(typeNode, constructor);
}

From source file:lombok.eclipse.handlers.HandleSynchronized.java

License:Open Source License

@Override
public void handle(AnnotationValues<Synchronized> annotation, Annotation source, EclipseNode annotationNode) {
    handleFlagUsage(annotationNode, ConfigurationKeys.SYNCHRONIZED_FLAG_USAGE, "@Synchronized");

    int p1 = source.sourceStart - 1;
    int p2 = source.sourceStart - 2;
    long pos = (((long) p1) << 32) | p2;
    EclipseNode methodNode = annotationNode.up();
    if (methodNode == null || methodNode.getKind() != Kind.METHOD
            || !(methodNode.get() instanceof MethodDeclaration)) {
        annotationNode.addError("@Synchronized is legal only on methods.");
        return;/*from  ww  w.j  av a 2 s.  co m*/
    }

    MethodDeclaration method = (MethodDeclaration) methodNode.get();
    if (method.isAbstract()) {
        annotationNode.addError("@Synchronized is legal only on concrete methods.");
        return;
    }

    char[] lockName = createLockField(annotation, annotationNode, method.isStatic(), true);
    if (lockName == null)
        return;
    if (method.statements == null)
        return;

    Block block = new Block(0);
    setGeneratedBy(block, source);
    block.statements = method.statements;

    // Positions for in-method generated nodes are special
    block.sourceEnd = method.bodyEnd;
    block.sourceStart = method.bodyStart;

    Expression lockVariable;
    if (method.isStatic())
        lockVariable = new QualifiedNameReference(
                new char[][] { methodNode.up().getName().toCharArray(), lockName }, new long[] { pos, pos }, p1,
                p2);
    else {
        lockVariable = new FieldReference(lockName, pos);
        ThisReference thisReference = new ThisReference(p1, p2);
        setGeneratedBy(thisReference, source);
        ((FieldReference) lockVariable).receiver = thisReference;
    }
    setGeneratedBy(lockVariable, source);

    method.statements = new Statement[] { new SynchronizedStatement(lockVariable, block, 0, 0) };

    // Positions for in-method generated nodes are special
    method.statements[0].sourceEnd = method.bodyEnd;
    method.statements[0].sourceStart = method.bodyStart;

    setGeneratedBy(method.statements[0], source);

    methodNode.rebuild();
}