Skip to content

Commit

Permalink
Initial commit - draft project
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-ashraf-bayor committed Nov 29, 2021
0 parents commit 4591f34
Show file tree
Hide file tree
Showing 16 changed files with 577 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Project exclude paths
/target/
.idea/
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Mohamed Ashraf Bayor

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
## JISEL: Java Interface Segregation Library
Interface Segregation Library for Java 17

Pitch Video:
...

### How to Install ?

If you are running a Maven project, add the latest release dependency to your pom.xml
```xml
<dependency>
<groupId>org.jisel</groupId>
<artifactId>jisel</artifactId>
<version>1.0</version>
</dependency>
```
For other build tools, please check: [Maven Central](https://search.maven.org/artifact/org.jisel/jisel/1.0/jar).

### Use on your declared bloated interfaces methods

```java
import SealForProfile;

public interface InterfaceA {
@SealForProfile
void something();
}
```

### Use on your declared child classes implementing generated sealed interfaces

```java
import AddToProfile;

@AddToProfile("PROFILE_NAME")
public final class ClientA implements SealedInterfaceA {
// ...
}
```

### Sample classes for testing
[https://github.com/mohamed-ashraf-bayor/jisel-annotation-client](https://github.com/mohamed-ashraf-bayor/jisel-annotation-client)

### Invalid Uses of Jisel
The annotation should be used ONLY on interfaces created in your own project.

### Issues, Bugs, Suggestions
Contribute to the project's growth by reporting issues or making improvement suggestions [here](https://github.com/mohamed-ashraf-bayor/jisel/issues/new/choose)
175 changes: 175 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>org.jisel</groupId>
<artifactId>jisel</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Jisel</name>
<description>Interface Segregation Library for Java 17</description>
<url>http://jisel.org</url>

<licenses>
<license>
<name>MIT License</name>
<url>https://opensource.org/licenses/MIT</url>
<distribution>repo</distribution>
</license>
</licenses>

<scm>
<connection>scm:git:git://github.com/mohamed-ashraf-bayor/froporec.git</connection>
<developerConnection>scm:git:git@github.com:mohamed-ashraf-bayor/mohamed-ashraf-bayor.git</developerConnection>
<url>https://github.com/mohamed-ashraf-bayor/jisel</url>
<tag>HEAD</tag>
</scm>

<developers>
<developer>
<name>Mohamed Ashraf Bayor</name>
<organization>Jisel.org</organization>
<organizationUrl>https://jisel.org/</organizationUrl>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.release>17</maven.compiler.release>
<auto-service.version>1.0.1</auto-service.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-deploy-plugin.version>2.8.2</maven-deploy-plugin.version>
<maven-release-plugin.version>2.5.3</maven-release-plugin.version>
<nexus-staging-maven-plugin.version>1.6.7</nexus-staging-maven-plugin.version>
<maven-source-plugin.version>3.2.1</maven-source-plugin.version>
<maven-gpg-plugin.version>1.5</maven-gpg-plugin.version>
<maven-javadoc-plugin.version>3.3.1</maven-javadoc-plugin.version>
</properties>

<dependencies>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
<version>${auto-service.version}</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<release>17</release>
<compilerArgs>-Xlint:unchecked</compilerArgs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>${maven-deploy-plugin.version}</version>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>${maven-release-plugin.version}</version>
<configuration>
<localCheckout>true</localCheckout>
<pushChanges>false</pushChanges>
<mavenExecutorId>forked-path</mavenExecutorId>
<arguments>-Dgpg.passphrase=${gpg.passphrase}</arguments>
<autoVersionSubmodules>true</autoVersionSubmodules>
<useReleaseProfile>false</useReleaseProfile>
<releaseProfiles>release</releaseProfiles>
<goals>deploy</goals>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.scm</groupId>
<artifactId>maven-scm-provider-gitexe</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>${nexus-staging-maven-plugin.version}</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://s01.oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>${maven-source-plugin.version}</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${maven-javadoc-plugin.version}</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>${maven-gpg-plugin.version}</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
<configuration>
<keyname>${gpg.keyname}</keyname>
<passphraseServerId>${gpg.keyname}</passphraseServerId>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/</url>
</repository>
</distributionManagement>

</project>
21 changes: 21 additions & 0 deletions src/main/java/org/jisel/AddToProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jisel;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
@Repeatable(AddToProfile.AddToProfiless.class)
public @interface AddToProfile {

String value();

@Retention(RetentionPolicy.SOURCE)
@Target(ElementType.TYPE)
@interface AddToProfiless {
AddToProfile[] value();
}
}
12 changes: 12 additions & 0 deletions src/main/java/org/jisel/AddToProfiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.jisel;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
public @interface AddToProfiles {
String[] value();
}
103 changes: 103 additions & 0 deletions src/main/java/org/jisel/JiselAnnotationProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package org.jisel;

import com.google.auto.service.AutoService;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes;
import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.TypeElement;
import javax.tools.JavaFileObject;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Set;

// @Slf4j
@SupportedAnnotationTypes("com.bayor.jisel.annotation.SealFor")
@SupportedSourceVersion(SourceVersion.RELEASE_17)
@AutoService(Processor.class)
public class JiselAnnotationProcessor extends AbstractProcessor {

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnvironment) {

for (TypeElement annotation : annotations) {

if (!annotation.getSimpleName().toString().contains("SealFor"))
continue;

Set<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(annotation);

System.out.println(">>>>>>>> annotatedElements >>>>>>>>>>>" + annotatedElements);

List<? extends Element> annotatedClasses = annotatedElements.stream()
//.filter(element -> element.getClass().getClassLoader().isRecord())
.filter(element -> ElementKind.INTERFACE.equals(element.getKind()))
.toList();

System.out.println(">>>>>>>> annotatedClasses >>>>>>>>>>>" + annotatedClasses);

List<? extends Element> annotatedMethods = annotatedElements.stream()
.filter(element -> !element.getClass().isRecord())
.filter(element -> ElementKind.METHOD.equals(element.getKind()))
.toList();

System.out.println(">>>>>>>> annotateMethods >>>>>>>>>>>" + annotatedMethods);
}

return true;
}

private void writeSealedInterfaceFile(String className, List<Element> gettersList, Map<String, String> getterMap, Set<Element> allAnnotatedElements) throws IOException {
String recordClassString = buildSealedInterfaceContent(className, gettersList, getterMap, allAnnotatedElements);
JavaFileObject recordClassFile = processingEnv.getFiler().createSourceFile(className + "Record");
try (PrintWriter out = new PrintWriter(recordClassFile.openWriter())) {
out.println(recordClassString);
}
}

private String buildSealedInterfaceContent(String className, List<Element> gettersList, Map<String, String> getterMap, Set<Element> allAnnotatedElements) {

StringBuilder recordClassContent = new StringBuilder();

String packageName = null;

int lastDot = className.lastIndexOf('.');

if (lastDot > 0) {
packageName = className.substring(0, lastDot);
}

String simpleClassName = className.substring(lastDot + 1);
String recordClassName = className + "Record";
String recordSimpleClassName = recordClassName.substring(lastDot + 1);

if (packageName != null) {
recordClassContent.append("package ");
recordClassContent.append(packageName);
recordClassContent.append(";");
recordClassContent.append("\n\n");
}

recordClassContent.append("public record ");
recordClassContent.append(recordSimpleClassName);
recordClassContent.append("(");

// // System.out.println("################################" + recordSimpleClassName);
// buildRecordAttributesFromGettersList(recordClassContent, getterMap, gettersList, allAnnotatedElements);

recordClassContent.append(") {\n\n");

// buildRecordCustom1ArgConstructor(recordClassContent, simpleClassName, gettersList, allAnnotatedElements);

recordClassContent.append("}");

return recordClassContent.toString();
}
}
Loading

0 comments on commit 4591f34

Please sign in to comment.