Recent Changes Printable View Page History Edit Page

Java

Discover the current call context with SecurityManager.getClassContext()

summary
The SecurityManager.getClassContext() method "returns the current execution stack as an array of classes". This is useful if you need to figure out who called your code.
real-world scenario
This is a technique used by the Eclipse OSGi framework implementation to find a classloader to load a class that is visible to the caller's class loader but not to the callee's class loader.
how-to
This method is a protected method, so you have to declare a subclass of it to make it visible. For instance:

  
class ContextInspector extends SecurityManager {
  public Class[] getClassContext() {
    return super.getClassContext();
  }
}

The returned array has all the classes involved in the current context call stack, from top to bottom.

example
Follows a working example of the above:

  
import java.util.Arrays;

public class Test {
	static class ContextInspector extends SecurityManager {
		public Class[] getClassContext() {
			return super.getClassContext();
		}
	}
	public static void main(String[] args) {
		final ContextInspector contextInspector = new ContextInspector();
		// shows: [class Test$ContextInspector, class Test]
		System.out.println(Arrays.asList(contextInspector.getClassContext()));
		// shows: [class Test$ContextInspector, class Test$1, class java.lang.Thread]
		new Thread(new Runnable() {
			public void run() {
				System.out.println(Arrays.asList(contextInspector.getClassContext()));
			}
		}).start();
	}
}

Eclipse

Avoid auto-activation if you don't need it

One of the main characteristics of Eclipse is automatic activation during the classloading of the first class from a plug-in. During activation plug-ins usually perform initialization of some essential state the plug-in code (and any clients) can rely on. Hence, activation can be expensive. Some times it is even undesired. For instance, the plug-in provides some class that is instantiated by other plug-ins but this class does not require the plug-in to be initialized.

Since its 3.0 version, Eclipse provides selective auto-activation through the Eclipse-AutoStart manifest header. A plug-in may choose to exclude some classes from the auto-activation mechanism, or even disable auto-activation altogether.

Check Eclipse 3.1 documentation on the Eclipse-specific headers in the MANIFEST.MF file to learn how to use the Eclipse-AutoStart header.

Linux/Bash

Finding all files with duplicate names

  
find . -name *.java -exec basename {} \; | sort | uniq -d | xargs -L 1 find . -name 

Groovy

