changeset 778:305ffb00ebc1

improve synchronization in JavaLuan
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 28 Aug 2016 03:26:39 -0600
parents 1460d297e960
children c38f6619feb9
files src/luan/modules/JavaLuan.java
diffstat 1 files changed, 53 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/src/luan/modules/JavaLuan.java	Fri Aug 26 15:42:15 2016 -0600
+++ b/src/luan/modules/JavaLuan.java	Sun Aug 28 03:26:39 2016 -0600
@@ -206,67 +206,70 @@
 	public static boolean privateAccess = false;
 	private static Map<Class,Map<String,List<Member>>> memberMap = new HashMap<Class,Map<String,List<Member>>>();
 
-	private static synchronized List<Member> getMembers(Class cls,String name) {
-		Map<String,List<Member>> clsMap = memberMap.get(cls);
-		if( clsMap == null ) {
-			clsMap = new HashMap<String,List<Member>>();
-			for( Class c : cls.getClasses() ) {
-				String s = c.getSimpleName();
-				List<Member> list = new ArrayList<Member>();
-				clsMap.put(s,list);
-				list.add(new Static(c));
-			}
-			for( Field field : cls.getFields() ) {
-				String s = field.getName();
-				try {
-					if( !cls.getField(s).equals(field) )
-						continue;  // not accessible
-				} catch(NoSuchFieldException e) {
-					throw new RuntimeException(e);
+	private static List<Member> getMembers(Class cls,String name) {
+		Map<String,List<Member>> clsMap;
+		synchronized(memberMap) {
+			clsMap = memberMap.get(cls);
+			if( clsMap == null ) {
+				clsMap = new HashMap<String,List<Member>>();
+				for( Class c : cls.getClasses() ) {
+					String s = c.getSimpleName();
+					List<Member> list = new ArrayList<Member>();
+					clsMap.put(s,list);
+					list.add(new Static(c));
 				}
-				List<Member> list = new ArrayList<Member>();
-				clsMap.put(s,list);
-				list.add(field);
-			}
-			for( Method method : cls.getMethods() ) {
-				String s = method.getName();
-				List<Member> list = clsMap.get(s);
-				if( list == null || !(list.get(0) instanceof Method) ) {
-					list = new ArrayList<Member>();
+				for( Field field : cls.getFields() ) {
+					String s = field.getName();
+					try {
+						if( !cls.getField(s).equals(field) )
+							continue;  // not accessible
+					} catch(NoSuchFieldException e) {
+						throw new RuntimeException(e);
+					}
+					List<Member> list = new ArrayList<Member>();
 					clsMap.put(s,list);
+					list.add(field);
 				}
-				list.add(method);
-			}
-			if( privateAccess ) {
-				for( Method method : cls.getDeclaredMethods() ) {
+				for( Method method : cls.getMethods() ) {
 					String s = method.getName();
 					List<Member> list = clsMap.get(s);
-					if( list == null ) {
+					if( list == null || !(list.get(0) instanceof Method) ) {
 						list = new ArrayList<Member>();
 						clsMap.put(s,list);
-					} else if( !(list.get(0) instanceof Method) )
-						continue;
-					if( !list.contains(method) ) {
-						list.add(method);
+					}
+					list.add(method);
+				}
+				if( privateAccess ) {
+					for( Method method : cls.getDeclaredMethods() ) {
+						String s = method.getName();
+						List<Member> list = clsMap.get(s);
+						if( list == null ) {
+							list = new ArrayList<Member>();
+							clsMap.put(s,list);
+						} else if( !(list.get(0) instanceof Method) )
+							continue;
+						if( !list.contains(method) ) {
+							list.add(method);
+						}
+					}
+					for( Field field : cls.getDeclaredFields() ) {
+						String s = field.getName();
+						List<Member> list = clsMap.get(s);
+						if( list == null ) {
+							list = new ArrayList<Member>();
+							clsMap.put(s,list);
+							list.add(field);
+						}
 					}
 				}
-				for( Field field : cls.getDeclaredFields() ) {
-					String s = field.getName();
-					List<Member> list = clsMap.get(s);
-					if( list == null ) {
-						list = new ArrayList<Member>();
-						clsMap.put(s,list);
-						list.add(field);
+				for( List<Member> members : clsMap.values() ) {
+					for( Member m : members ) {
+						if( m instanceof AccessibleObject )
+							((AccessibleObject)m).setAccessible(true);
 					}
 				}
+				memberMap.put(cls,clsMap);
 			}
-			for( List<Member> members : clsMap.values() ) {
-				for( Member m : members ) {
-					if( m instanceof AccessibleObject )
-						((AccessibleObject)m).setAccessible(true);
-				}
-			}
-			memberMap.put(cls,clsMap);
 		}
 		List<Member> rtn = clsMap.get(name);
 		if( rtn==null )
@@ -274,7 +277,7 @@
 		return rtn;
 	}
 
-	private static synchronized List<Member> getStaticMembers(Class cls,String name) {
+	private static List<Member> getStaticMembers(Class cls,String name) {
 		List<Member> staticMembers = new ArrayList<Member>();
 		for( Member m : getMembers(cls,name) ) {
 			if( Modifier.isStatic(m.getModifiers()) )