DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Java
  4. Meeting Plastic I: Introduction

Meeting Plastic I: Introduction

Taha Siddiqi user avatar by
Taha Siddiqi
·
May. 30, 11 · Interview
Like (1)
Save
Tweet
Share
4.07K Views

Join the DZone community and get the full member experience.

Join For Free

With Tapestry, you know things improve very fast. For the users, everything stays much the same(Ok, only after Tapestry5) but for developers, it keeps you on your toes especially if you are constantly peeking into the source code. In Tapestry 5.3, we are going to see complete replacement of Javassist by Plastic. It is a wrapper around ASM.

Example

Let us first start with the simplest of example. Say, we want to add a toString() method to all the classes in the controlled package. A controlled package is one which contains classes that are to be transformed. In order to perform a transformation, we implement PlasticClassTransformer interface.

/**
* A simple class transformer which adds a toString method to the
* classes to be transformed
*/
public class ToStringTransformer implements PlasticClassTransformer {

/**
* Adds a toString() method to the class
*/
public void transform(PlasticClass plasticClass) {
plasticClass.addToString("Modified by <ToStringTransformer>");
}

}

In the transform method, we use addToString method to add a toString() method to the class.

I have written a small spock test to use this transformer.

 

/**
* A simple test for {@link plasticdemo.transforms.ToStringTransformer}
*/
class ToStringTest extends Specification {
def pm

def setup(){
//Create a plastic manager and pass on the controlled package and the transformer
pm = PlasticManager.withContextClassLoader().packages(["plasticdemo.controlled"]).
delegate(new StandardDelegate(new ToStringTransformer())).create()
}

def "test if a class in controlled package has our toString method"(){
def foo = pm.getClassInstantiator("plasticdemo.controlled.Foo").newInstance()
expect: foo.toString().equals("Modified by <ToStringTransformer>")
}
}

The steps involved are
Step #1: Create and configure a PlasticManagerBuilder
PlasticManagerBuilder can be created using either withContextClassLoader() or withClassLoader(ClassLoader) method. In the latter case, class loader can be passed as argument. Once the builder is created, it can be passed our controlled package name and class transformer.

Step #2: Create a PlasticManager
An instance of PlasticManager can be obtained by calling PlasticManagerBuilder.create().

Step #3: Obtain a ClassInstantiator and create instance
To create an instance of the transformed class, we first get ClassInstantiator by calling PlasticManager.getClassInstantiator() and then call its newInstance() method

You can find the source here

 

From http://tawus.wordpress.com/2011/04/18/meeting-plastic/

Javassist Spock (testing framework) dev Testing Loader (equipment) Interface (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Easy Smart Contract Debugging With Truffle’s Console.log
  • Deploying Java Serverless Functions as AWS Lambda
  • Top Authentication Trends to Watch Out for in 2023
  • PHP vs React

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: