Commit a43ac0fd authored by Lionel Seinturier's avatar Lionel Seinturier
Browse files

Factorize helper methods.

parent 41fa8b61
......@@ -55,6 +55,8 @@ import org.objectweb.fractal.juliac.Juliac;
import org.objectweb.fractal.juliac.api.TypeGeneratorItf;
import org.objectweb.fractal.juliac.conf.Constants;
import org.objectweb.fractal.juliac.conf.JuliacConfig;
import org.objectweb.fractal.juliac.helper.ClassHelper;
import org.objectweb.fractal.juliac.helper.MemberHelper;
import org.objectweb.fractal.juliac.ipf.AnnotationHelper;
import org.objectweb.fractal.juliac.ipf.CompositeInjectionPointHashMap;
import org.objectweb.fractal.juliac.ipf.InjectionPoint;
......@@ -214,7 +216,7 @@ extends ClassGenerator {
for (Map.Entry<String,Class<?>> entry : props.entrySet()) {
String name = entry.getKey();
Class<?> type = entry.getValue();
String tname = getName(type);
String tname = ClassHelper.getName(type);
mv.visitIns("props.put(\""+name+"\",",tname,".class)");
mv.visitSet("propnames["+i+"]","\""+name+"\"");
i++;
......@@ -711,20 +713,18 @@ extends ClassGenerator {
BlockSourceCodeVisitor blockv, InjectionPoint<?> ip, String value ) {
Class<?> type = ip.getType();
if( type.isPrimitive() ) {
type = boxed.get(type);
}
String tname = getName(type);
type = ClassHelper.box(type);
String tname = ClassHelper.getName(type);
String castedvalue = "("+tname+")"+value;
if( ip instanceof InjectionPointFieldImpl ) {
InjectionPointFieldImpl<?> ipf = (InjectionPointFieldImpl<?>) ip;
Field f = ipf.getField();
int mod = f.getModifiers();
String fname = f.getName();
Class<?> dc = f.getDeclaringClass();
String name = getTargetTypeName();
if( isAccessibleWithStaticInvocation(mod,dc) ) {
if( MemberHelper.isAccessibleWithStaticInvocation(f,name) ) {
blockv.visitSet("content."+fname,castedvalue);
}
else {
......@@ -741,11 +741,11 @@ extends ClassGenerator {
else {
InjectionPointMethodImpl<?> ipm = (InjectionPointMethodImpl<?>) ip;
Method setter = ipm.getSetterMethod();
int mod = setter.getModifiers();
String mname = setter.getName();
Class<?> dc = setter.getDeclaringClass();
String name = getTargetTypeName();
if( isAccessibleWithStaticInvocation(mod,dc) ) {
if( MemberHelper.isAccessibleWithStaticInvocation(setter,name) ) {
blockv.visitIns("content.",mname,"(",castedvalue,")");
}
else {
......@@ -773,20 +773,20 @@ extends ClassGenerator {
String valueLocalFieldName, String valueLocalFieldType ) {
Class<?> type = ip.getType();
if( type.isPrimitive() ) {
type = boxed.get(type);
}
type = ClassHelper.box(type);
String tname =
valueLocalFieldType==null ? getName(type) : valueLocalFieldType;
valueLocalFieldType==null ?
ClassHelper.getName(type) :
valueLocalFieldType;
if( ip instanceof InjectionPointFieldImpl ) {
InjectionPointFieldImpl<?> ipf = (InjectionPointFieldImpl<?>) ip;
Field f = ipf.getField();
int mod = f.getModifiers();
String fname = f.getName();
Class<?> dc = f.getDeclaringClass();
String name = getTargetTypeName();
if( isAccessibleWithStaticInvocation(mod,dc) ) {
if( MemberHelper.isAccessibleWithStaticInvocation(f,name) ) {
blockv.visitSet(tname+" "+valueLocalFieldName,"content.",fname);
}
else {
......@@ -809,11 +809,11 @@ extends ClassGenerator {
return false;
}
else {
int mod = getter.getModifiers();
String mname = getter.getName();
Class<?> dc = getter.getDeclaringClass();
String name = getTargetTypeName();
if( isAccessibleWithStaticInvocation(mod,dc) ) {
if( MemberHelper.isAccessibleWithStaticInvocation(getter,name) ) {
blockv.visitSet(tname+" "+valueLocalFieldName,"content.",mname,"()");
}
else {
......@@ -1193,74 +1193,6 @@ extends ClassGenerator {
// Helper methods
// --------------------------------------------------------------------
/**
* Return <code>true</code> if the specified modifier allows to statically
* access methods and fields in the specified declaring class.
*
* TODO move to ClassHelper
*/
private boolean isAccessibleWithStaticInvocation( int mod, Class<?> dc ) {
if( Modifier.isPrivate(mod) ) { return false; }
if( Modifier.isPublic(mod) ) { return true; }
/*
* protected or package public. Check that the declaring class is in the
* same package as the one of the generated class.
*/
String targetClassName = getTargetTypeName();
int l = targetClassName.lastIndexOf('.');
String targetPackageName = targetClassName.substring(0,l);
String dpname = dc.getPackage().getName();
boolean b = dpname.equals(targetPackageName);
return b;
}
/**
* Associations between primitive types and their corresponding boxed types.
*/
final private static Map<Class<?>,Class<?>> boxed =
new HashMap<Class<?>,Class<?>>() {
private static final long serialVersionUID = -4311595724796658568L;
{
put(boolean.class,Boolean.class);
put(char.class,Character.class);
put(byte.class,Byte.class);
put(short.class,Short.class);
put(int.class,Integer.class);
put(long.class,Long.class);
put(float.class,Float.class);
put(double.class,Double.class);
}};
/**
* Workaround for Class#getName() which for array types, return something
* like <code>Ljava.lang.Object;</code> (should be <code>Object[]</code>)
* and for inner types, return something like <code>Outter$Inner</code>
* (should be Outter.Inner).
*
* This method is copied from
* {@link org.objectweb.fractal.juliac.UClass#getName(Class)}.
*
* @since 1.4.3
*/
private static String getName( Class<?> cl ) {
// Arrays
if( cl.isArray() ) {
Class<?> element = cl.getComponentType();
return getName(element)+"[]";
}
// Inner types
String name = cl.getName();
if( name.indexOf('$') != -1 ) {
name = name.replace('$','.');
}
return name;
}
/**
* Return a stringified representation for the instantiation of the
* specified type.
......@@ -1286,7 +1218,7 @@ extends ClassGenerator {
Class<?> element = cl.getComponentType();
return stringifyArrayInstantiation(element)+"[0]";
}
String name = getName(cl);
String name = ClassHelper.getName(cl);
return name;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment