-
Notifications
You must be signed in to change notification settings - Fork 106
Add api for static initializers #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
bc097d3
Add api for static initializers
Lauriichan 531b84f
Update modifier handling in InitializerImpl
gastaldi 7f675a5
Apply suggestions from code review
gastaldi 4920adb
Merge branch 'master' into master
Lauriichan 668bb29
Add more tests, extract common code and add equals/hashCode to init.
Lauriichan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
api/src/main/java/org/jboss/forge/roaster/model/Initializer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.jboss.forge.roaster.model; | ||
|
|
||
| import org.jboss.forge.roaster.Internal; | ||
| import org.jboss.forge.roaster.Origin; | ||
|
|
||
| /** | ||
| * Represents a initializer of a {@link JavaClass}, {@link JavaRecord} or {@link JavaEnum}. | ||
| */ | ||
| public interface Initializer<O extends JavaType<O>, T extends Initializer<O, T>> extends StaticCapable, Internal, Origin<O> { | ||
|
|
||
| /** | ||
| * Get the inner body of this {@link Initializer} | ||
| */ | ||
| String getBody(); | ||
|
|
||
| } |
21 changes: 21 additions & 0 deletions
21
api/src/main/java/org/jboss/forge/roaster/model/InitializerHolder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package org.jboss.forge.roaster.model; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Represents a {@link JavaType} that may define initializers. | ||
| */ | ||
| public interface InitializerHolder<O extends JavaType<O>> { | ||
|
|
||
| /** | ||
| * Get a {@link List} of all {@link Initializer}s declared by this {@link O} instance, if any; otherwise return an empty | ||
| * {@link List} | ||
| */ | ||
| List<? extends Initializer<O, ?>> getInitializers(); | ||
|
|
||
| /** | ||
| * Return true if this {@link O} instance has the provided {@link Initializer}; otherwise false. | ||
| */ | ||
| boolean hasInitializer(Initializer<O, ?> initializer); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
api/src/main/java/org/jboss/forge/roaster/model/source/InitializerHolderSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package org.jboss.forge.roaster.model.source; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.jboss.forge.roaster.model.Initializer; | ||
| import org.jboss.forge.roaster.model.InitializerHolder; | ||
|
|
||
| /** | ||
| * Represents a {@link JavaSource} that may declare initializers. | ||
| */ | ||
| public interface InitializerHolderSource<O extends JavaSource<O>> extends InitializerHolder<O>, MemberHolderSource<O> { | ||
|
|
||
| /** | ||
| * Get a {@link List} of all {@link InitializerSource}s declared by this {@link O} instance, if any; otherwise return an | ||
| * empty {@link List} | ||
| */ | ||
| @Override | ||
| List<InitializerSource<O>> getInitializers(); | ||
|
|
||
| /** | ||
| * Add an uninitialized {@link InitializerSource} declaration to this {@link O} instance. This {@link InitializerSource} will | ||
| * be a stub until further modified. | ||
| */ | ||
| InitializerSource<O> addInitializer(); | ||
|
|
||
| /** | ||
| * Add a new {@link InitializerSource} declaration to this {@link O} instance, using the given {@link String} as the | ||
| * initializer declaration. | ||
| * <p/> | ||
| * <strong>For example:</strong><br> | ||
| * <code>Initializer initializer = javaClass.addInitializer("static { System.out.println(\"Hello\") }")</code> | ||
| */ | ||
| InitializerSource<O> addInitializer(final String body); | ||
|
|
||
| /** | ||
| * Remove the given {@link InitializerSource} declaration from this {@link O} instance, if it exists; otherwise, do | ||
| * nothing. | ||
| */ | ||
| O removeInitializer(final Initializer<O, ?> initializer); | ||
|
|
||
| } | ||
16 changes: 16 additions & 0 deletions
16
api/src/main/java/org/jboss/forge/roaster/model/source/InitializerSource.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package org.jboss.forge.roaster.model.source; | ||
|
|
||
| import org.jboss.forge.roaster.model.Initializer; | ||
|
|
||
| /** | ||
| * Represents a Java initializer in source form. | ||
| */ | ||
| public interface InitializerSource<O extends JavaSource<O>> extends Initializer<O, InitializerSource<O>>, | ||
| JavaDocCapableSource<InitializerSource<O>>, StaticCapableSource<InitializerSource<O>>, LocationCapable { | ||
|
|
||
| /** | ||
| * Set the inner body of this {@link Initializer} | ||
| */ | ||
| InitializerSource<O> setBody(String body); | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
impl/src/main/java/org/jboss/forge/roaster/model/ast/InitializerAccessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package org.jboss.forge.roaster.model.ast; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; | ||
| import org.eclipse.jdt.core.dom.BodyDeclaration; | ||
| import org.eclipse.jdt.core.dom.Initializer; | ||
| import org.jboss.forge.roaster.model.impl.InitializerImpl; | ||
| import org.jboss.forge.roaster.model.source.InitializerSource; | ||
| import org.jboss.forge.roaster.model.source.JavaSource; | ||
|
|
||
| public final class InitializerAccessor | ||
| { | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <O extends JavaSource<O>> List<InitializerSource<O>> getInitializers(O origin, | ||
| AbstractTypeDeclaration declaration) | ||
| { | ||
|
|
||
| List<InitializerSource<O>> result = new ArrayList<>(); | ||
| List<BodyDeclaration> bodyDeclarations = declaration.bodyDeclarations(); | ||
| for (BodyDeclaration bodyDeclaration : bodyDeclarations) | ||
| { | ||
| if (bodyDeclaration instanceof Initializer) | ||
| { | ||
| Initializer initializer = (Initializer) bodyDeclaration; | ||
| result.add(new InitializerImpl<>(origin, initializer)); | ||
| } | ||
| } | ||
| return Collections.unmodifiableList(result); | ||
| } | ||
|
|
||
| public static <O extends JavaSource<O>> boolean hasInitializer(AbstractTypeDeclaration declaration, | ||
| org.jboss.forge.roaster.model.Initializer<?, ?> initializer) | ||
| { | ||
| return declaration.bodyDeclarations().contains(initializer.getInternal()); | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <O extends JavaSource<O>> InitializerSource<O> addInitializer(O origin, | ||
| AbstractTypeDeclaration declaration) | ||
| { | ||
| InitializerImpl<O> init = new InitializerImpl<>(origin); | ||
| declaration.bodyDeclarations().add(init.getInternal()); | ||
| return init; | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| public static <O extends JavaSource<O>> InitializerSource<O> addInitializer(O origin, | ||
| AbstractTypeDeclaration declaration, String initializer) | ||
| { | ||
| InitializerImpl<O> init = new InitializerImpl<>(origin, initializer); | ||
| declaration.bodyDeclarations().add(init.getInternal()); | ||
| return init; | ||
| } | ||
|
|
||
| public static void removeInitializer(AbstractTypeDeclaration declaration, | ||
| org.jboss.forge.roaster.model.Initializer<?, ?> initializer) | ||
| { | ||
| declaration.bodyDeclarations().remove(initializer.getInternal()); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.