SlideShare uma empresa Scribd logo
1 de 35
Semana da Tecnologia 2016
Turbinando o
compilador do Java 8
Semana da Tecnologia 2016
Marcelo de Castro
marcelo@castro.eti.br
@mcastroinfo
Plugin para o javac 8
0101010101011111000000000101001110001
1100011100000001111010101010110001110
1001001010000101001010101011010110111
• Por quê, para quê, desenvolver
plugins para o compilador do
Java?
– Adicionar checagem extras em tempo
de compilação
– Adicionar transformações de código
– Customizar análise do código fonte
Este código compila?
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
Java Code Conventions
Java Code Conventions
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
Inner
Java Code Conventions
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
ex
Java Code Conventions
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
VAR
Effective Java
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
vazio
Effective Java
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
Muitos
parâmetros
Jogo dos 5 erros(!?)
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
VAR
ex
Inner
vazio
Muitos
parâmetros
Compilar ou não compilar
Teste.java
package br.unip.sicc;
public class Teste {
public static final int var = 0;
public static void main(String[] args) {
int i = 0;
System.out.println("UNIP");
try{
i++;
}catch(Exception Ex){
}
}
public static void method(int a, int b, int c, int d,
int e, int f, int g, int h, int i, int j, int k){
}
class inner{}
}
Alterando o
comportamento do javac
1. Usar Java 8 (tools.jar no CLASSPATH)
2. Implementar Plugin
3. Implementar TaskListener
4. Implementar a lógica desejada
– (TreePathScanner, TreeScanner, TreeVisitor)
– Ex: Não aceitar bloco catch vazio
5. Gerar o jar do Plugin
– META-INF/services/
• com.sun.source.util.Plugin
– NomeTotalmenteQualificadoDoPlugin
6. Compilar usando o plugin
– javac -processorpath CodeCheckPlugin.jar -
Xplugin:CodeCheckPlugin Teste.java
Alterando o
comportamento do javac
1. Usar Java 8 (tools.jar no CLASSPATH)
2. Implementar Plugin
3. Implementar TaskListener
4. Implementar a lógica desejada
– (TreePathScanner, TreeScanner, TreeVisitor)
– Ex: Não aceitar bloco catch vazio
5. Gerar o jar do Plugin
– META-INF/services/
• com.sun.source.util.Plugin
– NomeTotalmenteQualificadoDoPlugin
6. Compilar usando o plugin
– javac -processorpath CodeCheckPlugin.jar -
Xplugin:CodeCheckPlugin Teste.java
DEMO
0101010101011111000000000101001110001
1100011100000001111010101010110001110
1001001010000101001010101011010110111
API
0101010101011111000000000101001110001
1100011100000001111010101010110001110
1001001010000101001010101011010110111
interfaces
com.sun.source.util
• Plugin
– permite definir um plugin para o
compilador
• SourcePositions
– fornece métodos para obter a
posição corrente da compilação
• TaskListener
– fornece um listener para monitorar
atividades do javac
interface Plugin
public class CodeCheckPlugin implements Plugin {
@Override
public String getName() {
return "CodeCheckPlugin";
}
@Override
public void init(JavacTask task, String... strings) {
System.out.println("Iniciando plugin!");
task.setTaskListener(new CheckTaskListener(task));
}
}
interface TaskListener
public class CheckTaskListener implements TaskListener{
@Override
public void started(TaskEvent taskEvent) {
if(taskEvent.getKind().equals(TaskEvent.Kind.ANALYZE)) {
CompilationUnitTree compUnit =
taskEvent.getCompilationUnit();
TreePathScanner emptyCacthVisitor =
new EmptyCacthVisitor(task);
emptyCacthVisitor.scan(compUnit, null);
}
}
@Override
public void finished(TaskEvent te) {
}
}
interface TaskListener
public class CheckTaskListener implements TaskListener{
@Override
public void started(TaskEvent taskEvent) {
if(taskEvent.getKind().equals(TaskEvent.Kind.ANALYZE)) {
CompilationUnitTree compUnit =
taskEvent.getCompilationUnit();
TreePathScanner emptyCacthVisitor =
new EmptyCacthVisitor(task);
emptyCacthVisitor.scan(compUnit, null);
}
}
@Override
public void finished(TaskEvent te) {
}
}
TaskEvent.Kind
•PARSE
•ENTER
•ANNOTATION_PROCESSING
•ANNOTATION_PROCESSING_ROUND
•ANALYZE
•GENERATE
Source File
Teste.java
Byte Code
Teste.class
interface SourcePositions
SourcePositions sourcePositions =
trees.getSourcePositions();
sourcePositions.getStartPosition
(compilationUnitTree, tree);
sourcePositions.getEndPosition
(compilationUnitTree, tree);
Classes (Syntax Trees)
com.sun.source.util
com.sun.source.tree
• public abstract class Trees
• public abstract class DocTrees extends Trees
• public interface Tree
• public enum Tree.Kind
• public interface CompilationUnitTree extends Tree
• public interface MethodTree extends Tree
• public interface BlockTree extends Tree
• public interface CatchTree extends Tree
• public interface StatementTree extends Tree
• public interface ModifiersTree extends Tree
• public interface VariableTree extends Tree
• public interface PrimitiveTypeTree extends Tree
• + um montão de Tree
Abstract Syntax Trees
Exemplo da Gramática da Linguagem Java
normalClassDeclaration
: 'class' Identifier typeParameters?
('extends' type)?
('implements' typeList)?
classBody
public class Teste
extends TesteBase{
}
class TesteBase{}
Abstract
Syntax Tree
SHOW ME THE CODE
0101010101011111000000000101001110001
1100011100000001111010101010110001110
1001001010000101001010101011010110111
Referências
• OpenJDK 8 http://openjdk.java.net/
• API Specification, The Java Programming Language Compiler
http://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/index.html
• Sun Microsystems, Java Code Conventions, 1997
• Compiler Grammar http://openjdk.java.net/projects/compiler-grammar/
• Effective Java by Joshua Block, 2008
• Hacking the Java Compiler for Fun and Profit By Raoul-Gabriel Urma
• “Java Compiler Plugins”, Oracle Java Magazine January/February 2013,
Raoul-Gabriel Urma& Jonathan Gibbons http://www.oraclejavamagazine-
digital.com/javamagazine/20130102?sub_id=jFwJyABK25xj#pg56
Marcelo de Castro
marcelo@castro.eti.br
@mcastroinfo
OBRIGADO

