https://github.com/dmfs/srcless
Generate code too trivial to write it down.
https://github.com/dmfs/srcless
Last synced: 13 days ago
JSON representation
Generate code too trivial to write it down.
- Host: GitHub
- URL: https://github.com/dmfs/srcless
- Owner: dmfs
- License: apache-2.0
- Created: 2021-12-14T01:15:58.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-06T09:55:09.000Z (over 2 years ago)
- Last Synced: 2025-10-28T01:53:48.275Z (9 months ago)
- Language: Java
- Size: 126 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/dmfs/srcless/actions/workflows/main.yml)
[](https://codecov.io/gh/dmfs/srcless)
[](https://lgtm.com/projects/g/dmfs/srcless/context:java)
# srcless
Generate code too trivial to write it down.
## @Composable
Annotation for interfaces that creates an abstract base class that merely delegates to
another instance of the same type.
### Example
```java
/**
* FooBar interface.
*/
@Composable
public interface FooBar
{
void foo(T foo);
T bar(String bazz) throws IOException;
}
```
generates
```java
/**
* FooBar interface.
* Automatically generated class for composition.
*/
public abstract class FooBarComposition implements FooBar
{
private final FooBar mDelegate;
public FooBarComposition(FooBar delegate)
{
mDelegate = delegate;
}
public final void foo(T foo)
{
mDelegate.foo(foo);
}
public final T bar(String bazz) throws IOException
{
return mDelegate.bar(bazz);
}
}
```
## @StaticFactories, @StaticFactory
Annotation for classes/constructors that generates static factory methods for each public constructor.
### Example
```java
@StaticFactories("Foo")
public final class FactoryFoo
{
public FactoryFoo(@Nonnull final String s)
{
this(s, 0);
}
public FactoryFoo(@Nonnull final String s, int i)
{
}
}
```
generates
```java
/**
* Automatically generated class with static factory methods.
*/
public final class Foo
{
private Foo()
{
// no-instances constructor
}
public static FactoryFoo factoryFoo(@Nonnull final String s)
{
return new FactoryFoo(s);
}
public static FactoryFoo factoryFoo(@Nonnull final String s, int i)
{
return new FactoryFoo(s, i);
}
}
```
## nullless
The nullless annotation processor doesn't process any annotations.
It processes all packages in the project and generates a
`package-info.java` like this:
```Java
@NonNullByDefault
package
import org.eclipse.jdt.annotation.NonNullByDefault;
```
As the name says this makes everything in a package `NonNull` by default.
Applying the annotation processor like
```groovy
annotationProcessor 'org.dmfs:nullless-processors:0.3.0'
```
Applies the `@NonNullByDefault` annotation to all classes in the project.
To make this work you must also add a dependency on the respective eclipse annotation lib like so
```groovy
annotationProcessor 'org.dmfs:nullless-processors:0.3.0'
api 'org.eclipse.jdt:org.eclipse.jdt.annotation:2.2.600'
```
Because this applied automatically on all packages, it's not part of the scrless-processors artifact and has to be applied separately.
Note that you still can annotate members, methods and parameters with
`@NonNull` to override the default.
## License
Copyright 2023 dmfs GmbH
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.