private FieldDeclaration addInstanceVar(ConstructorDeclaration constructor, TypeReference typeReference, TypeDeclaration innerClass) { FieldDeclaration field = new FieldDeclaration(); field.modifiers = AccPrivate | AccStatic | AccFinal; field.name = "INSTANCE".toCharArray(); field.type = typeReference; AllocationExpression exp = new AllocationExpression(); exp.type = typeReference; exp.binding = constructor.binding; exp.sourceStart = innerClass.sourceStart; exp.sourceEnd = innerClass.sourceEnd; field.initialization = exp; return field; }
private void addFactoryMethod(EclipseNode singletonClass, TypeDeclaration astNode, TypeReference typeReference, TypeDeclaration innerClass, FieldDeclaration field) { MethodDeclaration factoryMethod = new MethodDeclaration(astNode.compilationResult); factoryMethod.modifiers = AccStatic | ClassFileConstants.AccPublic; factoryMethod.returnType = typeReference; factoryMethod.sourceStart = astNode.sourceStart; factoryMethod.sourceEnd = astNode.sourceEnd; factoryMethod.selector = "getInstance".toCharArray(); factoryMethod.bits = ECLIPSE_DO_NOT_TOUCH_FLAG; long pS = factoryMethod.sourceStart; long pE = factoryMethod.sourceEnd; long p = (long) pS << 32 | pE; FieldReference ref = new FieldReference(field.name, p); ref.receiver = new SingleNameReference(innerClass.name, p); ReturnStatement statement = new ReturnStatement(ref, astNode.sourceStart, astNode.sourceEnd); factoryMethod.statements = new Statement[]{statement}; EclipseHandlerUtil.injectMethod(singletonClass, factoryMethod); }
private ConstructorDeclaration addConstructor(EclipseNode singletonClass, TypeDeclaration astNode) { ConstructorDeclaration constructor = new ConstructorDeclaration(astNode.compilationResult); constructor.modifiers = AccPrivate; constructor.selector = astNode.name; constructor.sourceStart = astNode.sourceStart; constructor.sourceEnd = astNode.sourceEnd; constructor.thrownExceptions = null; constructor.typeParameters = null; constructor.bits |= ECLIPSE_DO_NOT_TOUCH_FLAG; constructor.bodyStart = constructor.declarationSourceStart = constructor.sourceStart = astNode.sourceStart; constructor.bodyEnd = constructor.declarationSourceEnd = constructor.sourceEnd = astNode.sourceEnd; constructor.arguments = null; EclipseHandlerUtil.injectMethod(singletonClass, constructor); return constructor; } }
@Override public int compare(CompilationUnitDeclaration o1, CompilationUnitDeclaration o2) { String s1 = new String(o1.getFileName()); String s2 = new String(o2.getFileName()); return s1.compareTo(s2); } }
/** * In no classpath, the model of the super interface isn't always correct. */ private boolean isCorrectTypeReference(TypeReference ref) { if (ref.resolvedType == null) { return false; } if (!(ref.resolvedType instanceof ProblemReferenceBinding)) { return true; } final String[] compoundName = CharOperation.charArrayToStringArray(((ProblemReferenceBinding) ref.resolvedType).compoundName); final String[] typeName = CharOperation.charArrayToStringArray(ref.getTypeName()); if (compoundName.length == 0 || typeName.length == 0) { return false; } return compoundName[compoundName.length - 1].equals(typeName[typeName.length - 1]); }
@Override public boolean visit(ThisReference thisReference, BlockScope scope) { context.enter(factory.Code().createThisAccess(references.getTypeReference(thisReference.resolvedType), thisReference.isImplicitThis()), thisReference); return true; }
@Override public void handle(AnnotationValues<Singleton> annotation, Annotation ast, EclipseNode annotationNode) { //remove annotation EclipseHandlerUtil.unboxAndRemoveAnnotationParameter(ast, "onType", "@Singleton(onType=", annotationNode); //add private constructor EclipseNode singletonClass = annotationNode.up(); TypeDeclaration singletonClassType = (TypeDeclaration) singletonClass.get(); ConstructorDeclaration constructor = addConstructor(singletonClass, singletonClassType); TypeReference singletonTypeRef = EclipseHandlerUtil.cloneSelfType(singletonClass, singletonClassType); //add inner class //add instance field StringBuilder sb = new StringBuilder(); sb.append(singletonClass.getName()); sb.append("Holder"); String innerClassName = sb.toString(); TypeDeclaration innerClass = new TypeDeclaration(singletonClassType.compilationResult); innerClass.modifiers = AccPrivate | AccStatic; innerClass.name = innerClassName.toCharArray(); FieldDeclaration instanceVar = addInstanceVar(constructor, singletonTypeRef, innerClass); FieldDeclaration[] declarations = new FieldDeclaration[]{instanceVar}; innerClass.fields = declarations; EclipseHandlerUtil.injectType(singletonClass, innerClass); addFactoryMethod(singletonClass, singletonClassType, singletonTypeRef, innerClass, instanceVar); }
String oName = new String(out[idx].type.getLastToken()); boolean found = false; for (IAnnotation i : in) {
@Override public boolean visit(CastExpression castExpression, BlockScope scope) { CastInfo ci = new CastInfo(); //the 8 bits from 21 to 28 represents number of enclosing brackets ci.nrOfBrackets = ((castExpression.bits >>> 21) & 0xF); ci.typeRef = this.references.buildTypeReference(castExpression.type, scope, true); context.casts.add(ci); castExpression.expression.traverse(this, scope); return false; }
@Override public boolean visit(Initializer initializer, MethodScope scope) { CtAnonymousExecutable b = factory.Core().createAnonymousExecutable(); if (initializer.isStatic()) { b.addModifier(ModifierKind.STATIC); } context.enter(b, initializer); return true; }
/** * Searches a type declared in imports. * * @param typeName * Expected type name. * @param imports * Search the type in imports. * @return qualified name of the expected type. */ static String searchType(String typeName, ImportReference[] imports) { if (typeName == null || imports == null) { return null; } for (ImportReference anImport : imports) { final String importType = CharOperation.charToString(anImport.getImportName()[anImport.getImportName().length - 1]); if (typeName.equals(importType)) { return CharOperation.toString(anImport.getImportName()); } } return null; }
@Override public boolean visit(StringLiteral stringLiteral, BlockScope scope) { context.enter(factory.Code().createLiteral(CharOperation.charToString(stringLiteral.source())), stringLiteral); return true; }
private <T> void insertGenericTypesInNoClasspathFromJDTInSpoon(TypeReference original, CtTypeReference<T> type) { if (original.resolvedType instanceof ProblemReferenceBinding && original.getTypeArguments() != null) { for (TypeReference[] typeReferences : original.getTypeArguments()) { if (typeReferences != null) { for (TypeReference typeReference : typeReferences) { type.addActualTypeArgument(this.getTypeReference(typeReference.resolvedType)); } } } } }
@Override public boolean visit(QualifiedThisReference qualifiedThisRef, BlockScope scope) { context.enter(factory.Code().createThisAccess(references.getTypeReference(qualifiedThisRef.qualification.resolvedType), qualifiedThisRef.isImplicitThis()), qualifiedThisRef); return true; }
@Override public boolean visit(IntLiteral intLiteral, BlockScope scope) { intLiteral.computeConstant(); CtLiteral<Integer> l = factory.Code().createLiteral(intLiteral.constant.intValue()); context.enter(l, intLiteral); return true; }
@Override public boolean visit(DoubleLiteral doubleLiteral, BlockScope scope) { doubleLiteral.computeConstant(); context.enter(factory.Code().createLiteral(doubleLiteral.constant.doubleValue()), doubleLiteral); return true; }
@Override public boolean visit(FloatLiteral floatLiteral, BlockScope scope) { floatLiteral.computeConstant(); context.enter(factory.Code().createLiteral(floatLiteral.constant.floatValue()), floatLiteral); return true; }
@Override public boolean visit(CharLiteral charLiteral, BlockScope scope) { charLiteral.computeConstant(); context.enter(factory.Code().createLiteral(charLiteral.constant.charValue()), charLiteral); return true; }
@Override public boolean visit(LongLiteral longLiteral, BlockScope scope) { longLiteral.computeConstant(); context.enter(factory.Code().createLiteral(longLiteral.constant.longValue()), longLiteral); return true; }
private int getSourceEndOfTypeReference(char[] contents, TypeReference node, int sourceEnd) { TypeReference[][] typeArgs = node.getTypeArguments(); if (typeArgs != null && typeArgs.length > 0) { TypeReference[] trs = typeArgs[typeArgs.length - 1];