Mais conteúdo relacionado

Mais procurados

MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system Tarin Gamberini
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydneyjulien.ponge
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Groovy 1.8の新機能について
Groovy 1.8の新機能についてGroovy 1.8の新機能について
Groovy 1.8の新機能についてUehara Junji
 
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...Uehara Junji
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test frameworkAbner Chih Yi Huang
 
GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?Patrick Lam
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantesmikaelbarbero
 
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)Danny Preussler
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...julien.ponge
 
Unit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaUnit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaRobot Media
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnitEdorian
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnitEdorian
 

Mais procurados (19)

MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
MUTANTS KILLER (Revised) - PIT: state of the art of mutation testing system
 
Lab4
Lab4Lab4
Lab4
 
Software Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW SydneySoftware Testing - Invited Lecture at UNSW Sydney
Software Testing - Invited Lecture at UNSW Sydney
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Groovy 1.8の新機能について
Groovy 1.8の新機能についてGroovy 1.8の新機能について
Groovy 1.8の新機能について
 
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
Let's go Developer 2011 sendai Let's go Java Developer (Programming Language ...
 
An introduction to Google test framework
An introduction to Google test frameworkAn introduction to Google test framework
An introduction to Google test framework
 
GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Google Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG NantesGoogle Guava & EMF @ GTUG Nantes
Google Guava & EMF @ GTUG Nantes
 
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
15 tips to improve your unit tests (Droidcon Berlin 2016 Barcamp)
 
54240326 copy
54240326   copy54240326   copy
54240326 copy
 
Test Time Bombs
Test Time BombsTest Time Bombs
Test Time Bombs
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Java 7 LavaJUG
Java 7 LavaJUGJava 7 LavaJUG
Java 7 LavaJUG
 
Exception handling
Exception handlingException handling
Exception handling
 
Unit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon GaliciaUnit testing en iOS @ MobileCon Galicia
Unit testing en iOS @ MobileCon Galicia
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 
The State of PHPUnit
The State of PHPUnitThe State of PHPUnit
The State of PHPUnit
 

Semelhante a Turbinando o compilador do Java 8 (20)

Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
Java programs
Java programsJava programs
Java programs
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
Core java
Core javaCore java
Core java
 
Sam wd programs
Sam wd programsSam wd programs
Sam wd programs
 
Object oriented programming la bmanual jntu
Object oriented programming la bmanual jntuObject oriented programming la bmanual jntu
Object oriented programming la bmanual jntu
 
Java Programs
Java ProgramsJava Programs
Java Programs
 
Java Quiz Questions
Java Quiz QuestionsJava Quiz Questions
Java Quiz Questions
 
54240326 (1)
54240326 (1)54240326 (1)
54240326 (1)
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
 
Core java Essentials
Core java EssentialsCore java Essentials
Core java Essentials
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
STAMP Descartes Presentation
STAMP Descartes PresentationSTAMP Descartes Presentation
STAMP Descartes Presentation
 
Initial Java Core Concept
Initial Java Core ConceptInitial Java Core Concept
Initial Java Core Concept
 
3 j unit
3 j unit3 j unit
3 j unit
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
 
DAY_1.2.pptx
DAY_1.2.pptxDAY_1.2.pptx
DAY_1.2.pptx
 

Último

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 

Último (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 

Turbinando o compilador do Java 8

  • 2. Turbinando o compilador do Java 8 Semana da Tecnologia 2016
  • 4. Plugin para o javac 8 0101010101011111000000000101001110001 1100011100000001111010101010110001110 1001001010000101001010101011010110111
  • 5. • Por quê, para quê, desenvolver plugins para o compilador do Java? – Adicionar checagem extras em tempo de compilação – Adicionar transformações de código – Customizar análise do código fonte
  • 6. Este código compila? Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} }
  • 7. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} }
  • 10. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } Inner
  • 12. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } ex
  • 14. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } VAR
  • 16. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } vazio
  • 18. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } Muitos parâmetros
  • 19. Jogo dos 5 erros(!?) Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} } VAR ex Inner vazio Muitos parâmetros
  • 20. Compilar ou não compilar Teste.java package br.unip.sicc; public class Teste { public static final int var = 0; public static void main(String[] args) { int i = 0; System.out.println("UNIP"); try{ i++; }catch(Exception Ex){ } } public static void method(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k){ } class inner{} }
  • 21. Alterando o comportamento do javac 1. Usar Java 8 (tools.jar no CLASSPATH) 2. Implementar Plugin 3. Implementar TaskListener 4. Implementar a lógica desejada – (TreePathScanner, TreeScanner, TreeVisitor) – Ex: Não aceitar bloco catch vazio 5. Gerar o jar do Plugin – META-INF/services/ • com.sun.source.util.Plugin – NomeTotalmenteQualificadoDoPlugin 6. Compilar usando o plugin – javac -processorpath CodeCheckPlugin.jar - Xplugin:CodeCheckPlugin Teste.java
  • 22. Alterando o comportamento do javac 1. Usar Java 8 (tools.jar no CLASSPATH) 2. Implementar Plugin 3. Implementar TaskListener 4. Implementar a lógica desejada – (TreePathScanner, TreeScanner, TreeVisitor) – Ex: Não aceitar bloco catch vazio 5. Gerar o jar do Plugin – META-INF/services/ • com.sun.source.util.Plugin – NomeTotalmenteQualificadoDoPlugin 6. Compilar usando o plugin – javac -processorpath CodeCheckPlugin.jar - Xplugin:CodeCheckPlugin Teste.java
  • 25. interfaces com.sun.source.util • Plugin – permite definir um plugin para o compilador • SourcePositions – fornece métodos para obter a posição corrente da compilação • TaskListener – fornece um listener para monitorar atividades do javac
  • 26. interface Plugin public class CodeCheckPlugin implements Plugin { @Override public String getName() { return "CodeCheckPlugin"; } @Override public void init(JavacTask task, String... strings) { System.out.println("Iniciando plugin!"); task.setTaskListener(new CheckTaskListener(task)); } }
  • 27. interface TaskListener public class CheckTaskListener implements TaskListener{ @Override public void started(TaskEvent taskEvent) { if(taskEvent.getKind().equals(TaskEvent.Kind.ANALYZE)) { CompilationUnitTree compUnit = taskEvent.getCompilationUnit(); TreePathScanner emptyCacthVisitor = new EmptyCacthVisitor(task); emptyCacthVisitor.scan(compUnit, null); } } @Override public void finished(TaskEvent te) { } }
  • 28. interface TaskListener public class CheckTaskListener implements TaskListener{ @Override public void started(TaskEvent taskEvent) { if(taskEvent.getKind().equals(TaskEvent.Kind.ANALYZE)) { CompilationUnitTree compUnit = taskEvent.getCompilationUnit(); TreePathScanner emptyCacthVisitor = new EmptyCacthVisitor(task); emptyCacthVisitor.scan(compUnit, null); } } @Override public void finished(TaskEvent te) { } }
  • 30. interface SourcePositions SourcePositions sourcePositions = trees.getSourcePositions(); sourcePositions.getStartPosition (compilationUnitTree, tree); sourcePositions.getEndPosition (compilationUnitTree, tree);
  • 31. Classes (Syntax Trees) com.sun.source.util com.sun.source.tree • public abstract class Trees • public abstract class DocTrees extends Trees • public interface Tree • public enum Tree.Kind • public interface CompilationUnitTree extends Tree • public interface MethodTree extends Tree • public interface BlockTree extends Tree • public interface CatchTree extends Tree • public interface StatementTree extends Tree • public interface ModifiersTree extends Tree • public interface VariableTree extends Tree • public interface PrimitiveTypeTree extends Tree • + um montão de Tree
  • 32. Abstract Syntax Trees Exemplo da Gramática da Linguagem Java normalClassDeclaration : 'class' Identifier typeParameters? ('extends' type)? ('implements' typeList)? classBody public class Teste extends TesteBase{ } class TesteBase{} Abstract Syntax Tree
  • 33. SHOW ME THE CODE 0101010101011111000000000101001110001 1100011100000001111010101010110001110 1001001010000101001010101011010110111
  • 34. Referências • OpenJDK 8 http://openjdk.java.net/ • API Specification, The Java Programming Language Compiler http://docs.oracle.com/javase/8/docs/jdk/api/javac/tree/index.html • Sun Microsystems, Java Code Conventions, 1997 • Compiler Grammar http://openjdk.java.net/projects/compiler-grammar/ • Effective Java by Joshua Block, 2008 • Hacking the Java Compiler for Fun and Profit By Raoul-Gabriel Urma • “Java Compiler Plugins”, Oracle Java Magazine January/February 2013, Raoul-Gabriel Urma& Jonathan Gibbons http://www.oraclejavamagazine- digital.com/javamagazine/20130102?sub_id=jFwJyABK25xj#pg56