Script to rank ASP surfers for 8 best results

  
def int cutoff = 8	
def pointsPerPlace = [1: 1200, 2: 1032, 3: 876, 5: 732, 9: 600, 17: 410, 33: 225]
// data copied from aspworldtour.com, pasted into OpenOffice calc and then here
def String ranking = 	
"""	Mick Fanning (AUS)	 3	 5	 9	 5	 17	 1	 1	 9	 1
Joel Parkinson (AUS)	 1	 1	 9	 3	 1	 17	 17	 17	 3
Adriano de Souza (BRA)	 2	 17	 5	 2	 17	 5	 33	 1	 9
Bede Durbidge (AUS)	 5	 17	 17	 5	 9	 3	 2	 5	 2
C.J. Hobgood (USA)	 5	 5	 5	 3	 9	 9	 9	 3	 9
Taj Burrow (AUS)	 3	 17	 2	 5	 17	 33	 5	 5	 9
Kelly Slater (USA)	 17	 17	 17	 1	 9	 3	 5	 3	 17
Bobby Martinez (USA)	 33	 9	 1	 9	 5	 33	 5	 9	 5
Damien Hobgood (USA)	 5	 17	 9	 9	 2	 9	 17	 17	 5
Jordy Smith (ZAF)	 9	 3	 9	 17	 17	 33	 9	 9	 5
Taylor Knox (USA)	 17	 9	 5	 17	 5	 5	 9	 9	 17
Dane Reynolds (USA)	 9	 33	 33	 33	 3	 2	 9	 17	 9
Tom Whitaker (AUS)	 9	 9	 5	 9	 17	 33	 17	 9	 9
Kieren Perrow (AUS)	 17	 5	 9	 33	 9	 9	 9	 9	 17
Fredrick Patacchia (HAW)	 9	 3	 17	 33	 17	 17	 17	 5	 9
Kai Otton (AUS)	 33	 9	 17	 33	 3	 9	 17	 9	 9
Dean Morrison (AUS)	 33	 9	 17	 17	 5	 17	 9	 17	 17
Kekoa Bacalso (HAW)	 17	 5	 33	 9	 17	 9	 33	 9	 33
Mick Campbell (AUS)	 33	 17	 3	 9	 9	 inj	 17	 17	 33
Chris Davidson (AUS)	 9	 33	 33	 17	 17	 17	 17	 2	 33
Michel Bourez (PYF)	 33	 17	 33	 17	 9	 5	 9	 17	 33
Ben Dunn (AUS)	 33	 33	 17	 17	 9	 17	 5	 17	 17
Tiago Pires (PRT)	 17	 17	 33	 17	 33	 17	 3	 17	 33
Jeremy Flores (FRA)	 9	 17	 17	 9	 17	 17	 33	 inj	 inj
Drew Courtney (AUS)	 17	 9	 33	 33	 33	 33	 33	 5	 9
Tim Reyes (USA)	 33	 17	 17	 9	 17	 33	 33	 33	 5
Adrian Buchan (AUS)	 5	 17	 17	 inj	 inj	 17	 33	 17	 17
Heitor Alves (BRA)	 17	 33	 17	 17	 33	 5	 17	 33	 17
Roy Powers (HAW)	 17	 33	 33	 17	 17	 9	 17	 33	 17
Tim Boal (FRA)	 17	 17	 17	 17	 33	 17	 9	 33	 33
Greg Emslie (ZAF)	 17	 33	 33	 17	 17	 17	 17	 17	 17
Josh Kerr (AUS)	 17	 33	 9	 17	 33	 9	 inj	 33	 33
Jay Thompson (AUS)	 17	 9	 9	 33	 33	 17	 33	 33	 33
Nic Muscroft (AUS)	 17	 33	 33	 33	 17	 17	 17	 17	 17
Dustin Barca (HAW)	 17	 33	 17	 5	 33	 33	 17	 33	 33
Aritz Aranburu (EUK)	 33	 33	 3	 33	 33	 33	 17	 17	 33
Chris Ward (USA)	 33	 17	 33	 9	 17	 33	 33	 17	 33
Jihad Khodr (BRA)	 9	 17	 33	 33	 33	 33	 33	 17	 17
Michael Picon (FRA)	 33	 33	 33	 17	 17	 17	 33	 17	 17
Nathaniel Curran (USA)	 33	 33	 33	 17	 9	 33	 33	 33	 17
Dayyan Neve (AUS)	 17	 17	 17	 17	 33	 33	 33	 33	 33
David Weare (ZAF)	 17	 17	 33	 33	 33	 17	 33	 17	 33
Phillip MacDonald (AUS)	 33	 33	 17	 33	 33	 33	 17	 33	 33
Marlon Lipke (DEU)	 33	 33	 17	 33	 33	 33	 33	 33	 17
Luke Stedman (AUS)	 inj	 inj	 inj	 inj	 inj	 17	 inj	 inj	 inj
Gabe Kling (USA)	 33	 inj	 inj	 inj	 inj	 inj	 inj	 inj	 inj"""

def data = ranking.split('\n').collect{line ->
	def String name,resultsString
	(name,resultsString) = line.split('\\(.*\\)')
	def results = resultsString.replaceAll('inj','33').split('\\D+').findAll{!it.empty}
	def resultsAsNumbers = (results.collect { it.trim().toInteger() })
	return [name.trim()] + resultsAsNumbers
}
def surferPoints = data.collect {List results ->
	def surferName = results[0]
	def sortedResults = results.subList (1, results.size()).sort()
	def bestResults = sortedResults.subList (0, cutoff)
	def bestPoints = bestResults.inject(0) {sum, place -> sum + pointsPerPlace[place]}
	def totalPoints = sortedResults.inject(0) {sum, place -> sum + pointsPerPlace[place]}
	return [surfer: surferName, bestPoints: bestPoints, totalPoints: totalPoints, bestResults: bestResults]	
}.sort({-it.bestPoints})
surferPoints.eachWithIndex {sp,i -> println "#${i+1} - ${sp.surfer} - ${sp.bestPoints}/${sp.totalPoints} - results: ${sp.bestResults}" }


Edit Page - Page History - Printable View - Recent Changes - SearchWiki
Page last modified on November 29, 2009, at 03:24 AM