Search in sources :

Example 1 with ArrayAllocationExpression

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

the class HandleSynchronized method createLockField.

public char[] createLockField(AnnotationValues<Synchronized> annotation, EclipseNode annotationNode, boolean isStatic, boolean reportErrors) {
    char[] lockName = annotation.getInstance().value().toCharArray();
    Annotation source = (Annotation) annotationNode.get();
    boolean autoMake = false;
    if (lockName.length == 0) {
        autoMake = true;
        lockName = isStatic ? STATIC_LOCK_NAME : INSTANCE_LOCK_NAME;
    }
    if (fieldExists(new String(lockName), annotationNode) == MemberExistsResult.NOT_EXISTS) {
        if (!autoMake) {
            if (reportErrors)
                annotationNode.addError(String.format("The field %s does not exist.", new String(lockName)));
            return null;
        }
        FieldDeclaration fieldDecl = new FieldDeclaration(lockName, 0, -1);
        setGeneratedBy(fieldDecl, source);
        fieldDecl.declarationSourceEnd = -1;
        fieldDecl.modifiers = (isStatic ? Modifier.STATIC : 0) | Modifier.FINAL | Modifier.PRIVATE;
        //We use 'new Object[0];' because unlike 'new Object();', empty arrays *ARE* serializable!
        ArrayAllocationExpression arrayAlloc = new ArrayAllocationExpression();
        setGeneratedBy(arrayAlloc, source);
        arrayAlloc.dimensions = new Expression[] { makeIntLiteral("0".toCharArray(), source) };
        arrayAlloc.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(arrayAlloc.type, source);
        fieldDecl.type = new QualifiedTypeReference(TypeConstants.JAVA_LANG_OBJECT, new long[] { 0, 0, 0 });
        setGeneratedBy(fieldDecl.type, source);
        fieldDecl.initialization = arrayAlloc;
        // TODO temporary workaround for issue 217. http://code.google.com/p/projectlombok/issues/detail?id=217
        // injectFieldSuppressWarnings(annotationNode.up().up(), fieldDecl);
        injectField(annotationNode.up().up(), fieldDecl);
    }
    return lockName;
}
Also used : ArrayAllocationExpression(org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression) QualifiedTypeReference(org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference) Annotation(org.eclipse.jdt.internal.compiler.ast.Annotation) FieldDeclaration(org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)

Aggregations

Annotation (org.eclipse.jdt.internal.compiler.ast.Annotation)1 ArrayAllocationExpression (org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression)1 FieldDeclaration (org.eclipse.jdt.internal.compiler.ast.FieldDeclaration)1 QualifiedTypeReference (org.eclipse.jdt.internal.compiler.ast.QualifiedTypeReference)1