Slide 1

Slide 1 text

The Hidden Secret of Java Open Source Projects Eder Ignatowicz Senior Software Engineer Red Hat Alex Porcelli Principal Software Engineer Red Hat

Slide 2

Slide 2 text

@Entity @Table(name = "stock_daily_record", catalog = “ederign", uniqueConstraints = @UniqueConstraint(columnNames = "DATE")) public class StockDailyRecord implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) @Column(name = "RECORD_ID", unique = true, nullable = false) public Integer getRecordId() { return this.recordId; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "STOCK_ID", nullable = false) public Stock getStock() { return this.stock; } @Column(name = "PRICE_CHANGE", precision = 6) public Float getPriceChange() { return this.priceChange; } }

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Entity { String name() default ""; }

Slide 5

Slide 5 text

Annotations Metadata Tooling Code Generation Reduce Boilerplate

Slide 6

Slide 6 text

Annotations Runtime Compile time

Slide 7

Slide 7 text

@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) public @interface Entity { String name() default ""; }

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

JDora UnitTest Framework

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Annotations Runtime

Slide 12

Slide 12 text

Annotations Compile Time

Slide 13

Slide 13 text

Annotation Processing

Slide 14

Slide 14 text

Annotation Processing Part of Compilation Process Annotated Sources Scanned Code Generation Enhance Compilation Feedback Reduce boilerplate <3

Slide 15

Slide 15 text

Annotation Processing Don’ts: Inject code Change existing sources Bytecode manipulation

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

package com.example; // PackageElement public class Foo { // TypeElement private int a; // VariableElement private Foo other; // VariableElement public Foo () {} // ExecutableElement public void setA () // ExecutableElement }

Slide 19

Slide 19 text

//@SupportedAnnotationTypes( {"javax.persistence.Entity", "javax.persistence.OneToMany"}) @SupportedSourceVersion(SourceVersion.RELEASE_7) public class JpaProcessor extends AbstractProcessor { @Override public void init(ProcessingEnvironment env) { super.init(env); } @Override public Set getSupportedAnnotationTypes() { return new HashSet() {{ add(PrintMe.class.getCanonicalName()); }}; } @Override public boolean process( Set extends TypeElement> annotations, RoundEnvironment roundEnv) { //seu processamento return false; // continua processando? } }

Slide 20

Slide 20 text

Real world…

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

Important Architecture Tool

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

Preferences Framework

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

@WorkbenchPreference(identifier = "MyPreference", bundleKey = "MyPreference.Label") public class MyPreference implements BasePreference { @Property(bundleKey = "MyPreference.Text", helpBundleKey = "MyPreference.Text.Help", validators = NotEmptyValidator.class, formOptions = PropertyFormOptions.DISABLED) String text; @Property(formType = PropertyFormType.BOOLEAN, bundleKey = "MyPreference.SendReports") boolean sendReports; @Property(formType = PropertyFormType.COLOR, bundleKey = "MyPreference.BackgroundColor") String backgroundColor; @Property(formType = PropertyFormType.NATURAL_NUMBER, bundleKey = "MyPreference.Age") int age; @Property(formType = PropertyFormType.SECRET_TEXT, bundleKey = "MyPreference.Password") String password; @Property(bundleKey = "MyPreference.MyInnerPreference") MyInnerPreference myInnerPreference; @Property(shared = true, bundleKey = "MyPreference.MySharedPreference") MySharedPreference mySharedPreference; @Override public MyPreference defaultValue(final MyPreference defaultValue) { defaultValue.text = "text"; defaultValue.sendReports = true; defaultValue.backgroundColor = "ABCDEF"; defaultValue.age = 27; defaultValue.password = "password"; defaultValue.myInnerPreference.text = "text"; return defaultValue; } }

Slide 28

Slide 28 text

public class MyServerBean { @Inject private MyPreference myPreference; public void load() { // Loads the preference content from the file system myPreference.load(); myPreference.text = "text"; myPreference.sendReports = true; myPreference.backgroundColor = "ABCDEF"; myPreference.age = 27; myPreference.password = "password"; myPreference.myInnerPreference.text = "text"; myPreference.myInheritedPreference.text = "text"; myPreference.myInheritedPreference.myInnerPreference2.text = "text"; myPreference.myInheritedPreference.myInnerPreference2.myInheritedPreference2.text = "text"; // Saves the modified preference content. myPreference.save(); } }

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

No content

Slide 31

Slide 31 text

AppFormer UI Components

Slide 32

Slide 32 text

Perspective Editor Screen Screen

Slide 33

Slide 33 text

Contract Based Screen -> Interface WorkbenchScreenActivity Editor -> Interface WorkbenchEditorActivity Perspective -> Interface PerspectiveActivity

Slide 34

Slide 34 text

GWT Perspective OLD GWT WIDGET ERRAI UI Perspective Plain HTML Errai UI Angular Perspective Angular native Perspective Any JS Perspective Any JS Framework AppFormer Lookup Perspective CDI-Lite CDI BEAN MANAGER Scan all Perspective Activity

Slide 35

Slide 35 text

@WorkbenchPerspective(identifier = “HomePerspective", isDefault = true) @Templated public class HomePerspective implements IsElement { @Inject @DataField @WorkbenchPanel(parts = "MoodScreen?uber=fire&uber1=fire1") Div moodScreen; @Inject @DataField @WorkbenchPanel(parts = "HomeScreen?uber=fire") Div homeScreen; @Inject @DataField @WorkbenchPanel(parts = "AnotherScreen") Div anotherScreen; } @JsType public interface PerspectiveActivity{ PerspectiveDefinition getDefaultPerspectiveLayout(); @Override default String getName() { return getDefaultPerspectiveLayout().getName(); } boolean isDefault(); Menus getMenus(); ToolBar getToolBar(); }

Slide 36

Slide 36 text

@Dependent @Generated("org.uberfire.annotations.processors.WorkbenchScreenProcessor") @Named("HomeScreen") public class HomeScreenActivity extends AbstractWorkbenchScreenActivity { @Inject private HomeScreen realPresenter; @Inject //Constructor injection for testing public HomeScreenActivity(final PlaceManager placeManager) { super( placeManager ); } @Override public void onStartup(final PlaceRequest place) { super.onStartup( place ); realPresenter.onStartup(); } @Override public void onClose() { super.onClose(); realPresenter.onClose(); } @Override public void onShutdown() { super.onShutdown(); realPresenter.onShutdown();

Slide 37

Slide 37 text

Annotations

Slide 38

Slide 38 text

+

Slide 39

Slide 39 text

Reflection

Slide 40

Slide 40 text

+

Slide 41

Slide 41 text

Annotation Processors

Slide 42

Slide 42 text

+

Slide 43

Slide 43 text

Open Source

Slide 44

Slide 44 text

Eder Ignatowicz @ederign Thank you! <3 Alex Porcelli @porcelli