List of usage examples for org.eclipse.jdt.core.dom PrimitiveType CHAR
Code CHAR
To view the source code for org.eclipse.jdt.core.dom PrimitiveType CHAR.
Click Source Link
From source file:de.crowdcode.kissmda.core.jdt.DataTypeUtils.java
License:Apache License
private void createPrimitiveTypeCodes() { primitiveTypeCodes = new HashMap<String, Code>(); primitiveTypeCodes.put("integer", PrimitiveType.INT); primitiveTypeCodes.put("int", PrimitiveType.INT); primitiveTypeCodes.put("short", PrimitiveType.SHORT); primitiveTypeCodes.put("double", PrimitiveType.DOUBLE); primitiveTypeCodes.put("long", PrimitiveType.LONG); primitiveTypeCodes.put("boolean", PrimitiveType.BOOLEAN); primitiveTypeCodes.put("byte", PrimitiveType.BYTE); primitiveTypeCodes.put("character", PrimitiveType.CHAR); primitiveTypeCodes.put("char", PrimitiveType.CHAR); primitiveTypeCodes.put("float", PrimitiveType.FLOAT); primitiveTypeCodes.put("void", PrimitiveType.VOID); // Publish an event to the bus eventBus.post(new PrimitiveTypeCodesCreatedEvent(primitiveTypeCodes)); }
From source file:lang.java.jdt.internal.JdtAstToRascalAstConverter.java
License:Open Source License
public boolean visit(PrimitiveType node) { IValue type;//from w ww .j a v a 2 s .co m if (node.getPrimitiveTypeCode().equals(PrimitiveType.BOOLEAN)) { type = values.constructor(Java.CONS_BOOLEAN); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.BYTE)) { type = values.constructor(Java.CONS_BYTE); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.CHAR)) { type = values.constructor(Java.CONS_CHAR); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.DOUBLE)) { type = values.constructor(Java.CONS_DOUBLE); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.FLOAT)) { type = values.constructor(Java.CONS_FLOAT); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.INT)) { type = values.constructor(Java.CONS_INT); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.LONG)) { type = values.constructor(Java.CONS_LONG); } else if (node.getPrimitiveTypeCode().equals(PrimitiveType.SHORT)) { type = values.constructor(Java.CONS_SHORT); } else { type = values.constructor(Java.CONS_VOID); } ownValue = constructRascalNode(node, type); return false; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
public boolean visit(CastExpression node) { Type type = node.getType();//from w ww.jav a2s. c om /* * TODO: some casting should have its meaning! * int to byte, int to short, long to int will lost values */ Expression expression = node.getExpression(); if (type.isPrimitiveType()) { ITypeBinding resolveTypeBinding = expression.resolveTypeBinding(); if (resolveTypeBinding != null) { String name = resolveTypeBinding.getName(); PrimitiveType pType = (PrimitiveType) type; if (pType.getPrimitiveTypeCode() == PrimitiveType.INT || pType.getPrimitiveTypeCode() == PrimitiveType.BYTE || pType.getPrimitiveTypeCode() == PrimitiveType.SHORT || pType.getPrimitiveTypeCode() == PrimitiveType.LONG) { if ("char".equals(name)) { buffer.append("("); if (expression instanceof ParenthesizedExpression) { ParenthesizedExpression pe = (ParenthesizedExpression) expression; pe.getExpression().accept(this); } else { expression.accept(this); } buffer.append(").charCodeAt (0)"); return false; } else if ("float".equals(name) || "double".equals(name)) { //buffer.append("Math.round ("); buffer.append("Clazz."); buffer.append(name); buffer.append("To"); String targetType = pType.getPrimitiveTypeCode().toString(); buffer.append(targetType.substring(0, 1).toUpperCase()); buffer.append(targetType.substring(1)); buffer.append(" ("); if (expression instanceof ParenthesizedExpression) { ParenthesizedExpression pe = (ParenthesizedExpression) expression; pe.getExpression().accept(this); } else { expression.accept(this); } buffer.append(")"); return false; } } if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { if ("char".equals(name)) { // buffer.append("("); // node.getExpression().accept(this); // buffer.append (").charCodeAt (0)"); // return false; } else if ("float".equals(name) || "double".equals(name)) { // TODO: buffer.append("Clazz."); buffer.append(name); buffer.append("ToChar ("); // buffer.append("String.fromCharCode ("); // buffer.append("Math.round ("); if (expression instanceof ParenthesizedExpression) { ParenthesizedExpression pe = (ParenthesizedExpression) expression; pe.getExpression().accept(this); } else { expression.accept(this); } // buffer.append (")"); buffer.append(")"); return false; } else if ("int".equals(name) || "byte".equals(name) // || "double".equals(name) || "float".equals(name) || "short".equals(name) || "long".equals(name)) { Object constantValue = expression.resolveConstantExpressionValue(); if (constantValue != null) { if (constantValue instanceof Integer) { int value = ((Integer) constantValue).intValue(); if ((value >= '0' && value <= '9') || (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')) { buffer.append('\''); buffer.append((char) value); buffer.append('\''); return false; } } else if (constantValue instanceof Long) { long value = ((Long) constantValue).longValue(); if ((value >= '0' && value <= '9') || (value >= 'A' && value <= 'Z') || (value >= 'a' && value <= 'z')) { buffer.append('\''); buffer.append((char) value); buffer.append('\''); return false; } } } buffer.append("String.fromCharCode ("); if (expression instanceof ParenthesizedExpression) { ParenthesizedExpression pe = (ParenthesizedExpression) expression; pe.getExpression().accept(this); } else { expression.accept(this); } buffer.append(")"); return false; } } } } expression.accept(this); return false; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
public boolean visitWith(FieldDeclaration node, boolean ignoreInitializer) { if ((node.getModifiers() & Modifier.STATIC) != 0) { return false; }//from www. ja v a 2 s. com ASTNode xparent = node.getParent(); while (xparent != null && !(xparent instanceof AbstractTypeDeclaration) && !(xparent instanceof AnonymousClassDeclaration)) { xparent = xparent.getParent(); } ITypeBinding typeBinding = null; //ITypeBinding anonBinding = null; if (xparent != null) { if (xparent instanceof AbstractTypeDeclaration) { AbstractTypeDeclaration type = (AbstractTypeDeclaration) xparent; typeBinding = type.resolveBinding(); } else if (xparent instanceof AnonymousClassDeclaration) { AnonymousClassDeclaration type = (AnonymousClassDeclaration) xparent; typeBinding = type.resolveBinding();//.getSuperclass(); } } List fragments = node.fragments(); for (Iterator iter = fragments.iterator(); iter.hasNext();) { VariableDeclarationFragment element = (VariableDeclarationFragment) iter.next(); String fieldName = getJ2SName(element.getName()); // String fieldName = element.getName().getIdentifier(); String ext = ""; if (checkKeyworkViolation(fieldName)) { ext += "$"; } if (typeBinding != null && checkSameName(typeBinding, fieldName)) { ext += "$"; } //fieldName = ext + fieldName; //buffer.append(fieldName); buffer.append("this."); if (isInheritedFieldName(typeBinding, fieldName)) { fieldName = getFieldName(typeBinding, fieldName); buffer.append(ext + fieldName); } else { buffer.append(ext + fieldName); } //buffer.append(element.getName()); buffer.append(" = "); if (!ignoreInitializer && element.getInitializer() != null) { element.getInitializer().accept(this); } else { boolean isArray = false; List frags = node.fragments(); if (frags.size() > 0) { VariableDeclarationFragment varFrag = (VariableDeclarationFragment) frags.get(0); IVariableBinding resolveBinding = varFrag.resolveBinding(); if (resolveBinding != null) { isArray = resolveBinding.getType().isArray(); if (isArray) { buffer.append("null"); } } } if (!isArray) { if (node.getType().isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) node.getType(); if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { buffer.append("false"); } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { buffer.append("'\\0'"); } else { buffer.append("0"); } } else { buffer.append("null"); } } } buffer.append(";\r\n"); } return false; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
public boolean visit(MethodDeclaration node) { // methodBuffer = new StringBuffer(); if (getJ2STag(node, "@j2sIgnore") != null) { return false; }/* w ww .j av a2s .c o m*/ IMethodBinding mBinding = node.resolveBinding(); if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimpleRPCRunnable", "ajaxRun")) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } String[] pipeMethods = new String[] { "pipeSetup", "pipeThrough", "through", "pipeMonitoring", "pipeMonitoringInterval", "pipeWaitClosingInterval", "setPipeHelper" }; for (int i = 0; i < pipeMethods.length; i++) { if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.SimplePipeRunnable", pipeMethods[i])) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } } if (Bindings.isMethodInvoking(mBinding, "net.sf.j2s.ajax.CompoundPipeSession", "convert")) { if (getJ2STag(node, "@j2sKeep") == null) { return false; } } if (mBinding != null) { methodDeclareStack.push(mBinding.getKey()); } if (node.getBody() == null) { /* * Abstract or native method */ if (isMethodNativeIgnored(node)) { return false; } } /* * To skip those methods or constructors which are just overriding with * default super methods or constructors. */ Block body = node.getBody(); boolean needToCheckArgs = false; List argsList = null; if (body != null && containsOnlySuperCall(body)) { List sts = body.statements(); Object statement = sts.get(sts.size() - 1); if (statement instanceof ReturnStatement) { ReturnStatement ret = (ReturnStatement) statement; Expression exp = ret.getExpression(); if (exp instanceof SuperMethodInvocation) { SuperMethodInvocation superRet = (SuperMethodInvocation) exp; if (superRet.getName().toString().equals(node.getName().toString())) { // same method name needToCheckArgs = true; argsList = superRet.arguments(); } } } else if (statement instanceof ExpressionStatement) { ExpressionStatement sttmt = (ExpressionStatement) statement; Expression exp = sttmt.getExpression(); if (exp instanceof SuperMethodInvocation) { SuperMethodInvocation superRet = (SuperMethodInvocation) exp; if (superRet.getName().toString().equals(node.getName().toString())) { // same method name needToCheckArgs = true; argsList = superRet.arguments(); } } } else if (statement instanceof SuperConstructorInvocation) { SuperConstructorInvocation superConstructor = (SuperConstructorInvocation) statement; needToCheckArgs = true; argsList = superConstructor.arguments(); if (argsList.size() == 0) { IMethodBinding constructorBinding = superConstructor.resolveConstructorBinding(); ITypeBinding declaringClass = constructorBinding.getDeclaringClass(); if ("java.lang.Object".equals(declaringClass.getQualifiedName())) { needToCheckArgs = false; } } } // } else if (node.isConstructor() && (body != null && body.statements().size() == 0)) { // IMethodBinding superConstructorExisted = Bindings.findConstructorInHierarchy(mBinding.getDeclaringClass(), mBinding); // if (superConstructorExisted != null) { // needToCheckArgs = true; // argsList = new ArrayList(); // } } if (needToCheckArgs) { List params = node.parameters(); if (params.size() == argsList.size()) { // same parameters count boolean isOnlySuper = true; for (Iterator iter = params.iterator(), itr = argsList.iterator(); iter.hasNext();) { ASTNode astNode = (ASTNode) iter.next(); ASTNode argNode = (ASTNode) itr.next(); if (astNode instanceof SingleVariableDeclaration && argNode instanceof SimpleName) { SingleVariableDeclaration varDecl = (SingleVariableDeclaration) astNode; String paramID = varDecl.getName().getIdentifier(); String argID = ((SimpleName) argNode).getIdentifier(); if (!paramID.equals(argID)) { // not with the same order, break out isOnlySuper = false; break; } } else { isOnlySuper = false; break; } } if (isOnlySuper && getJ2STag(node, "@j2sKeep") == null) { return false; } } } if ((node.getModifiers() & Modifier.PRIVATE) != 0) { if (mBinding != null) { boolean isReferenced = MethodReferenceASTVisitor.checkReference(node.getRoot(), mBinding.getKey()); if (!isReferenced && getJ2STag(node, "@j2sKeep") == null) { return false; } } } if (node.isConstructor()) { if (getJ2STag(node, "@j2sOverride") != null) { buffer.append("Clazz.overrideConstructor ("); } else { buffer.append("Clazz.makeConstructor ("); } } else { if ((node.getModifiers() & Modifier.STATIC) != 0) { /* replace full class name with short variable name */ buffer.append("cla$$"); //buffer.append(fullClassName); buffer.append("."); //buffer.append(methods[i].getName()); node.getName().accept(this); buffer.append(" = "); } if (getJ2STag(node, "@j2sOverride") != null) { buffer.append("Clazz.overrideMethod ("); } else { boolean isOK2AutoOverriding = canAutoOverride(node); if (isOK2AutoOverriding) { buffer.append("Clazz.overrideMethod ("); } else { buffer.append("Clazz.defineMethod ("); } } } /* replace full class name with short variable name */ buffer.append("cla$$"); if (node.isConstructor()) { buffer.append(", "); } else { buffer.append(", \""); String identifier = getJ2SName(node.getName()); if (checkKeyworkViolation(identifier)) { buffer.append('$'); } buffer.append(identifier); buffer.append("\", "); } buffer.append("\r\n"); boolean isPrivate = (node.getModifiers() & Modifier.PRIVATE) != 0; if (isPrivate) { buffer.append("($fz = "); } buffer.append("function ("); List parameters = node.parameters(); visitList(parameters, ", "); buffer.append(") "); if (node.isConstructor()) { boolean isSuperCalled = false; List statements = node.getBody().statements(); if (statements.size() > 0) { ASTNode firstStatement = (ASTNode) statements.get(0); if (firstStatement instanceof SuperConstructorInvocation || firstStatement instanceof ConstructorInvocation) { isSuperCalled = true; } } if (getJ2STag(node, "@j2sIgnoreSuperConstructor") != null) { isSuperCalled = true; } boolean existedSuperClass = false; IMethodBinding binding = node.resolveBinding(); if (binding != null) { ITypeBinding declaringClass = binding.getDeclaringClass(); ITypeBinding superclass = declaringClass.getSuperclass(); String qualifiedName = discardGenericType(superclass.getQualifiedName()); existedSuperClass = superclass != null && !"java.lang.Object".equals(qualifiedName) && !"java.lang.Enum".equals(qualifiedName); } if (!isSuperCalled && existedSuperClass) { buffer.append("{\r\n"); buffer.append("Clazz.superConstructor (this, "); buffer.append(assureQualifiedName(shortenQualifiedName(getFullClassName()))); boolean constructorVarargs = isConstructorVarargs(binding, true); if (constructorVarargs) { buffer.append(", [[]]);\r\n"); } else { buffer.append(", []);\r\n"); } boolean read = checkJ2STags(node, false); if (!read) { blockLevel++; visitList(statements, ""); //buffer.append("}"); endVisit(node.getBody()); } else { buffer.append("}"); } } else { boolean read = checkJ2STags(node, true); if (!read) { node.getBody().accept(this); } } } else if (node.getBody() == null) { blockLevel++; boolean read = checkJ2STags(node, true); if (!read) { buffer.append("{\r\n"); visitNativeJavadoc(node.getJavadoc(), null, false); buffer.append("}"); } List normalVars = ((ASTVariableVisitor) getAdaptable(ASTVariableVisitor.class)).normalVars; for (int i = normalVars.size() - 1; i >= 0; i--) { ASTFinalVariable var = (ASTFinalVariable) normalVars.get(i); if (var.blockLevel >= blockLevel) { normalVars.remove(i); } } blockLevel--; } else { boolean read = checkJ2STags(node, true); if (!read) { node.getBody().accept(this); } } if (isPrivate) { buffer.append(", $fz.isPrivate = true, $fz)"); } if (parameters.size() != 0) { buffer.append(", \""); for (Iterator iter = parameters.iterator(); iter.hasNext();) { SingleVariableDeclaration element = (SingleVariableDeclaration) iter.next(); boolean isArray = false; IBinding resolveBinding = element.getName().resolveBinding(); if (resolveBinding instanceof IVariableBinding) { IVariableBinding varBinding = (IVariableBinding) resolveBinding; if (varBinding != null) { isArray = varBinding.getType().isArray(); if (isArray) { //buffer.append("Array"); buffer.append("~A"); } } } if (!isArray) { Type type = element.getType(); if (type.isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) type; if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { buffer.append("~B"); // Boolean } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { buffer.append("~S"); // String for char } else { buffer.append("~N"); // Number } } else if (type.isArrayType()) { buffer.append("~A"); // Array } else { ITypeBinding binding = type.resolveBinding(); if (binding != null) { if (binding.isTypeVariable()) { buffer.append("~O"); } else { String name = binding.getQualifiedName(); name = shortenQualifiedName(name); if ("String".equals(name)) { buffer.append("~S"); } else if ("Object".equals(name)) { buffer.append("~O"); } else { buffer.append(name); } } } else { buffer.append(type); } } } if (iter.hasNext()) { buffer.append(","); } } buffer.append("\""); } buffer.append(");\r\n"); return false; }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
public void endVisit(TypeDeclaration node) { if (node != rootTypeNode && node.getParent() != null && (node.getParent() instanceof AbstractTypeDeclaration || node.getParent() instanceof TypeDeclarationStatement)) { return;//from w ww . j a v a 2 s. co m } if (!node.isInterface()) { buffer.append("Clazz.instantialize (this, arguments);\r\n"); //buffer.append("};\r\n"); buffer.append("}, "); } String emptyFun = "Clazz.decorateAsClass (function () {\r\n" + "Clazz.instantialize (this, arguments);\r\n" + "}, "; int idx = buffer.lastIndexOf(emptyFun); if (idx != -1 && idx == buffer.length() - emptyFun.length()) { buffer.replace(idx, buffer.length(), "Clazz.declareType ("); } String fullClassName = null; String packageName = ((ASTPackageVisitor) getAdaptable(ASTPackageVisitor.class)).getPackageName(); String className = ((ASTTypeVisitor) getAdaptable(ASTTypeVisitor.class)).getClassName(); if (packageName != null && packageName.length() != 0) { fullClassName = packageName + '.' + className; } else { fullClassName = className; } if (node.isInterface()) { boolean needReturn = false; for (Iterator iter = node.bodyDeclarations().iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof Initializer) { if (getJ2STag((Initializer) element, "@j2sIgnore") != null) { continue; } needReturn = true; } else if (element instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) element; if (getJ2STag(field, "@j2sIgnore") != null) { continue; } if ((field.getModifiers() & Modifier.STATIC) != 0) { needReturn = true; } else if (node.isInterface()) { List fragments = field.fragments(); needReturn = fragments.size() > 0; } } if (needReturn) { break; } } if (needReturn) { buffer.append("cla$$ = "); } buffer.append("Clazz.declareInterface ("); int lastIndexOf = fullClassName.lastIndexOf('.'); if (lastIndexOf != -1) { buffer.append(assureQualifiedName(shortenPackageName(fullClassName))); buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\""); } else { buffer.append("null, \"" + fullClassName + "\""); } } else { int lastIndexOf = fullClassName.lastIndexOf('.'); if (lastIndexOf != -1) { buffer.append(assureQualifiedName(shortenPackageName(fullClassName))); buffer.append(", \"" + fullClassName.substring(lastIndexOf + 1) + "\""); } else { buffer.append("null, \"" + fullClassName + "\""); } buffer.append(", "); } boolean defined = false; ITypeBinding typeBinding = node.resolveBinding(); if (typeBinding != null) { ITypeBinding superclass = typeBinding.getSuperclass(); if (superclass != null) { String clazzName = superclass.getQualifiedName(); clazzName = assureQualifiedName(shortenQualifiedName(clazzName)); if (clazzName != null && clazzName.length() != 0 && !"Object".equals(clazzName)) { buffer.append(clazzName); defined = true; } } } if (!defined && !node.isInterface()) { buffer.append("null"); } buffer.append(", "); //List superInterfaces = node.superInterfaceTypes(); List superInterfaces = node.superInterfaceTypes(); int size = superInterfaces.size(); if (size == 0) { buffer.append("null"); } else if (size > 1) { buffer.append("["); } for (Iterator iter = superInterfaces.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); ITypeBinding binding = ((Type) element).resolveBinding(); if (binding != null) { String clazzName = binding.getQualifiedName(); clazzName = assureQualifiedName(shortenQualifiedName(clazzName)); buffer.append(clazzName); } else { buffer.append(element); } if (iter.hasNext()) { buffer.append(", "); } } if (size > 1) { buffer.append("]"); } ITypeBinding superclass = null; Type superType = node.getSuperclassType(); if (superType != null) { superclass = superType.resolveBinding(); } if (superclass != null) { ITypeBinding binding = superclass;//.resolveTypeBinding(); if (binding != null && !binding.isTopLevel()) { if ((binding.getModifiers() & Modifier.STATIC) == 0) { buffer.append(", Clazz.innerTypeInstance ("); buffer.append(assureQualifiedName(shortenQualifiedName(binding.getQualifiedName()))); buffer.append(", this, null, Clazz.inheritArgs"); buffer.append(")"); } } } int len = buffer.length(); // ", null, null" if (", null, null".equals(buffer.substring(len - 12))) { buffer.delete(len - 12, len); } else if (", null".equals(buffer.substring(len - 6))) { buffer.delete(len - 6, len); } buffer.append(");\r\n"); StringBuffer laterBufferBackup = laterBuffer; //buffer.append(laterBuffer); laterBuffer = new StringBuffer(); // Enum is considered as static member! List bodyDeclarations = node.bodyDeclarations(); StringBuffer tmpBuffer = buffer; //StringBuffer tmpLaterBuffer = laterBuffer; // StringBuffer tmpMethodBuffer = methodBuffer; // buffer = new StringBuffer(); // laterBuffer = new StringBuffer(); // methodBuffer = new StringBuffer(); boolean needPreparation = false; for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) element; if (getJ2STag(field, "@j2sIgnore") != null) { continue; } if (node.isInterface() || !isFieldNeedPreparation(field)) { continue; } needPreparation = true; //element.accept(this); break; } else if (element instanceof Initializer) { Initializer init = (Initializer) element; if (getJ2STag(init, "@j2sIgnore") != null) { continue; } if ((init.getModifiers() & Modifier.STATIC) == 0) { needPreparation = true; break; } } } // if (methodBuffer.length() > 0) { // tmpBuffer.append(methodBuffer.toString()); // } // buffer = tmpBuffer; // laterBuffer = tmpLaterBuffer; // methodBuffer = tmpMethodBuffer; if (needPreparation) { buffer.append("Clazz.prepareFields (cla$$, function () {\r\n"); for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) element; if (getJ2STag(field, "@j2sIgnore") != null) { continue; } if (node.isInterface() || !isFieldNeedPreparation(field)) { continue; } element.accept(this); } else if (element instanceof Initializer) { Initializer init = (Initializer) element; if (getJ2STag(init, "@j2sIgnore") != null) { continue; } if ((init.getModifiers() & Modifier.STATIC) == 0) { element.accept(this); } } } buffer.append("});\r\n"); } for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof EnumDeclaration) { element.accept(this); } } MethodDeclaration[] methods = node.getMethods(); for (int i = 0; i < methods.length; i++) { // All the methods are defined outside the main function body! -- March 17, 2006 methods[i].accept(this); } int staticCount = -1; ReferenceASTVisitor refVisitor = new ReferenceASTVisitor(); /* * Fixing bug#2797539 : Incorrect instantiation of member before inner class declaration inside interface * http://sourceforge.net/tracker/?func=detail&aid=2797539&group_id=155436&atid=795800 * Interface's inner classes declaration is not in the correct order. Fix it by move codes a few lines * ahead of member initialization. */ for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof TypeDeclaration) { if (node.isInterface()) { /* * Here will create a new visitor to do the Java2Script process * and laterBuffer may be filled with contents. */ element.accept(this); } } } // Interface's inner interfaces or classes buffer.append(laterBuffer); tmpBuffer = buffer; StringBuffer tmpLaterBuffer = laterBuffer; buffer = new StringBuffer(); laterBuffer = new StringBuffer(); /* Testing class declarations in initializers */ for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof TypeDeclaration) { if (node.isInterface()) { // the above codes have already dealt those inner classes inside interface // just ignore here continue; } } else if (element instanceof Initializer) { if (getJ2STag((Initializer) element, "@j2sIgnore") != null) { continue; } if ((((Initializer) element).getModifiers() & Modifier.STATIC) != 0) { element.accept(this); } else { continue; // ignore here } } else if (element instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) element; if (getJ2STag(field, "@j2sIgnore") != null) { continue; } if ((field.getModifiers() & Modifier.STATIC) != 0) { List fragments = field.fragments(); for (int j = 0; j < fragments.size(); j++) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j); Expression initializer = vdf.getInitializer(); if (initializer != null) { initializer.accept(this); } } } else if (node.isInterface()) { List fragments = field.fragments(); for (int j = 0; j < fragments.size(); j++) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j); Expression initializer = vdf.getInitializer(); vdf.getName().accept(this); if (initializer != null) { initializer.accept(this); } } } } } buffer = tmpBuffer; laterBuffer = tmpLaterBuffer; if (methodBuffer.length() > 0) { buffer.append(methodBuffer); methodBuffer = new StringBuffer(); } // method first /* * Fixing bug for such class * class A { * class B () { * } * static class C extends A { * } * } * A.B should be declared before A.C: * c$.$A$B$ = function () ... * c$.Clazz.decorateAsClass ( ... */ buffer.append(laterBufferBackup); for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof TypeDeclaration) { if (node.isInterface()) { // the above codes have already dealt those inner classes inside interface // just ignore here continue; } } else if (element instanceof Initializer) { if (getJ2STag((Initializer) element, "@j2sIgnore") != null) { continue; } if (staticCount != -1) { buffer.append(");\r\n"); staticCount = -1; } if ((((Initializer) element).getModifiers() & Modifier.STATIC) != 0) { element.accept(this); } else { continue; // ignore here } } else if (element instanceof FieldDeclaration) { FieldDeclaration field = (FieldDeclaration) element; if (getJ2STag(field, "@j2sIgnore") != null) { continue; } if ((field.getModifiers() & Modifier.STATIC) != 0) { List fragments = field.fragments(); for (int j = 0; j < fragments.size(); j++) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j); if ("serialVersionUID".equals(vdf.getName().getIdentifier())) { continue; } Expression initializer = vdf.getInitializer(); refVisitor.setReferenced(false); if (initializer != null) { initializer.accept(refVisitor); } if (refVisitor.isReferenced()) { if (staticCount != -1) { buffer.append(");\r\n"); staticCount = -1; } buffer.append("cla$$"); //buffer.append(fullClassName); buffer.append("."); //buffer.append(vdf.getName()); vdf.getName().accept(this); buffer.append(" = "); buffer.append("cla$$"); //buffer.append(fullClassName); buffer.append(".prototype."); vdf.getName().accept(this); buffer.append(" = "); initializer.accept(this); buffer.append(";\r\n"); continue; } else { staticCount++; if (staticCount == 0) { buffer.append("Clazz.defineStatics (cla$$"); } } buffer.append(",\r\n\""); vdf.getName().accept(this); buffer.append("\", "); Type type = field.getType(); if (initializer != null) { if (type.isPrimitiveType() && ((PrimitiveType) type).getPrimitiveTypeCode() == PrimitiveType.CHAR) { ITypeBinding tBinding = initializer.resolveTypeBinding(); if (tBinding != null && !("char".equals(tBinding.getName()))) { buffer.append("String.fromCharCode ("); initializer.accept(this); buffer.append(")"); } else { initializer.accept(this); } } else { initializer.accept(this); } } else { if (type.isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) type; if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { buffer.append("false"); } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { buffer.append("'\\0'"); } else { buffer.append("0"); } } else { buffer.append("null"); } } } } else if (node.isInterface()) { List fragments = field.fragments(); for (int j = 0; j < fragments.size(); j++) { VariableDeclarationFragment vdf = (VariableDeclarationFragment) fragments.get(j); if ("serialVersionUID".equals(vdf.getName().getIdentifier())) { continue; } Expression initializer = vdf.getInitializer(); refVisitor.setReferenced(false); if (initializer != null) { initializer.accept(refVisitor); } if (refVisitor.isReferenced()) { if (staticCount != -1) { buffer.append(");\r\n"); staticCount = -1; } buffer.append("cla$$"); buffer.append("."); vdf.getName().accept(this); buffer.append(" = "); buffer.append("cla$$"); buffer.append(".prototype."); vdf.getName().accept(this); buffer.append(" = "); initializer.accept(this); buffer.append(";\r\n"); continue; } else { staticCount++; if (staticCount == 0) { buffer.append("Clazz.defineStatics (cla$$"); } } buffer.append(",\r\n\""); vdf.getName().accept(this); buffer.append("\", "); Type type = field.getType(); if (initializer != null) { if (type.isPrimitiveType() && ((PrimitiveType) type).getPrimitiveTypeCode() == PrimitiveType.CHAR) { ITypeBinding tBinding = initializer.resolveTypeBinding(); if (tBinding != null && !("char".equals(tBinding.getName()))) { buffer.append("String.fromCharCode ("); initializer.accept(this); buffer.append(")"); } else { initializer.accept(this); } } else { initializer.accept(this); } } else { if (type.isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) type; if (pType.getPrimitiveTypeCode() == PrimitiveType.BOOLEAN) { buffer.append("false"); } else if (pType.getPrimitiveTypeCode() == PrimitiveType.CHAR) { buffer.append("'\\0'"); } else { buffer.append("0"); } } else { buffer.append("null"); } } } } } } if (staticCount != -1) { buffer.append(");\r\n"); } String fieldsSerializables = prepareSimpleSerializable(node, bodyDeclarations); if (fieldsSerializables.length() > 0) { buffer.append("Clazz.registerSerializableFields(cla$$, "); buffer.append(fieldsSerializables.toString()); buffer.append(");\r\n"); } readSources(node, "@j2sSuffix", "\r\n", "\r\n", true); laterBuffer = new StringBuffer(); super.endVisit(node); }
From source file:net.sf.j2s.core.astvisitors.ASTScriptVisitor.java
License:Open Source License
private String prepareSimpleSerializable(TypeDeclaration node, List bodyDeclarations) { StringBuffer fieldsSerializables = new StringBuffer(); ITypeBinding binding = node.resolveBinding(); boolean isSimpleSerializable = binding != null && (Bindings.findTypeInHierarchy(binding, "net.sf.j2s.ajax.SimpleSerializable") != null); for (Iterator iter = bodyDeclarations.iterator(); iter.hasNext();) { ASTNode element = (ASTNode) iter.next(); if (element instanceof FieldDeclaration) { if (node.isInterface()) { /*/* w ww.jav a 2 s . com*/ * As members of interface should be treated * as final and for javascript interface won't * get instantiated, so the member will be * treated specially. */ continue; } FieldDeclaration fieldDeclaration = (FieldDeclaration) element; if (isSimpleSerializable) { List fragments = fieldDeclaration.fragments(); int modifiers = fieldDeclaration.getModifiers(); if ((Modifier.isPublic(modifiers)/* || Modifier.isProtected(modifiers)*/) && !Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) { Type type = fieldDeclaration.getType(); int dims = 0; if (type.isArrayType()) { dims = 1; type = ((ArrayType) type).getComponentType(); } String mark = null; if (type.isPrimitiveType()) { PrimitiveType pType = (PrimitiveType) type; Code code = pType.getPrimitiveTypeCode(); if (code == PrimitiveType.FLOAT) { mark = "F"; } else if (code == PrimitiveType.DOUBLE) { mark = "D"; } else if (code == PrimitiveType.INT) { mark = "I"; } else if (code == PrimitiveType.LONG) { mark = "L"; } else if (code == PrimitiveType.SHORT) { mark = "S"; } else if (code == PrimitiveType.BYTE) { mark = "B"; } else if (code == PrimitiveType.CHAR) { mark = "C"; } else if (code == PrimitiveType.BOOLEAN) { mark = "b"; } } ITypeBinding resolveBinding = type.resolveBinding(); if ("java.lang.String".equals(resolveBinding.getQualifiedName())) { mark = "s"; } else { ITypeBinding t = resolveBinding; do { String typeName = t.getQualifiedName(); if ("java.lang.Object".equals(typeName)) { break; } if ("net.sf.j2s.ajax.SimpleSerializable".equals(typeName)) { mark = "O"; break; } t = t.getSuperclass(); if (t == null) { break; } } while (true); } if (mark != null) { for (Iterator xiter = fragments.iterator(); xiter.hasNext();) { VariableDeclarationFragment var = (VariableDeclarationFragment) xiter.next(); int curDim = dims + var.getExtraDimensions(); if (curDim <= 1) { if (fieldsSerializables.length() > 0) { fieldsSerializables.append(", "); } /* * Fixed bug for the following scenario: * class NT extends ... { * public boolean typing; * public void typing() { * } * } */ String fieldName = var.getName().toString(); if (checkKeyworkViolation(fieldName)) { fieldName = "$" + fieldName; } String prefix = null; if (binding != null && checkSameName(binding, fieldName)) { prefix = "$"; } if (binding != null && isInheritedFieldName(binding, fieldName)) { fieldName = getFieldName(binding, fieldName); } if (prefix != null) { fieldName = prefix + fieldName; } fieldsSerializables.append("\"" + fieldName + "\", \""); if (mark.charAt(0) == 's' && curDim == 1) { fieldsSerializables.append("AX"); } else if (curDim == 1) { fieldsSerializables.append("A"); fieldsSerializables.append(mark); } else { fieldsSerializables.append(mark); } fieldsSerializables.append("\""); } } } } } } } return fieldsSerializables.toString(); }
From source file:net.sf.j2s.core.astvisitors.ASTTigerVisitor.java
License:Open Source License
protected void boxingNode(ASTNode element) { if (element instanceof Expression) { Expression exp = (Expression) element; if (exp.resolveBoxing()) { ITypeBinding typeBinding = exp.resolveTypeBinding(); if (typeBinding.isPrimitive()) { String name = typeBinding.getName(); Code type = PrimitiveType.toCode(name); String primitiveTypeName = null; if (type == PrimitiveType.INT) { primitiveTypeName = "Integer"; } else if (type == PrimitiveType.LONG) { primitiveTypeName = "Long"; } else if (type == PrimitiveType.FLOAT) { primitiveTypeName = "Float"; } else if (type == PrimitiveType.DOUBLE) { primitiveTypeName = "Double"; } else if (type == PrimitiveType.BOOLEAN) { primitiveTypeName = "Boolean"; } else if (type == PrimitiveType.BYTE) { primitiveTypeName = "Byte"; } else if (type == PrimitiveType.SHORT) { primitiveTypeName = "Short"; } else if (type == PrimitiveType.CHAR) { primitiveTypeName = "Character"; }// w ww . j av a 2 s. co m if (primitiveTypeName != null) { getBuffer().append("new " + primitiveTypeName + " ("); element.accept(this.visitor); getBuffer().append(")"); return; } } } else if (exp.resolveUnboxing()) { ITypeBinding typeBinding = exp.resolveTypeBinding(); if (!typeBinding.isPrimitive()) { String name = typeBinding.getQualifiedName(); String primitiveName = null; if ("java.lang.Integer".equals(name)) { primitiveName = "int"; } else if ("java.lang.Long".equals(name)) { primitiveName = "long"; } else if ("java.lang.Float".equals(name)) { primitiveName = "float"; } else if ("java.lang.Double".equals(name)) { primitiveName = "double"; } else if ("java.lang.Boolean".equals(name)) { primitiveName = "boolean"; } else if ("java.lang.Byte".equals(name)) { primitiveName = "byte"; } else if ("java.lang.Short".equals(name)) { primitiveName = "short"; } else if ("java.lang.Character".equals(name)) { primitiveName = "char"; } if (primitiveName != null) { getBuffer().append("("); element.accept(this.visitor); getBuffer().append(")." + primitiveName + "Value ()"); return; } } } } element.accept(this.visitor); }
From source file:org.decojer.cavaj.utils.Expressions.java
License:Open Source License
/** * New type.//from w w w. jav a 2s .c om * * @param t * type * @param context * context * @return AST type */ @SuppressWarnings("deprecation") public static Type newType(@Nonnull final T t, @Nonnull final Element context) { final AST ast = context.getCu().getAst(); // handle array first because annot(array()) is special if (t.isArray()) { if (ast.apiLevel() <= AST.JLS4) { final T componentT = t.getComponentT(); assert componentT != null; return ast.newArrayType(newType(componentT, context)); } final T elementT = t.getElementT(); assert elementT != null; for (T checkT = t; checkT != null && checkT.isArray(); checkT = checkT.getComponentT()) { if (checkT.isAnnotated()) { final ArrayType arrayType = ast.newArrayType(newType(elementT, context)); final List<Dimension> dimensions = arrayType.dimensions(); dimensions.clear(); for (T componentT = t; componentT != null && componentT.isArray(); componentT = componentT.getComponentT()) { final Dimension dimension = ast.newDimension(); final List<IExtendedModifier> annotations = dimension.annotations(); assert annotations != null; if (componentT.isAnnotated()) { Annotations.decompileAnnotations(componentT, annotations, context); } dimensions.add(dimension); } return arrayType; } } return ast.newArrayType(newType(elementT, context), t.getDimensions()); } if (t.isAnnotated()) { Type type = newType(t.getRawT(), context); if (ast.apiLevel() <= AST.JLS4) { log.warn("Cannot decompile type annotations for type '" + t + "' in Eclipse AST JLS4!"); return type; } // parameterized type is not directly annotateable in Eclipse; but DecoJer thinks the // whole type is meant, not just the generic type, hence translate here AnnotatableType annotatableType = (AnnotatableType) (type instanceof ParameterizedType ? ((ParameterizedType) type).getType() : type); qualified: if (annotatableType instanceof SimpleType) { // direct annotation of qualified types adds the annotations as first element, // strange Java spec doesn't allow "@A package.Name" but wants "package.@A Name" final Name typeName = ((SimpleType) annotatableType).getName(); if (!(typeName instanceof QualifiedName)) { break qualified; } final Name qualifier = ((QualifiedName) typeName).getQualifier(); final SimpleName name = ((QualifiedName) typeName).getName(); // cannot delete mandory childs, copy them annotatableType = ast.newNameQualifiedType((Name) ASTNode.copySubtree(ast, qualifier), (SimpleName) ASTNode.copySubtree(ast, name)); if (type instanceof ParameterizedType) { ((ParameterizedType) type).setType(annotatableType); } else { type = annotatableType; } } final List<IExtendedModifier> annotations = annotatableType.annotations(); assert annotations != null; Annotations.decompileAnnotations(t, annotations, context); return type; } // doesn't work, now with Dimension (see above): if (t.isArray()) { return // ast.newArrayType(newType(t.getComponentT(), contextT)); } if (t.isParameterized()) { final T genericT = t.getGenericT(); assert genericT != null; final ParameterizedType parameterizedType = ast.newParameterizedType(newType(genericT, context)); for (final T typeArg : t.getTypeArgs()) { assert typeArg != null; parameterizedType.typeArguments().add(newType(typeArg, context)); } return parameterizedType; } if (t.isWildcard()) { final T boundT = t.getBoundT(); if (boundT == null) { return ast.newWildcardType(); } if (t.isSubclassOf()) { final WildcardType wildcardType = ast.newWildcardType(); // default...newWildcardType.setUpperBound(true); wildcardType.setBound(newType(boundT, context)); return wildcardType; } final WildcardType wildcardType = ast.newWildcardType(); wildcardType.setUpperBound(false); wildcardType.setBound(newType(boundT, context)); return wildcardType; } if (t.isMulti()) { log.warn("Convert type for multi-type '" + t + "'!"); // prefer boolean for multi-type with 0 or 1, synchronous to newLiteral()! // prefer byte before char if no explicit char type given } if (t.is(T.BOOLEAN)) { return ast.newPrimitiveType(PrimitiveType.BOOLEAN); } if (t.is(T.BYTE)) { return ast.newPrimitiveType(PrimitiveType.BYTE); } if (t.is(T.CHAR)) { return ast.newPrimitiveType(PrimitiveType.CHAR); } if (t.is(T.SHORT)) { return ast.newPrimitiveType(PrimitiveType.SHORT); } if (t.is(T.INT)) { return ast.newPrimitiveType(PrimitiveType.INT); } if (t.is(T.FLOAT)) { return ast.newPrimitiveType(PrimitiveType.FLOAT); } if (t.is(T.LONG)) { return ast.newPrimitiveType(PrimitiveType.LONG); } if (t.is(T.DOUBLE)) { return ast.newPrimitiveType(PrimitiveType.DOUBLE); } if (t.is(T.VOID)) { return ast.newPrimitiveType(PrimitiveType.VOID); } if (t.isQualified()) { final T qualifierT = t.getQualifierT(); assert qualifierT != null : "cannot be null for qualified"; // could be ParamT etc., not decompileable with name as target; // restrict qualifications to really necessary enclosings: // t = Outer.Inner.InnerInner, t = Outer.Inner ==> Inner final T contextT = context.getT(); assert contextT != null; if (contextT.getFullName().startsWith(qualifierT.getFullName())) { // TODO full name has too much info yet (like annotations) return ast.newSimpleType(newSimpleName(t.getSimpleIdentifier(), ast)); } return ast.newQualifiedType(newType(qualifierT, context), newSimpleName(t.getSimpleIdentifier(), ast)); } // else fall through... return ast.newSimpleType(newTypeName(t, context)); }
From source file:org.eclipse.jdt.core.dom.ASTConverter.java
License:Open Source License
protected PrimitiveType.Code getPrimitiveTypeCode(char[] name) { switch (name[0]) { case 'i': if (name.length == 3 && name[1] == 'n' && name[2] == 't') { return PrimitiveType.INT; }/*from w ww .ja va2s .c o m*/ break; case 'l': if (name.length == 4 && name[1] == 'o' && name[2] == 'n' && name[3] == 'g') { return PrimitiveType.LONG; } break; case 'd': if (name.length == 6 && name[1] == 'o' && name[2] == 'u' && name[3] == 'b' && name[4] == 'l' && name[5] == 'e') { return PrimitiveType.DOUBLE; } break; case 'f': if (name.length == 5 && name[1] == 'l' && name[2] == 'o' && name[3] == 'a' && name[4] == 't') { return PrimitiveType.FLOAT; } break; case 'b': if (name.length == 4 && name[1] == 'y' && name[2] == 't' && name[3] == 'e') { return PrimitiveType.BYTE; } else if (name.length == 7 && name[1] == 'o' && name[2] == 'o' && name[3] == 'l' && name[4] == 'e' && name[5] == 'a' && name[6] == 'n') { return PrimitiveType.BOOLEAN; } break; case 'c': if (name.length == 4 && name[1] == 'h' && name[2] == 'a' && name[3] == 'r') { return PrimitiveType.CHAR; } break; case 's': if (name.length == 5 && name[1] == 'h' && name[2] == 'o' && name[3] == 'r' && name[4] == 't') { return PrimitiveType.SHORT; } break; case 'v': if (name.length == 4 && name[1] == 'o' && name[2] == 'i' && name[3] == 'd') { return PrimitiveType.VOID; } } return null; // cannot be reached }