Tải bản đầy đủ - 0 (trang)
Chapter 14. Builders, Markers, and Natures

Chapter 14. Builders, Markers, and Natures

Tải bản đầy đủ - 0trang

Thegoalofthischapteristodiscussbuilders,markers,and

naturesinthecontextofanewplugin.propertiesfileauditorin

theFavoritesproduct.Thepropertiesauditorisimplemented

asabuilderandcross-referencespropertykeysintheplugin.xml

withentriesintheplugin.propertiesfile.Markersareusedto

reportproblemsthattheauditorfinds;keysintheplugin.xml

thatarenotdeclaredintheplugin.propertiesfilearemarkedas

missing,whilekeysintheplugin.propertiesfilethatarenot



referencedintheplugin.xmlfilearemarkedasunused.Anew

projectnatureiscreatedtoassociatetheauditorwithaproject.



14.1.Builders

Abuilderisscopedtoaproject.Whenoneormoreresourcesin

aprojectchange,thebuildersassociatedwiththeprojectare

notified.Ifthesechangeshavebeenbatched(seeSection9.3,

BatchingChangeEvents,onpage382),thebuilderreceivesa

singlenotificationcontainingalistofallthechangedresources

ratherthanindividualnotificationsforeachchangedresource.



Tip

Ifyouwantaglobalbuildernotassociatedwithany

specificproject,hookintotheearlystartup

extensionpoint(seeSection3.4.2,Earlyplug-in

startup,onpage114)andaddaworkspaceresource

changelistener(seeSection9.1,

IResourceChangeListener,onpage375).The

downsideofthisapproachisthatthebuilderwill

consumememoryandexecutioncyclesregardlessof

whetheritisreallyneeded.



Buildersprocessthelistofchangesandupdatetheirbuildstate

byregeneratingthenecessaryderivedresources(seeSection

14.1.3,Derivedresources,onpage509),annotatingsource

resources,andsoon.Buildersarenotifiedwhenaresource

changes,suchaswhenausersavesamodifiedJavasourcefile,

andthusareexecutedquitefrequently.Becauseofthis,a

buildermustexecuteincrementally,meaningthatitmust

rebuildonlythosederivedresourcesthathavechanged.

IftheEclipseJavacompilerrebuiltalltheJavasourcefilesin

theprojecteverytimeasingleJavasourcefilewassaved,it



wouldbringEclipsetoitsknees.



14.1.1.Declaringabuilder

Thefirststepincreatingtheplugin.propertiesauditorinvolves

addingabuilderdeclarationtotheFavoritesplug-inmanifest.

Opentheplug-inmanifesteditorontheFavoritesplugin.xml

file,switchtotheExtensionspage,andaddan

org.eclipse.core.resources.buildersextension(seeFigure14-2).



Figure14-2.TheNewExtensionwizardshowing

theorg.eclipse.core.resources.builders



Clickontheorg.eclipse.core.resources.buildersextensiontoeditits

properties,andsettheidattributefortheextension(seeFigure

14-3).



Figure14-3.Theplug-inmanifesteditorshowing

thebuilder'sextension.



[Viewfullsizeimage]



id"propertiesFileAuditor"

Thelastsegmentofthebuilder'suniqueidentifier.Ifthe

declarationappearsinthecom.qualityeclipse.favoritesplugin,thenthebuilder'sfullyqualifiedidentifieris

com.qualityeclipse.favorites.propertiesFileAuditor.



Right-clickontheextensionandselectNew>builderinthe

contextmenu.Thebuilderelementhasthesetwoattributes

(seeFigure14-4).



Figure14-4.Theplug-inmanifesteditorshowing

thebuilder'sattributes.



[Viewfullsizeimage]



hasNature"true"

ABooleanindicatingwhetherthebuilderisownedbya

projectnature.Iftrueandnocorrespondingnatureis

found,thisbuilderwillnotrun,butwillremaininthe

project'sbuildspec.Iftheattributeisnotspecified,itis

assumedtobefalse.

isConfiguarbleLeaveblank

ABooleanindicatingwhetherthebuilderallows

customizationofwhichbuildtriggersitwillrespondto.If

TRue,clientswillbeabletousetheAPIICommand.setBuilding

tospecifywhetherthisbuildershouldberunfora

particularbuildtrigger.Iftheattributeisnotspecified,it

isassumedtobefalse.

Right-clickonthebuilderelementandselectNew>runinthe



contextmenutoassociateaJavaclasswiththebuilder.The

Javaclasswillprovidebehaviorforthebuilder.Therunelement

hasonlyoneattribute(seeFigure14-5),class,specifyingthe

Javaclasstobeexecuted.



Figure14-5.Theplug-inmanifesteditorshowing

therunattributes.



[Viewfullsizeimage]



Clicktheclass:labeltotherightoftheclassfieldandusethe

JavaAttributeEditortocreateanewclassintheFavorites

projectwiththespecifiedpackageandclassname.

class

"com.qualityeclipse.favorites.builder.PropertiesFileAuditor"

Thefullyqualifiednameofasubclassof

org.eclipse.core.resources.IncrementalProjectBuilder.Theclass



isinstantiatedusingitsnoargumentconstructorbutcan

beparameterizedusingtheIExecutableExtensioninterface

(seeSection20.5.1,Parameterizedtypes,onpage724).

ThecompletedeclarationintheFavoritesplug-inmanifest



shouldlooklikethis:




id="propertiesFileAuditor"

