@Override public String getSignature() { return "L" + getClassIdentifier() + ";"; }
@Override public Field getField(String varName) { Field field = getField(getFields(), varName); if (field == null) { field = getField(getStaticFields(), varName); } if (field == null && extending != null) { field = extending.getField(varName); } return field; }
public void validate(FutureType type) { for (Method method : type.getMethods()) { validate(type, method); } for (Method method : type.getStaticMethods()) { validate(type, method); } }
classNode.sourceFile = futureType.getSourceFile(); classNode.name = futureType.getClassIdentifier(); classNode.superName = futureType.getExtending().getClassIdentifier(); if (classNode.name.indexOf('$') != -1) { String declaringClass = classNode.name.substring(0, classNode.name.lastIndexOf("$")); classNode.outerClass = declaringClass; Iterable<Field> fields = futureType.getFields(); for (Field field : fields) { classNode.fields.add(new FieldNode( Iterable<Field> staticFields = futureType.getStaticFields(); for (Field field : staticFields) { classNode.fields.add(new FieldNode( ImmutableList<Method> constructors = futureType.getConstructors(); if (constructors.isEmpty()) { futureType.getName(), new MemberFlags(false, false, PUBLIC), VOID, for (Method method : futureType.getMethods()) { classNode.methods.add(getMethodNode(futureType, method)); for (Method method : futureType.getStaticMethods()) { classNode.methods.add(getMethodNode(futureType, method));
@Override public void visit(FutureType futureType) { int lastDot = futureType.getName().lastIndexOf('.'); String name; if (lastDot == -1) { name = futureType.getName(); } else { name = futureType.getName().substring(lastDot + 1); println("package "+futureType.getName().substring(0, lastDot)+";"); (futureType.getExtending() == null ? "" : " extends " + futureType.getExtending().getName())+ " {"); incIndent(); println("// static fields"); printFields(futureType.getStaticFields()); println(); println("// static methods"); printMethods(futureType, name, futureType.getStaticMethods()); println(); println("// fields"); printFields(futureType.getFields()); println(); println("// constructors"); printMethods(futureType, name, futureType.getConstructors()); println(); println("// methods"); printMethods(futureType, name, futureType.getMethods()); println(); decIndent();
@Override public void visit(CallConstructorExpression callConstructorExpression) { methodByteCodeContext.incIndent("call super constructor"); methodByteCodeContext.loadThis(); Method constructor = methodContext.getType().getSuperConstructor(callConstructorExpression.getParameters().size()); if (constructor == null) { throw new RuntimeException( "can't find constructor with " + callConstructorExpression.getParameters().size() + " parameters in " + methodContext.getType().getExtending() + " parent of " + methodContext.getType()); } loadParameters("<init>", constructor, callConstructorExpression.getParameters()); methodByteCodeContext.addInstruction(new MethodInsnNode(INVOKESPECIAL, methodContext.getType().getExtending().getClassIdentifier(), "<init>", constructor.getSignature()), "super(...)"); methodByteCodeContext.decIndent(); }
@Override public String getClassIdentifier() { return getName().replace('.', '/'); }
public Method getSuperConstructor(int parameterCount) { return getExtending().getConstructor(parameterCount); }
/** * @return the resulting class */ public FutureType endClass() { FutureType futureType = new FutureType(name, extending == null ? ExistingType.OBJECT : extending, fields, staticFields, methods, staticMethods, constructors, sourceFile); new ClassValidator().validate(futureType); return futureType; }
public VarAccessType getVarAccessType(String varName) { Parameter param = getParam(varName); if (param != null) { return new ParameterAccessType(param); } else { Field field = type.getField(varName); if (field!=null) { return new FieldAccessType(field); } else { LocalVarContext localVarContext; if (!localVars.containsKey(varName)) { throw new RuntimeException("can not access " + varName + " in parameters, local variables or fields"); } else { localVarContext = localVars.get(varName); } return new LocalVariableAccessType(varName, localVarContext.getIndex(), localVarContext.getType()); } } }
public Class<?> define(FutureType type) { byte[] classBytes = asmTypeGenerator.generate(type); return super.defineClass(type.getName(), classBytes, 0, classBytes.length); } }
public String getClassIdentifier() { return type.getClassIdentifier(); }
public void visit(FieldAccessType fieldAccessType) { Field field = fieldAccessType.getField(); if (field.isStatic()) { methodByteCodeContext.addInstruction( new FieldInsnNode(GETSTATIC, methodContext.getType().getClassIdentifier(), getExpression.getFieldName(), field.getSignature()), "get static field", getExpression.getFieldName()); } else { methodByteCodeContext.loadThis("get field", getExpression.getFieldName(), "on this"); methodByteCodeContext.addInstruction( new FieldInsnNode(GETFIELD, methodContext.getType().getClassIdentifier(), getExpression.getFieldName(), field.getSignature()), "get field", getExpression.getFieldName()); } lastExpressionType = field.getType(); } public void visit(ParameterAccessType parameterAccessType) {