JavaML 2.0 Enriching the Markup Language for Java Source Code

合集下载

java lambda 循环的方法

java lambda 循环的方法

Java中的lambda表达式是一种简洁而强大的语法特性,它使得在函数式编程中能够更加流畅地使用匿名函数。

在循环的过程中,lambda 表达式也大大简化了代码的编写,使得代码更易读、易懂,提高了开发效率。

在本文中,我们将重点探讨在Java中使用lambda表达式进行循环的方法,包括基本语法、常见应用场景和一些注意事项。

一、基本语法在Java中,lambda表达式的基本语法为:(parameter_list) -> {body}其中,parameter_list为参数列表,可以为空或非空;箭头"->"为lambda运算符;{body}为lambda函数体,可以包含一条或多条语句。

在循环中使用lambda表达式,通常使用foreach循环进行演示。

示例如下:List<String> list = new ArrayList<>();list.add("Java");list.add("Python");list.add("C++");list.forEach(str -> System.out.println(str));上述代码使用lambda表达式对列表中的元素进行遍历,并输出每个元素的值。

二、常见应用场景1. 列表遍历如上述示例所示,使用lambda表达式可以极大地简化列表的遍历过程。

与传统的for循环相比,lambda表达式更加简洁易读。

2. 线程处理在多线程编程中,经常需要对线程进行处理。

使用lambda表达式可以更加便捷地定义线程的任务,如下所示:Thread t = new Thread(() -> System.out.println("This is a new thread"));t.start();3. 集合操作在对集合进行操作时,lambda表达式也能够提供更加便捷的方式。

Java语言程序设计基础篇英文版第六版教学设计

Java语言程序设计基础篇英文版第六版教学设计

Java Language Programming Basics Sixth EditionTeaching DesignIntroductionJava is a high-level object-oriented programming language that is widely used around the world. It is an important skill to understandJava programming in order to be a successful software developer. This document presents a teaching design approach for the Java Language Programming Basics Sixth Edition book in order to assist instructors in teaching Java concepts to their students.ObjectiveIn teaching this material, the goal is to have students attn a basic understanding of Java programming. By teaching the foundational concepts, students will develop the skills needed to create simple applications. They will learn to use variables, control structures, arrays, and loops. Additionally, we m to develop critical thinking and problem-solvingskills in students. These skills will be utilized throughout their software development careers.Target AudienceThis instructional guide is created primarily for students who are novices in the field of programming. The recommended target audience includes high school and undergraduate students who have no prior programming experience.Learning OutcomesThe following learning outcomes are expected for students who successfully complete this course:•Ability to write and read basic Java code•Fostering problem-solving abilities•Development of critical thinking•Understanding of fundamental concepts of object-oriented programmingLearning ResourcesThe teaching resources and materials for this course include: •Java Language Programming Basics Sixth Edition textbook•Oracle’s Java Development Kit (JDK) and Integrated Development Environment (IDE)•Online resources and tutorialsCourse Assessment and GradingThe course assessment and grading structure are as follows: •Class Participation: 20%•Assignments: 30%•Mid-term Exam: 25%•Final Exam: 25%Curriculum OutlineWeek One: Introduction to Java•Java Background•Java Programming Environment•The Structure of a Java Program Week Two: Basic Concepts•Identifiers and Keywords•Variables and Constants•Data TypesWeek Three: Operators and Expressions •Assignment operators•Arithmetic operators•Logical operatorsWeek Four: Control Structures•Decision Structures•Loop StructuresWeek Five: Arrays•Introduction to Arrays•Array Processing•Sorting ArraysWeek Six: Object-Oriented Programming •Classes and Objects•Encapsulation•InheritanceWeek Seven: Exception Handling•Fundamentals of Exception Handling•Determining Error Types•Exception Handling in A Java ProgramWeek Eight: Java Applets and Graphic User Interface•Basics of Java Applets•Building a Simple Java Applet•Developing a Graphical User Interface in Java ConclusionBy following the teaching design approach outlined in this document, instructors can help novice students grasp the basics of Java programming. With these foundational skills, students will be better positioned to advance their studies and seek a career in software development.。

java enhancer 高级用法

java enhancer 高级用法

java enhancer 高级用法Java 是一种流行的编程语言,其类库和框架提供了许多用于增强代码的功能。

在 Java 中,增强器(Enhancer)通常指的是用于修改类行为的工具或库。

以下是一些 Java 增强器的高级用法:1. Aspect Oriented Programming (AOP):AOP 是一种编程范式,它允许程序员定义横切关注点,这些关注点可以在多个类或方法上应用。

Spring Framework 中的 AOP 模块是一个流行的 Java 增强器,它可以用于实现日志记录、事务管理、安全等跨多个类和方法的横切关注点。

2. Bytecode Manipulation:Java 字节码是 Java 源代码编译后的二进制表示形式。

通过字节码操作库(如 ASM、CGLIB、ByteBuddy 等),开发人员可以修改已编译的字节码以增强类的行为。

这些库可以用于实现代理模式、动态生成子类、拦截方法调用等功能。

3. Java Instrumentation:Java Instrumentation API 允许开发人员修改运行中的 Java 应用程序的行为。

通过 Instrumentation API,开发人员可以创建自己的增强器,用于在应用程序运行时修改类的字节码。

这可以用于实现性能监控、调试、代码生成等功能。

4. Lambda Expressions:Java 8 引入了 Lambda 表达式,这是一种简洁的语法,用于表示匿名函数。

Lambda 表达式可以与 Java 的函数式接口一起使用,以实现高阶函数和函数式编程风格。

Lambda 表达式可以用于简化代码,提高可读性和可维护性。

5. Stream API:Java 8 中的 Stream API 是一个强大的工具,用于处理集合和数组。

通过Stream API,开发人员可以轻松地执行各种操作,如过滤、映射、排序和聚合等。

Stream API 可以帮助简化复杂的集合操作,并使代码更加简洁和易于理解。

《Java高级课件之Lambda表达式》

《Java高级课件之Lambda表达式》

思维转变
函数式编程的思维转变可以帮 助开发者更好地理解问题及封 装方法。
Lambda表达式与并发编程
1
并发编程与Lambda表达式
Lambda表达式支持函数式编程,而函数式编程非常适合并发编程。
2
函数式接口与线程池
可以使用函数式接口和Lambda表达式来方便地访问Java线程池中的任务。
3
死锁和数据竞争
Lambda表达式的返回值
Lambda表达式的返回值可以 是单个值或对象,也可以没 有返回值。
方法引用
方法引用是Lambda表达式的 一种简写方式,可以用来引 用已有的方法。
Lambda表达式的作用域与闭包
1
变量捕获
Lambda表达式可以访问同一个作用域中的变量,称为“捕获变量”。
2
代码块
在Lambda表达式中使用大括号包围代码块,可以创建一个新的代码块作用域。
箭头符号
箭头符号 -> 是Lambda表达式 的分隔符,将参数列表和 Lambda体分开。
Lambda体
Lambda表达式的Lambda体可 以是语句块,也可以是单个表 达式。
如何在Java中使用Lambda表达式
函数接口的定义
在Lambda表达式中,需要定义 一个只包含一个抽象方法的函数 接口。
Lambda表达式可以帮助简化并发编程中与死锁和数据竞争相关的代码。
使用Lambda表达式优化代码效率
简介性能优化
在代码中使用Lambda表达式 可以极大地降低括号的使用, 提高代码性能。
复用代码
使用Lambda表达式可以跨多 个不同的代码块复用代码, 减少冗余。
代码维护
使用Lambda表达式可以使代 码更为简洁,更易于开发人 员维护。

java2实用教程第六版文献引用格式

java2实用教程第六版文献引用格式

java2实用教程第六版文献引用格式以下是参考文献的格式和详细信息:
条目1:
姚翔,王爱民,吉洪波. Java 2实用教程(第6版)[M]. 北京: 机械工业出版社, 2012.
条目2:
YAO X, WANG A, JI H B. Java 2 Practical Tutorial (6th Edition)[M]. Beijing: China Machine Press, 2012.
条目3:
Yao, X., Wang, A., & Ji, H. B. (2012). Java 2 Practical Tutorial (6th Edition)[M]. Beijing, China: China Machine Press.
本书是一本面向初学者编写的Java编程教程,由姚翔、王爱民和吉洪
波共同撰写。

该教程已经出版到第六版,由北京机械工业出版社出版。

本书的第一部分介绍了Java基础知识,包括了数据类型、运算符、控
制语句、数组和对象等。

第二部分则讲述了Java中的面向对象编程,
其中包括了继承、多态、接口、包等内容。

同时本教程还介绍了Java
中的图形用户界面设计、异常处理、多线程编程和网络编程等方面,
这些都是Java编程中非常重要的知识点。

该教程的语言简练、深入浅出,非常适合初学者入门使用。

此外,该书还提供了大量的实例代码和练习题目,让读者能够更加深入地了解Java编程,并掌握相关技巧。

总的来说,《Java 2实用教程(第6版)》是一本非常优秀的Java编程教程,无论你是初学者还是有一定经验的开发人员,都可以从中受益。

java 匹配markdown语法正则

java 匹配markdown语法正则

标题:如何使用Java编写正则表达式匹配Markdown语法正文:随着Markdown语法在技术写作中的广泛应用,许多开发者希望使用Java编写正则表达式来匹配Markdown语法。

本文将介绍如何使用Java编写正则表达式来匹配Markdown语法,以及一些常见的匹配规则和技巧。

1. 了解Markdown语法在开始编写正则表达式之前,首先要了解Markdown语法的基本规则。

Markdown是一种轻量级标记语言,常用于编写文档、博客和笔记。

Markdown语法包括标题、段落、列表、信息、图片等元素,每个元素都有特定的格式和规则。

2. 使用Java编写正则表达式在Java中,可以使用java.util.regex包中的Pattern和Matcher类来编写和匹配正则表达式。

首先需要创建一个Pattern对象,并使用pile方法编译正则表达式。

然后创建一个Matcher对象,使用其matches、find、group等方法来匹配字符串。

3. 匹配Markdown标题Markdown标题使用井号(#)来表示不同级别的标题,可以使用正则表达式来匹配不同级别的标题。

可以使用正则表达式"^(#{1,6})\\s*(.+)$"来匹配一到六个井号开头的标题,然后使用Matcher的find和group方法来匹配标题内容。

4. 匹配Markdown段落Markdown段落通常是以空行分隔的文本块,可以使用正则表达式"^(.+)$"来匹配段落内容。

需要注意的是,正则表达式匹配的是一行文本,需要使用Pattern.MULTILINE参数来匹配多行文本。

5. 匹配Markdown列表Markdown列表可以使用星号(*)、加号(+)、或减号(-)来表示无序列表,或使用数字和点(.)来表示有序列表。

可以使用正则表达式"^(\\s{0,3})(\\*|\\+|\\-|\\d+\\.)\\s+(.+)$"来匹配无序和有序列表,然后使用Matcher的find和group方法来匹配列表内容。

java2实用教程第六版知识点汇总

java2实用教程第六版知识点汇总

Java2实用教程第六版知识点汇总1.引言本文档旨在对Ja va2实用教程第六版涉及的主要知识点进行全面的汇总和总结。

通过学习该教程,读者将能够全面掌握Ja va2编程的核心概念和技巧,为日后的J av a开发工作打下坚实的基础。

2.数据类型J a va2实用教程第六版详细介绍了Ja va中的各种数据类型及其使用方法。

以下是一些关键的知识点:2.1基本数据类型J a va的基本数据类型包括整型、浮点型、字符型和布尔型。

本教程提供了详细的介绍和示例代码,帮助读者理解这些数据类型的特点和用法。

2.2引用数据类型除了基本数据类型外,J av a还提供了多种引用数据类型,如数组、类、接口等。

教程中的例子演示了如何声明和使用这些引用数据类型,帮助读者熟悉它们的基本概念和操作。

3.控制流程控制流程是编程中的重要概念,决定了程序的执行顺序和逻辑。

J a va2实用教程第六版涵盖了常见的控制流程语句,包括条件语句和循环语句。

3.1条件语句条件语句用于根据条件的真假来选择性地执行不同的代码块。

本教程提供了i f语句、swi t ch语句等条件语句的详细说明和示例,让读者明白如何正确运用它们。

3.2循环语句循环语句用于重复执行某段代码,直到满足退出条件为止。

Ja v a2实用教程第六版介绍了三种循环语句:f or循环、w hi le循环和d o-wh il e循环。

读者将学会如何正确选择和使用不同类型的循环语句,以解决各种实际问题。

4.类与对象面向对象编程是J ava的核心思想之一。

J a va2实用教程第六版详细讲解了类与对象的概念、属性和方法的定义与使用等内容。

4.1类的定义与使用教程中提供了清晰的例子,介绍了如何定义类、声明对象、调用类的方法等操作。

读者将了解到如何通过类和对象来构建复杂的应用程序。

4.2构造方法与析构方法构造方法用于在创建对象时进行初始化操作,而析构方法则在对象销毁时执行清理工作。

本教程详细说明了构造方法和析构方法的特点和使用方法,帮助读者正确地管理对象的生命周期。

elasticsearch-java多条件查询lambda表达式

elasticsearch-java多条件查询lambda表达式

elasticsearch-java多条件查询lambda表达式1. 引言1.1 概述本文将介绍如何使用elasticsearch-java库进行多条件查询,其中重点关注lambda表达式在查询中的应用。

Elasticsearch是一个强大的开源搜索引擎,用于快速、可扩展和分布式的全文搜索解决方案。

而elasticsearch-java是Elasticsearch官方提供的Java客户端,可以方便地与Elasticsearch进行交互。

1.2 文章结构文章将分为5个部分,每个部分从不同的角度介绍多条件查询和lambda表达式在elasticsearch-java中的应用。

首先,在"引言"部分我们将对文章进行概述并描述本文的结构。

接下来,在第二部分"正文"中,我们会简要介绍Elasticsearch和elasticsearch-java库,并详细讨论多条件查询的概念和应用场景。

第三部分将重点关注lambda表达式在Java中的作用和使用方法。

我们将简单介绍lambda表达式,并探讨它在多条件查询中的优势以及具体应用方法。

通过示例代码解析和实践经验总结,读者将更好地理解lambda表达式在多条件查询中的实际应用。

第四部分将详细阐述多条件查询的实现步骤和示例代码。

我们会逐步指导读者如何配置Elasticsearch客户端连接并设置索引信息,以及如何构建查询请求体对象。

最后,我们还会展示如何执行多条件查询和解析结果集。

最后一部分是结论部分,在这里我们将总结lambda表达式在elasticsearch-java多条件查询中的应用优势,并对未来研究方向进行一些探讨。

1.3 目的本文的目的是帮助读者了解elasticsearch-java库的基本概念和使用方法,并重点介绍lambda表达式在多条件查询中的应用。

通过学习本文,读者将能够编写出更高效、简洁且易于维护的代码,从而提升其Java开发技能,并深入理解多条件查询与lambda表达式之间的关系与优劣。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

JavaML2.0:Enriching the Markup Language forJava Source CodeAdemar Aguiar1,Gabriel David1,and Greg Badros21Faculdade de Engenharia da Universidade do Porto and INESC Porto{aaguiar,gtd}@fe.up.pt2Google Inc.,2400Bayshore Parkway,Mountain View,CA94043badros@Abstract.Although the representation of source code in plain text formatis convenient for manipulation by programmers,it is not an effective formatfor processing by software engineering tools at an abstraction level suitable forsource code analysis,reverse-engineering,or refactoring.Textual source codefiles require language-specific parsing to uncover program structure,a task un-dertaken by all compilers but by only a few software engineering tools.JavaMLis an alternative and complementary XML representation of Java source codethat adds structural and semantic information into source code,and is easy tomanipulate,query,and transform using general purpose XML tools and tech-niques.This paper presents an evolved version of JavaML,dubbed JavaML2.0,and the enhancements made to the schema and respective converters:DTD andXML Schema support,cross-linking of all program symbols,and full preser-vation of original formatting and comments.The application of JavaML2.0is illustrated with concrete examples taken from the software documentationtool that motivated the enhancements.1IntroductionSince thefirst computer programming languages,programmers have used a plain-text format for encoding software structure and computation.The immediate users of this format include the compiler,which converts sequences of characters into a data structure that closely reflects the program structure—an abstract syntax tree(AST).Despite the advances in compilers and document management tools,software engi-neers manipulate source code using the plain text format,and often employ superficial tools based on regular expressions.Although the plain-text representation of source code is convenient for program-mers and has nice properties,pure text is not the most effective format for manip-ulating source code at an abstraction level suitable for software engineering tools. Plain-textfiles are good for lexical analysis,but they are not suitable for structural and semantic analysis before being parsed and translated to higher level formats. There is thus a need for a standard format capable of directly representing program structure and semantics,one readable and widely supported format that tools can easily analyze and manipulate.JavaML is a markup language for Java source code proposed by Greg Badros[1,2] that provides an alternative representation for Java source code.JavaML enriches source code textfiles with structural and semantic information typically present in a compiler annotated ASTs.Because the representation is XML-based,it results easy2Ademar Aguiar,Gabriel David,and Greg Badrosto manipulate,query,and transform using general purpose and widely available tools and techniques.This paper presents an evolved version of JavaML,dubbed2.0,that incorporates enhancements and new features to the original JavaML[1]:XML Schema support, updated converters,cross-referencing of all program symbols,and full preservation of original lexical information(formatting and comments).These enhancements were motivated by the implementation of XSDoc,an extensible infrastructure for docu-menting object-oriented frameworks[3].The next section briefly discusses the benefits of using an XML format for repre-senting source code.The sections that follow describe JavaML2.0and present concrete examples of application taken from the XSDoc infrastructure.The paper then reviews related work and concludes pointing ideas for future work.2Why represent source code in XML?The plain-text representation of source code is convenient to express programmers ideas.It is concise,easy to read by programmers,and very simple to exchange and manipulate using a wide variety of tools,such as text editors,version control systems, andfile system utilities.But the plain-text representation of source code also has many limitations.Perhaps the most significant is that the program structure is not directly represented in the format and thus require specific language processing to uncover it.2.1XMLXML is an universal format widely used to represent structured information in text-basedfiles that was designed to be lightweight and simple.An XML document consists of text marked up with tags enclosed in angle braces.XML documents have an inherent hierarchical structure and are therefore very convenient for representing source code constructs.XML-based representations have many qualities:easy to understand,easy to manipulate by tools,veryflexible,ex-tensible,and widely supported.Although XML documents are primarily intended for automatic processing by tools,the format is human readable and understandable.XML documents are therefore an empowering complementary representation for source code,more abstract,which enables the usage of tools at an higher level of abstraction.2.2Limitations of plain text for representing source codeDespite its nice properties,plain text source codefiles require parsing to uncover the most important information they contain about a program:the structural and semantic information.This severe limitation forces the inclusion of language-specific parsers in each tool that needs to manipulate programs at an abstraction level higher than the lexical.While sophisticated software engineering tools can afford to embed such parsers,unfortunately,the majority of utility tools cannot and are thus limited to simple tasks.Common manipulations of source code,such as generation,refactoring,reformat-ting,or reverse-engineering,usually require the manipulation of more abstract source code representations equivalent to ASTs.JavaML2.0—XATA’20043 Although modern integrated development environments(IDE)provide applica-tion programming interfaces(API)to manipulate in-memory AST representations of source code,these APIs are still not appropriate for simple tools,since they re-quire integration in a complex environment—the IDE—that creates a strong and undesirable dependency.2.3Benefits of representing source code in XMLThe representation of source code in XML has several benefits[4,5]and enables the use of more powerful software engineering and document management methods and tools.Explicit code structure.XML documents are structured by nature,and can be used to build tree-like code representations and sophisticated manipulation of source code using general purpose XML tools.Some good examples are automatic synchro-nization of generated code and documentation,or quick code generation and trans-formation using predefined templates.With source code in XML it is possible to annotate generated code with its template definition so that the constructed code can be regenerated every time its template is plex templates for custom instantiation of design patterns would benefit from such mechanisms.Powerful querying capabilities.In addition to textual searches using regular ex-pressions,modern IDEs usually include also tools specific for source code that allow searching for common programming language constructs,such as classes,methods,fields,etc.These features are useful but are only a small subset of what is possible to query with XML standards and tools,such as XPath[6]and XQuery[7]. Extensible representation.Plain-text source code is not easy to extend with new code constructs or annotations because they would disrupt the code structure and require parser modifications.Because of this,such extensions are often embedded as comments.In a XML document,the addition of new elements is much easier,because the structure is explicitly marked up,and we can separate different kinds of elements (nguage specific elements and user-defined elements)using XML namespaces. Distinct tools can define and insert their own elements in the code structure and then process only those that are relevant for them.Examples of common extensions are code annotations,comments,meta-information,authoring and version information, revisions,documentation and conditional code.Flexible formatting.Most programming languages,including Java,ignore the se-mantic value of whitespace and enable programmers to adopt diversified formatting conventions,which,despite its usefulness,are not easy to enforce and maintain in consistency.Mistakes in following formatting conventions sometimes result in an in-creased difficulty to locate structural or semantic errors,due to the suggestive nature of formatting.In an XML representation of code,the structure can be abstracted from the coding style.XML standards and tools,such as XSLT[8]facilitate the (re)formatting of source code using different styles which can enrich its readability through an appropriate usage of layouts,colors,fonts and links.4Ademar Aguiar,Gabriel David,and Greg BadrosCross-referencing.Source code fragments in plain text are usually referenced by theirfile position,i.e.line and column numbers.On an XML tree-like structure of a program,it is possible to reference code fragments directly to code constructs,thus enabling the relocation of code fragments without disrupting references.Wide support.A program representation must be widely supported in a wide variety of platforms,otherwise it can’t succeed.XML tools are available in all major platforms and thus completely satisfy this requirement.3JavaML2.0The JavaML markup language provides a complete self-describing representation in XML for Java source code.Unlike the classical plain-text representation of programs, JavaML reflects the structure of Java programs directly in the hierarchical structure of XML documents.Because JavaML uses the XML format,which is a text-based representation, many of the advantages of the classical source representation remain.In addition, the JavaML representation is easy to parse and manipulate with general purpose XML tools,not requiring language-specific tools difficult to implement and maintain. Therefore,JavaML leverages the development of XML tools for the manipulation of Java source code in JavaML.Although the immediate users of JavaML format are tools,not being intended to be written directly by hand,the format is easily readable and understandable, enabling its direct inspection by developers.JavaML2.0enriches the original JavaML[1]with more information at several lev-els of abstracting ranging from the lexical level to the semantic level.The enhanced representation of JavaML2.0now includes full lexical information about tokens,com-ments and formatting,only small enhancements in terms of structural information, and much richer semantic information related with symbol definitions,references and type information.3.1Background on JavaMLIn order to represent Java source code in XML there are many possible approaches [1].Consider the following sourcefile for the class FirstTest.1package junit.samples;2import junit.framework.∗;34/∗∗5∗My first unit test.6∗/7public class FirstTest extends TestCase{8double value= 2.0;910public void testAdd(){11double result=value+3;12//forced failure result==513assertTrue(result==6);14}15}JavaML2.0—XATA’20045 The most obvious approach is to dump the derived AST to a XML format,butthis would result very verbose and uninteresting due to the irrelevant grammar detailsit would reveal.Another possibility is to markup the Java source program without changing the original text.The result would be a richer representation,easier to convert back to the original sourcefile,but the retrieval of specific information from the representation would require undesired lexical analysis of element contents.The representation chosen for JavaML aimed to model Java programming language constructs without binding to the specificities of the language syntax[1].As a resultof this design principle,JavaML can be used as a base for the design of a generalized markup language supporting other object-oriented programming languages,such asC#,C++or Smalltalk.To illustrate the approach followed by JavaML,consider the sourcefile FirstTest.java presented above and its corresponding basic JavaML representation(FirstTest.java.xml). The major design decisions are enumerated below.3<java−class−file name="f:/junit3.8.1/src/junit/samples/FirstTest.java">4<package−decl name="junit.samples"/>5<import module="junit.framework.∗"/>6<class name="FirstTest"id="Ljunit/samples/FirstTest;">7<doc−comment>/∗∗&#xA;∗My first unit test.&#xA;∗/</doc−comment>8<modifiers>9<modifier name="public"/>10</modifiers>11<superclass name="TestCase"idref="Ljunit/framework/TestCase;"/>12<field name="value"id="Ljunit/samples/FirstTest;value">13<type name="double"primitive="true"/>14<var−initializer>15<literal−number kind="double"value="2.0"/>16</var−initializer>17</field>18<method name="testAdd"id="Ljunit/samples/FirstTest;testAdd()V">19<modifiers>20<modifier name="public"/>21</modifiers>22<type name="void"primitive="true"/>23<formal−arguments/>24<block>25<local−variable−decl>26<type name="double"primitive="true"/>27<local−variable name="result"id="Ljunit/samples/FirstTest;var1946">28<var−initializer>29<binary−expr op="+">30<field−ref name="value"idref="Ljunit/samples/FirstTest;value"/>31<literal−number kind="integer"value="3"/>32</binary−expr>33</var−initializer>34</local−variable>35</local−variable−decl>36<send message="assertTrue"idref="Ljunit/framework/Assert;assertTrue(Z)V">37<arguments>38<binary−expr op="==">39<var−ref name="result"idref="Ljunit/samples/FirstTest;var1946"/>40<literal−number kind="integer"value="6"/>41</binary−expr>42</arguments>43</send>44</block>45</method>46</class>47</java−class−file>6Ademar Aguiar,Gabriel David,and Greg Badros Representation of code constructs.JavaML represents the important concepts of the Java language,namely classes,superclasses,fields,methods,variables,message sends,and literals,directly in document elements and attributes.Code structure is reflected in the nesting of elements.Program structure is reflected in the nesting of elements.As an example,it is presented below a visual presentation of the document tree,which shows the nesting of the literal number3in the initializer part of the variable declaration it appears.Fig.1.Tree view of the JavaML representation[?].Generic elements.In order to reduce the size and complexity of the schema, JavaML generalizes related concepts and represents them using generic elements with different attribute values.Loops and literal numbers are just two examples of such generalizations.In FirstTest.java.xml,the lines15and31contain literal-number elements with different kind attribute values to disambiguate the representation of a double and an integer.In addition,unstructured information is stored in attribute elements as illustrated in the modifier element,in line20.3.2The new JavaML2.0schemaThe major enhancements of JavaML2.0consisted on improving the schema to accom-modate richer lexical and semantic information.The goal is to enable full preservation of original sourcefiles and complete cross-linking of program symbols.The correspond-ing converters were re-implemented to cope with these new schema requirements.These enhancements were mainly motivated by the implementation of XSDoc, an infrastructure for framework documentation that uses JavaML to dynamically integrate source code in the documentation.All JavaML and JavaML2.0artifacts referred in this paper are available online in [2,9].JavaML2.0—XATA’20047 DTD and XML Schema support.Both XML Schema and DTD provide a basic grammar for defining XML documents in terms of the metadata that comprise the shape of the document.XML Schemas are themselves XML documents.XML Schemas provide a more powerful means to define a XML document structure and validations than DTDs,because provide an object-oriented approach,with all the benefits this entails,namely the ability to reuse and extend type definitions.Because both DTD and XML Schema have their own advantages,JavaML2.0is primarily designed for XML Schema but still support DTDs.The JavaML2.0XML Schema has around90 elements.The original DTD was four times shorter in terms of number of lines.Cross-referencing of program symbols.JavaML representation includes infor-mation to link symbol references to their definitions.This linking is achieved through the standard XML mechanism using id attributes in definitions and idref attributes in references.Although not strictly necessary,JavaML considers variables,fields,and arguments as distinct kinds of symbols.In line39,we can see a reference to the local variable named result that points back to its definition in line27.27<local−variable name="result"id="Ljunit/samples/FirstTest;var1946">39<var−ref name="result"idref="Ljunit/samples/FirstTest;var1946"/> Because these references are defined in the XML Schema,they are automatically checked when the document is validated.The listing below shows the lines that define the keys and references for local-variable elements in the JavaML2.0schema (java-ml.xsd).The key is defined as a value appearing in the id attribute(line 213)of local-variable elements or formal-argument elements that are immediate children of catch elements(line212).This key is referenced by values of the idref attribute(line217)of var-ref elements(line216).The values used in these keys and references are typical to symbol tables and are automatically generated by the Java compiler.211<xs:key name="KeyLocalVariable">212<xs:selector xpath=".//local−variable|.//catch/formal−argument"/>213<xs:field xpath="@id"/>214</xs:key>215<xs:keyref name="RefLocalVariable"refer="KeyLocalVariable">216<xs:selector xpath=".//var−ref"/>217<xs:field xpath="@idref"/>218</xs:keyref>Type dependencies.Source codefiles usually have dependencies to other source files and libraries.During type checking and name resolution,all thesefiles must be analyzed and a type dependency graph can be built.JavaML2.0representation in-cludes these dependencies to complement the structural information presented before. 48<type−dependences>49<type−dependence filename="f:/junit3.8.1/src/junit/samples/FirstTest.java"50signature="Ljunit/samples/FirstTest;">51<type−ref signature="Ljunit/framework/TestCase;"/>52</type−dependence>53</type−dependences>This information enables following references to source code and documentation of external types.In line6of FirstTest.java.xml,we can see the value used to uniquely identify the class FirstTest and in line11there is a reference to the external type TestCase.8Ademar Aguiar,Gabriel David,and Greg Badros6<class name="FirstTest"id="Ljunit/samples/FirstTest;">11<superclass name="TestCase"idref="Ljunit/framework/TestCase;"/>Preservation of formatting and comments.The preservation of comments and whitespace were two issues not completely solved in the original JavaML implemen-tation[1].Although the comments are easy to store,they are challenging to attach to the correct elements.JavaML2.0preserves all comments present in sourcefiles and attach the formal ones,i.e.Javadoc comments[10],to their respective code elements(class, method,field,etc.)using the rules defined by Javadoc.To enable the exact regeneration of original sourcefiles,the JavaML2.0has new codeline,token,and comment elements to store all the lexical information of a source codefile.Below,it is presented the JavaML2.0lexical representation of line1 of FirstTest.java.54<java−source−code>55<codeline no="1">56<token idx="1"line="1"column="1"type="preprocessor"lexeme="package"57afterEol="true"/>58<sp/>59<token idx="2"line="1"column="9"type="normal"lexeme="junit"/>60<token idx="3"line="1"column="14"type="normal"lexeme="."/>61<token idx="4"line="1"column="15"type="normal"lexeme="samples"/>62<token idx="5"line="1"column="22"type="normal"lexeme=";"/>63</codeline>Due to the verbosity of this information,it is optionally generated.In the following listing is shown how Javadoc comments are represented.75<codeline no="4">76<comment idx="1"line="4"column="1"type="formal">/∗∗</comment>77</codeline>78<codeline no="5">79<comment idx="1"continued="true">∗My first unit test.</comment>80</codeline>81<codeline no="6">82<comment idx="1"continued="true">∗/</comment>83</codeline>3.3Java to JavaML converterBecause JavaML was designed to be primarily written by tools,it is mandatory to have one converter from Java sourcefiles to JavaML.The approach followed is presented in[1]and consisted on adding one XMLUnparse method for each AST node of the IBM Jikes Java compiler framework(version1.12).The result is a fast and robust JavaML converter[11].To implement the schema enhancements of JavaML2.0,the converter was initially migrated to Jikes1.18and then evolved tofit the new requirements.The generation of lexical information is implemented by embedding Jikes scanner results in JavaML elements.JavaML elements are generated by visiting the AST and its associated symbol and type annotations.The generation of semantic information to assign to the id and idref attributes of each program symbol is definitely the most trickier part to implement because it requires navigation in the AST and lookups in the symbol table and type definitions.The overall code that adds JavaML support to Jikes rounds2000lines of C++.JavaML2.0—XATA’200493.4JavaML convertersOnce generated,the JavaML representation can be easily processed using general purpose XML ing an XSLT stylesheet it is possible to convert the JavaML representation to several other formats.To exemplify this,below are presented two converters:one for producing an HTML view of the source code and another for regenerating the original Java sourcefile.JavaML to HTML.The JavaML tools include an XSLT stylesheet that converts JavaML to HTML,named(javaml-to-html.xsl).Because JavaML2.0contains more lexical information than the original JavaML,the new stylesheet resulted sim-pler than the original[1].It produces HTML that cross-links all symbol references (types,methods,variables,etc.)to their definitions in source code or documentation, depending on what is available.When compared to the original source code,the gen-erated HTML view results more convenient for program understanding,because it enables good navigation from references both to internal and external definitions.The conversion consists basically on two tasks:first,on applying a predefined style to each token,based on the kind it was assigned during scanning(literal,keyword, etc.);and,second,on defining anchor elements(<a name=>)and reference elements ((<a href=>)for each symbol definition and symbol referenced,respectively.The most important part of the linking is the conversion of id and idref values to anchor names and references,which is done with the help of a Java function that receives the symbol information and computes the link.The new stylesheet(javaml-to-html.xsl)has 21template rules and around300lines.Below is listed the line of the template rule create-link that creates and formats the anchor name element.217<xsl:value−of select="linker:getLinkFromId($javadoc−url,$filename,218$type−signature,$kind,key(’KeyTypeSignature’,$signature)/@filename,$id)"/> JavaML to Java.The regeneration of Java source code is straightforward to imple-ment because the JavaML representation contains low-level lexical information about the tokens,comments and spaces of the original sourcefile.The corresponding XSLT stylesheet has only30lines and only6simple template rules.The most complex template rule is for processing token elements.14<xsl:template match="token">15<xsl:if test="not(@type=’TK EOF’)">16<xsl:value−of select="@lexeme"disable−output−escaping="yes"/>17</xsl:if>18</xsl:template>4Applying JavaMLJavaML2.0is the result of evolving original JavaML[1]tofit the requirements of XSDoc[3],an extensible infrastructure based on a WikiWikiWeb engine that supports the creation,integration,publishing and presentation of documentation for object-oriented frameworks.XSDoc helps on creating and annotating framework documents, on integrating different kinds of contents(text,models and source code),and providesa simple and economic cooperative web-based documentation environment that can beused standalone in a web-browser,or inside an integrated development environment.10Ademar Aguiar,Gabriel David,and Greg Badros To illustrate the application of JavaML2.0,it will be presented a concrete exam-ple taken from a simple usage of XSDoc for integrating in a web document source code from thefile FirstTest.java and some text.XSDoc provides two dynamic mechanisms for the integration and synchronization of the possible kinds of docu-ment contents(source code,UML diagrams and XMLfiles):inlining of contents,and automatic linking.The inlining of Java source code is defined with a reference to the specific contents, annotated with the<javaSource>tags.See below the method for testing the addition:[<javaSource>]junit.samples.FirstTest#testAdd();comments=no;lines=first,last;[</javaSource>]For example,the text above extracts the code fragment corresponding to the method testAdd()of class junit.samples.FirstTest,then removes all its com-ments,and returns itsfirst and last line.This would produce the web page presentedin Figure2.Fig.2.Example of a XSDoc page inlining code from FirstTest.java.4.1Generating JavaMLAfter parsing the source code reference(in Javadoc format)contained in the<javaSource> tag,XSDocfinds that the code to inline must be in thefile FirstTest.java.If the JavaML representation is outdated,it is updated by invoking the Jikes compiler withthe appropriate arguments.$jikes+B+L+c+T=3+ulx FirstTest.java4.2Filtering JavaMLThe JavaML document is thenfiltered to get the requested method.Based on the source code reference,XSDoc builds a XPath query and applies it using Saxon[12].∗[(name()=’class’or name()=’interface’)and@name=’FirstTest’]/method[@name=’testAdd’and./formal−arguments/descendant−or−self::∗[last()=1]]After this structuralfiltering,the document is lexicallyfiltered to remove the comments,and then,thefirst and last line are extracted.。

相关文档
最新文档