point="org.eclipse.core.resources.builders">




"com.qualityeclipse.favorites.builder.PropertiesFileAudit







14.1.2.IncrementalProjectBuilder

Theclassspecifiedinthedeclarationoftheprevioussection

mustbeasubclassofIncrementalProjectBuilder,andatthevery

least,shouldimplementthebuild()andclean()methods.The

build()methodiscalledbyEclipsewhenthebuildershould

eitherincrementallyorfullybuildrelatedfilesandmarkers.This

methodhasseveralargumentsprovidingbuildinformationand

amechanismfordisplayingprogresstotheuser.

kindThekindofbuildbeingrequested.Validvalues



include:FULL_BUILD,INCREMENTAL_BUILD,andAUTO_BUILD.

argsAmapofbuilder-specificargumentskeyedby



argumentname(keytype:String,valuetype:String)or

null,indicatinganemptymap.

monitorAprogressmonitor,ornullifprogressreporting



andcancellationarenotdesired.

Thekindargumentcanhaveoneofthefollowingseveralvalues.



FULL_BUILDThebuildershouldrebuildallderivedresources



andperformitsworkasifithasnotbeenexecuted

before.

CLEAN_BUILDThebuildershoulddeleteallderivedresources



andmarkersbeforeperformingafullbuild(seethe

discussionoftheclean()methodthatfollows).

INCREMENTAL_BUILDThebuildershouldonlyrebuildthose



derivedresourcesthatneedtobeupdatedandonly

performtheworkthatisnecessarybasedonitspriorbuild

state.

AUTO_BUILDSameasINCREMENTAL_BUILD,exceptthatthebuild



wasanautomaticallytriggeredincrementalbuild(autobuildingON).

CallingIWorkspace.build()orIProject.build()wheneverthebuild

kindisCLEAN_BUILDTRiggerstheclean()methodpriortocalling

thebuild()methodwiththebuildkindequaltoFULL_BUILD.The

clean()methodshoulddiscardanyadditionalstatethathasbeen

computedasaresultofpreviousbuildsincludingallderived

resourcesandallmarkersoftypeIMarker.PROBLEM.Theplatform

willtakecareofdiscardingthebuilder'slastbuiltstate(noneed

tocallforgetLastBuiltState()).Thefollowingareseveral

interestingmethodsinIncrementalProjectBuilder.

build(int,Map,IProgressMonitor)Overriddenbysubclassesto



performthebuildoperation.Seethedescriptionearlierin

thissectionandtheimplementationexamplelaterinthis

section.

clean(IProgressMonitor)Similartobuild(),exceptallderived



resources,generatedmarkers,andpreviousstateshould

bediscardedbeforebuilding.



forgetLastBuiltState()Requeststhatthisbuilderforgetany



stateitmaybecachingregardingpreviouslybuiltstates.

Thismayneedtobecalledbyasubclassifthebuild

processisinterruptedorcanceled(seecheckCancel()

methodlaterinthissection).

getCommand()Returnsthebuildcommandassociatedwith



thisbuilderwhichmaycontainproject-specific

configurationinformation(seeSection14.1.4,Associating

abuilderwithaproject,onpage509).

geTDelta(IProject)Returnstheresourcedeltarecordingthe



changesinthegivenprojectsincethelasttimethebuilder

wasrun,ornullifnosuchdeltaisavailable.SeeSection

9.2,ProcessingChangeEvents,onpage379fordetailson

processingresourcechangeeventsandtheshouldAudit()

methodlateroninthissection.

getProject()Returnstheprojectwithwhichthisbuilderis



associated.

isInterrupted()Returnswhetheraninterruptrequesthas



beenmadeforthisbuild.Backgroundauto-buildis

interruptedwhenanotherthreadtriestomodifythe

workspaceconcurrentlywiththebuildthread.See

shouldAudit()methodlateroninthissection.

setInitializationData(IConfigurationElement,String,Object)



Calledimmediatelyafterthebuilderisinstantiatedwith

configurationinformationspecifiedinthebuilder's

declaration(seeSection20.5.1,Parameterizedtypes,on

page724).

Afterdeclaringthebuilderintheprevioussection,youmust

implementPropertiesFileAuditor,asubclassof

org.eclipse.core.resources.IncrementalProjectBuilder,toperformthe



operation.Whenthebuild()methodiscalled,the

PropertiesFileAuditorbuilderdelegatestoshouldAudit()tosee

whetheranauditshouldbeperformedand,ifnecessary,to

auditPluginManifest()toperformtheaudit.

packagecom.qualityeclipse.favorites.builder;

import...

publicclassPropertiesFileAuditor

extendsIncrementalProjectBuilder

{

protectedIProject[]build(

intkind,

Mapargs,

IProgressMonitormonitor

)throwsCoreException

{

if(shouldAudit(kind)){

auditPluginManifest(monitor);

}

returnnull;

}

...othermethodsdiscussedlaterinsertedhere...

}



TheshouldAudit()methodchecksforFULL_BUILD,oriftheplugin.xml

orplugin.propertiesfilesofaprojecthavechanged(seeSection

9.2,ProcessingChangeEvents,onpage379).Ifabuilderhas

neverbeeninvokedbefore,thengetdelta()returnsnull.

privatebooleanshouldAudit(intkind){

if(kind==FULL_BUILD)



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 14. Builders, Markers, and Natures

Tải bản đầy đủ ngay(0 tr)

×