kit-sdq / XAnnotations

Additional active annotations for the Java-dialect Xtend

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

XAnnotations

Additional active annotations for the Java-dialect Xtend

Install from update site (only for use via "Install Software" in Eclipse)

@StaticDelegate

Use the active annotation @StaticDelegate in an Xtend class to automatically obtain delegation methods for all accessible static methods of other classes.

@StaticDelegate can be used, for example, in order to reduce the number of imports for static extension methods, which allow you to call static methods that take at least one parameter as if the were declared as local methods of the first parameter.

An example usage of @StaticDelegate can be found in the API for OCL-aligned extension methods.

import edu.kit.ipd.sdq.activextendannotations.StaticDelegate

@StaticDelegate(#[BooleanXOCLExtensions,ClassXOCLExtensions,...])
class XOCLExtensionsAPI {
  ...
}

@Lazy

@Lazy can be declared on fields in order to initialise them lazily. That means that their initialiser code will not be executed before its first access:

class Example {
	@Lazy String field = expensiveComputation()
	
	def expensiveComputation() {
		// will not be called before the first access of field
	}
}

To realise this behaviour, a getter getField will be generated through which the field will be accessed. field will be renamed to _field and should not be accessed directly. For more information, see the annotation’s documentation.

@Utility

@Utility can be declared on classes to make them Utility classes. This means that they:

  • are final
  • only have one private, parameterless constructor that does nothing
  • may only have static methods
  • may have no fields but static final ones

This annotation is meant to save some keystrokes, but to make Utility classes easier to detect.

@DelegateDeclared

@DelegateDeclared is a variant of the Xtend @Delegate active annotation that only delegates members that are declared in the interfaces which are implemented directly by the class that uses this annotation.

For example, when delegating methods from an interface, only the methods declared in that interface are delegated. The methods that are declared in other interfaces which this interface extends are not delegated.

@CloseResource

@CloseResource is a replacement for Java’s try with resources, which is not supported in Xtend. Resource parameters annotated with it will be closed when the method exits – regardless if that happens by returning or by throwing an exception.

So instead of writing (in Java):

public static void writeToFileZipFileContents(String zipFileName,
    String outputFileName) throws IOException {
	Charset charset = StandardCharsets.US_ASCII;
    Path outputFilePath = Paths.get(outputFileName);

    try (
        ZipFile zf = new ZipFile(zipFileName);
        BufferedWriter writer = Files.newBufferedWriter(outputFilePath, charset)
    ) {
        for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
            String newLine = System.getProperty("line.separator");
            String zipEntryName =
                 ((ZipEntry)entries.nextElement()).getName() + newLine;
            writer.write(zipEntryName, 0, zipEntryName.length());
        }
    }
}

one would write in Xtend:

def static void writeToFileZipFileContents(String zipFileName,
    String outputFileName) throws IOException {
    val charset = StandardCharsets.US_ASCII
    val outputFilePath = Paths.get(outputFileName)

    writeZipFile(new ZipFile(zipFileName),
        Files.newBufferedWriter(outputFilePath, charset))
}

def private static void writeZipFile(@CloseResource ZipFile zf,
    @CloseResource Writer writer) {
    for (var entries = zf.entries(); entries.hasMoreElements();) {
        String newLine = System.getProperty("line.separator")
        String zipEntryName =
             ((ZipEntry)entries.nextElement()).getName() + newLine
        writer.write(zipEntryName, 0, zipEntryName.length())
    }
}

(the example was taken from the Oracle Docs)

About

Additional active annotations for the Java-dialect Xtend

License:Eclipse Public License 2.0


Languages

Language:Xtend 76.3%Language:HTML 23.7%